Is there something like a KeyValuePair in Java?
I have a Very long list of elements of the following class:
public class Foo {
int id;
Set<String> items;
}
which is stored here:
LinkedList<Foo> myList;
each time I search for a item, I iterate over the list and search for the Item, but this takes to much time. I want to do something like this:
myList.get(123) => items of the Foo with id = 123
You can use Map in Java for that purpose. It will allow key, value pairs.
Map<Integer,Set<String>> map = new HashMap<Integer,Set<String>>();
Adding item to map
Set<String> set = new HashSet<String>();
set.add("ABC");
set.add("DEF");
map.put(123,set);
Getting item from map
map .get(123) will give Set<String> associated with id 123
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With