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

Asked

Viewed 347 times

2

I need to iterate on a text and take every word of it, store it in a structure (like TreeMap) and together with each word the lines in which they appear, to generate a remissive index.

My big doubt has been on how to structure this, because using TreeMap I can put the word, but only one Integer for every word?

1 answer

2


Make a:

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.