Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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

·
Free Resource

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("fruits",fruits);
		map.put("juice", juice);
		String result = Iterables.find(Iterables.concat(map.values()), new Predicate
    
     ()
		{
		@Override
		public boolean apply(@Nullable String input) {
			return "banana".equals(input);
		}
	}) ;
	System.out.println("Found "+result);
}
}
    
   
  
 
Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}