Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

key value pair implementation in java?

Tags:

java

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
like image 907
gurehbgui Avatar asked Jul 30 '26 22:07

gurehbgui


1 Answers

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
like image 168
PSR Avatar answered Aug 01 '26 12:08

PSR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!