Advertisement
Guest User

Untitled

a guest
May 25th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. package prIndicePalabras;
  2.  
  3. import java.io.PrintWriter;
  4. import java.util.Collection;
  5. import java.util.Map;
  6. import java.util.Scanner;
  7.  
  8. import java.util.SortedMap;
  9. import java.util.SortedSet;
  10. import java.util.StringJoiner;
  11. import java.util.TreeMap;
  12. import java.util.TreeSet;
  13.  
  14.  
  15. public class IndiceLineas extends IndiceAbstracto {
  16. private SortedMap<String, SortedSet<Integer>> indice;
  17.  
  18. public IndiceLineas() {
  19. indice = new TreeMap<>();
  20. }
  21.  
  22. private void anyadir(String pal, int nlinea) {
  23. pal = pal.toLowerCase().trim();
  24. SortedSet<Integer> ln = indice.get(pal);
  25. if (ln == null) {
  26. ln = new TreeSet<>();
  27. indice.put(pal,ln);
  28. } else {
  29. ln.add(nlinea);
  30. }
  31. }
  32.  
  33. public void resolver(String delim) {
  34. int nlinea = 0;
  35. for(String f:frases) {
  36. ++nlinea;
  37. try (Scanner sc = new Scanner(f)) {
  38. sc.useDelimiter(delim);
  39. while(sc.hasNext()) {
  40. anyadir(sc.next(), nlinea);
  41. }
  42. }
  43. }
  44. }
  45.  
  46.  
  47. public void presentarIndice(PrintWriter pw) {
  48. for (Map.Entry<String, SortedSet<Integer>> e : indice.entrySet()) {
  49. System.out.printf("%-10s %s\n", e.getKey(), col2Str(e.getValue()));
  50. }
  51. }
  52. private String col2Str(Collection<Integer> col) {
  53. StringJoiner sj = new StringJoiner(",", "<", ">");
  54. for (Integer i: col) {
  55. sj.add(i.toString());
  56. }
  57. return sj.toString();
  58. }
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement