Over a million developers have joined DZone.

Find An Element from Map and Map contains values as List using Guava


Finding an element from Map bit  complex but using Google Guava API its simply whole logic. The above code explain how can f we ind an element from MAP object and it contains list of objects.

Iterables.concat() method collect lists and combine into single list.

Iterables.find() method find an element from the list by specified Predicate

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
public class FindItemFromMap
public static void main(String args[])
Map> map = new HashMap<>();
		List fruits = Arrays.asList("apple","orange","banana");
		List juice=   Arrays.asList("pepsi","fanta","sprite");
		map.put("juice", juice);
		String result = Iterables.find(Iterables.concat(map.values()), new Predicate()
		public boolean apply(@Nullable String input) {
			return "banana".equals(input);
	}) ;
	System.out.println("Found "+result);

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}