Reputation: 13264
I have a List
of Map.Entry
, in roder to have a list of pairs.
In my List
, I will be inserting pairs where the key is a Long
value representing a UNIX epoch, and the value is a String
with some text:
ArrayList<Map.Entry<Long, String>> events = new ArrayList<Map.Entry<Long, String>>();
events.add(new Map.Entry<Long, String>(140000000, "Test event"));
I want to remove Map.Entry with duplicated key, so each UNIX epoch have only an associated String
. Any fancy way of doing this without iterating the whole list manually?
Bonus if the String
value is a concat of all the String
values from the same key.
Upvotes: 2
Views: 84
Reputation: 4080
Use a hashMap - it only allows a single entry per key, so will overwrite. Then you can just get the MapEntries out of the Map using map.entrySet. This works if the keys are the same but the values are different.
Just putting the entries into the Set will work if the key and value pairs are the same, i.e. they are identical entries, as opposed to just having the same key. Look up the behaviour of MapEntry.equals() to understand this.
Upvotes: 2