javaguavamultimap

Java Multimap search for value


I have a Multimap and need to search for the values. It looks like this

ListMultiMap<String, Person> pers =  ArrayListMultimap.create();
....
Person person = new Person();
person.setName(name);
peson.setAge(age);
person.setId(id);
...
pers.put(name, person);

I need the name as a key, and it should be possible to add e.g. two Persons with Name "Bob". The ID should be unique.

For Example:

Name: Bob, ID:1
Name: Bob, ID:2

I know how to get the values for the key "Bob" out of the map. But how can I get only the values for Bob with the ID 1?


Solution

  • This will retrieve the person Bob with an ID of 1:

    ListMultiMap<String, Person> pers =  ArrayListMultimap.create();
    List<Person> persons = pers.get("Bob");
    for(Person p : persons){
        if (p.getId() == 1){
            //do something
        }
    }