Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package prIndicePalabras;
- import java.io.PrintWriter;
- import java.util.Collection;
- import java.util.Map;
- import java.util.Scanner;
- import java.util.SortedMap;
- import java.util.SortedSet;
- import java.util.StringJoiner;
- import java.util.TreeMap;
- import java.util.TreeSet;
- public class IndiceLineas extends IndiceAbstracto {
- private SortedMap<String, SortedSet<Integer>> indice;
- public IndiceLineas() {
- indice = new TreeMap<>();
- }
- private void anyadir(String pal, int nlinea) {
- pal = pal.toLowerCase().trim();
- SortedSet<Integer> ln = indice.get(pal);
- if (ln == null) {
- ln = new TreeSet<>();
- indice.put(pal,ln);
- } else {
- ln.add(nlinea);
- }
- }
- public void resolver(String delim) {
- int nlinea = 0;
- for(String f:frases) {
- ++nlinea;
- try (Scanner sc = new Scanner(f)) {
- sc.useDelimiter(delim);
- while(sc.hasNext()) {
- anyadir(sc.next(), nlinea);
- }
- }
- }
- }
- public void presentarIndice(PrintWriter pw) {
- for (Map.Entry<String, SortedSet<Integer>> e : indice.entrySet()) {
- System.out.printf("%-10s %s\n", e.getKey(), col2Str(e.getValue()));
- }
- }
- private String col2Str(Collection<Integer> col) {
- StringJoiner sj = new StringJoiner(",", "<", ">");
- for (Integer i: col) {
- sj.add(i.toString());
- }
- return sj.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement