Is there a collection in which I can hold a string and an integer with duplicate values?

I have text and I will do some filtering based on the number of words of each phrase.

I was thinking about using the Map, in the key in which I put the phrase, int value, the number of words, iterating on the map and deleting lines that are of little importance than something that was mentioned earlier.

  • There is a map where I can store duplicate entries (phrase and quantity)?
  • Can I get from the card in the order in which I placed the elements? This is because it is text, so the order of phrases is important.

Example

Original text

My name is Renato | 4
Today is Tuesday | 3
I live in Brazil | 4
My name is Renato | 4
20 years | 2
StackOverflow is a fantastic website | 5

Filter removal of phrases with less than 4 words

Final text

My name is Renato
I live in Brazil
My name is Renato
StackOverflow is a fantastic website

+4
source share
3 answers

Is there a map where I can post repeated entries (phrase and quantity)?

Why not

 Map<Integer, List<String>> wordCountToStringsMap 

Can I get from the card in the order in which I placed the items? This is because it is text, so the order of phrases is important.

+3
source

I think you can go to LinkedHashMap. It keeps the order in which you entered

 Map<String,Integer> map=new LinkedHashMap<String,Integer>(); 

Then you can add and perform your operations.

+1
source

Well, the map interface looks exactly as you need. The specific implementation should be LinkedHashMap, because you want to be able to retrieve the elements in the order in which you inserted them.

So you will have

 Map <String, Integer> myMap = new LinkedHashMap <String, Integer> (); 

then just add the lines, iterate over them using for (String phrase: myMap.keys ()) and do what you want with them!

0
source

All Articles