How to create a TreeMap to store words and the lines where they appear?

2

I need to iterate through a text and take each word of it, store it in a structure (as TreeMap ) and next to each word the lines in which they appear, to generate an index.

My big question has been how to structure this, because using TreeMap I can put the word, but only a Integer for each word?

    
asked by anonymous 23.06.2014 / 22:05

1 answer

2

Make one:

TreeMap<String, List<Integer>> mapeamento = new TreeMap<String, ArrayList<Integer>>();

So you will have for each word (String) a list of rows in which it occurs. This list supports more than one occurrence per line and can be ordered through Collections.sort() if the insertion of rows in the list is no longer ordered.

    
24.06.2014 / 00:04