Guava Collections - Multimap Interface

Multimap Interface

Multimap interface extends Map so that its keys can be mapped to multiple values at a time.

package org.fool.guava.collections;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

public class MultimapTest {
	public static void main(String[] args) {
		Multimap<String, String> multimap = ArrayListMultimap.create();

		multimap.put("lower", "a");
		multimap.put("lower", "b");
		multimap.put("lower", "c");
		multimap.put("lower", "d");
		multimap.put("lower", "e");

		multimap.put("upper", "A");
		multimap.put("upper", "B");
		multimap.put("upper", "C");
		multimap.put("upper", "D");

		Collection<String> lowerCollection = multimap.get("lower");

		System.out.println("Initial lower case list");
		System.out.println(lowerCollection.toString());
		lowerCollection.add("f");
		System.out.println("Modified lower case list");
		System.out.println(lowerCollection.toString());

		Collection<String> upperCollection = multimap.get("upper");
		System.out.println("Initial upper case list");
		System.out.println(upperCollection.toString());
		upperCollection.remove("D");
		System.out.println("Modified upper case list");
		System.out.println(upperCollection.toString());

		Map<String, Collection<String>> map = multimap.asMap();
		System.out.println("Multimap as a map");
		map.forEach((key, value) -> System.out.println(key + ":" + value));
		
		System.out.println("Keys of Multimap");
		Set<String> keys = multimap.keySet();
		keys.forEach(key -> System.out.println(key));
		
		System.out.println("Values of Multimap");
		Collection<String> values = multimap.values();
		System.out.println(values);
	}
}

 output

Initial lower case list
[a, b, c, d, e]
Modified lower case list
[a, b, c, d, e, f]
Initial upper case list
[A, B, C, D]
Modified upper case list
[A, B, C]
Multimap as a map
lower:[a, b, c, d, e, f]
upper:[A, B, C]
Keys of Multimap
lower
upper
Values of Multimap
[a, b, c, d, e, f, A, B, C]

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值