Example usage for com.google.common.collect ImmutableListMultimap of

List of usage examples for com.google.common.collect ImmutableListMultimap of

Introduction

In this page you can find the example usage for com.google.common.collect ImmutableListMultimap of.

Prototype

public static <K, V> ImmutableListMultimap<K, V> of(K k1, V v1, K k2, V v2) 

Source Link

Document

Returns an immutable multimap containing the given entries, in order.

Usage

From source file:com.tinspx.util.example.Collect.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static void selector() {
    Function<Object, String> search = CollectUtils.selector()
            //select value mapped to "a" in a Map or Multimap
            .key("a")
            //select the first value that is a Multimap
            .value(Predicates.instanceOf(Multimap.class))
            //select value mapped to "b" in a Map or Multimap
            .key("b")
            //select the value at index 1 (the second element)
            .value(1)//  w  w w .  j ava2  s.  c o m
            //build the Function expecting a String result
            .get(String.class);

    //create the object to traverse, this can contain any hierarchy
    //of Collection, List, Set, Multiset, Map, Multimap, Table
    Map map = MapUtils.newMutableSingletonMap("a",
            Arrays.asList(null, ImmutableListMultimap.of("b", "b-1", "b", "b-2")));

    String result = search.apply(map);
    System.out.println("map: " + map);
    System.out.println("result: " + result);
    //prints
    //map: {a=[null, {b=[b-1, b-2]}]}
    //result: b-2

    //now slightly modify the map so the search will fail
    map.put("a", Arrays.asList(null, ImmutableListMultimap.of("b", "b-1",
            //second value mapped to "b" is an Integer instead
            //of a String so the search will fail
            "b", 1)));

    result = search.apply(map);
    System.out.println("map: " + map);
    System.out.println("result: " + result);
    //prints
    //{a=[null, {b=[b-1, 1]}]}
    //null
}