Guava Collections - Maps

1. Maven Dependency

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>org.fool.guava</groupId>
	<artifactId>guava</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>guava</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>19.0</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>
	</dependencies>
</project>

2.Book.java

package org.fool.guava.collections.map;

import java.util.Date;

import com.google.common.base.MoreObjects;

public class Book {
	private String isbn;
	private String name;
	private String author;
	private Date publishDate;

	public Book(String isbn, String name, String author, Date publishDate) {
		this.isbn = isbn;
		this.name = name;
		this.author = author;
		this.publishDate = publishDate;
	}

	public String getIsbn() {
		return isbn;
	}

	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public Date getPublishDate() {
		return publishDate;
	}

	public void setPublishDate(Date publishDate) {
		this.publishDate = publishDate;
	}

	@Override
	public String toString() {
		return MoreObjects.toStringHelper(this).add("ISBN", this.getIsbn()).add("name", this.getName())
				.add("author", this.getAuthor()).add("publish date", this.getPublishDate()).toString();
	}
}

3. MapTest.java

package org.fool.guava.collections.map;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;

public class MapTest {

	private Book book1;
	private Book book2;
	private Book book3;
	private Book book4;
	private List<Book> bookList;

	@Before
	public void setUp() throws Exception {
		book1 = new Book("9787121281556", "Hadoop in Action", "LiuBei", new Date());
		book2 = new Book("9787121281557", "Spark in Action", "GuanYu", new Date());
		book3 = new Book("9787121281558", "Storm in Action", "ZhangFei", new Date());
		book4 = new Book("9787121281559", "Kafka in Action", "ZhaoYun", new Date());

		bookList = Lists.newArrayList(book1, book2, book3, book4);
	}

	@Test
	public void testUniqueIndex() {
		ImmutableMap<String, Book> bookMap = Maps.uniqueIndex(bookList.iterator(), new Function<Book, String>() {

			@Override
			public String apply(Book input) {
				return input.getIsbn();
			}
		});

		bookMap.forEach((k, v) -> System.out.println(Joiner.on(" ").join(k, v)));
	}

	@Test
	public void testArrayListMultiMap() {
		Multimap<String, String> multiMap = ArrayListMultimap.create();
		multiMap.put("Country Shu", "LiuBei");
		multiMap.put("Country Shu", "GuanYu");
		multiMap.put("Country Shu", "ZhangFei");
		multiMap.put("Country Shu", "ZhangFei");
		multiMap.put("Country Wei", "CaoCao");
		multiMap.put("Country Wei", "XunYu");
		multiMap.put("Country Wei", "GuoJia");

		List<String> valueList = Lists.newArrayList("LiuBei", "GuanYu", "ZhangFei", "ZhangFei");
		assertEquals(multiMap.get("Country Shu"), valueList);

		Map<String, Collection<String>> map = multiMap.asMap();
		map.forEach((k, v) -> System.out.println(Joiner.on(" ").join(k, v)));
	}

	@Test
	public void testHashMultimap() {
		Multimap<String, String> multiMap = HashMultimap.create();
		multiMap.put("Country Shu", "LiuBei");
		multiMap.put("Country Shu", "GuanYu");
		multiMap.put("Country Shu", "ZhangFei");
		multiMap.put("Country Shu", "ZhangFei");

		assertThat(multiMap.size(), equalTo(3));

		multiMap.asMap().forEach((k, v) -> System.out.println(Joiner.on(" ").join(k, v)));
	}

	@Test
	public void testBiMapForcePut() {
		BiMap<String, String> biMap = HashBiMap.create();
		biMap.put("1", "LvBu");
		biMap.forcePut("2", "LvBu");

		assertThat(biMap.containsKey("1"), equalTo(false));
		assertThat(biMap.containsKey("2"), equalTo(true));
	}

	@Test
	public void testBiMapInverse() throws Exception {
		BiMap<String, String> biMap = HashBiMap.create();
		biMap.put("1", "LvBu");
		biMap.put("2", "DiaoChan");

		assertThat(biMap.get("1"), equalTo("LvBu"));
		assertThat(biMap.get("2"), equalTo("DiaoChan"));

		BiMap<String, String> inverseMap = biMap.inverse();
		assertThat(inverseMap.get("LvBu"), equalTo("1"));
		assertThat(inverseMap.get("DiaoChan"), equalTo("2"));
	}

}

猜你喜欢

转载自agilestyle.iteye.com/blog/2288889
今日推荐