Guava collections -- Maps

Guava全文介绍地址:Google Guava
这次主要介绍是的是com.google.common.collect.Maps.主要的作用是就与Java Map相关的静态的实用方法。在Guava中与之类似的还有Sets,Lists以及Queues这些类。
下面就介绍一个Maps的主要用法:

1、准备工作

1.基础类

@Builder
public class Book {

    private String author;
    private String title;
    private String publisher;
    private String isbn;
    private double price;

}

2.构造数据

public class MapsTest {

   private  Book book = new Book.Builder().isbn("ISBN123").title("book1").build();
   private  Book book2 = new Book.Builder().isbn("ISBN456").title("book2").build();
   private  Book book3 = new Book.Builder().isbn("ISBN789").title("book3").build();
   private  List<Book> books = Lists.newArrayList(book,book2,book3);

}

2、把集合中唯一Filed作用的Map

    @Test
    public void uniqueIndexTest(){
        Map<String,Book> bookMap = Maps.uniqueIndex(books,new Function<Book, String>() {
            @Override
            public String apply(Book book) {
                return book.getIsbn();
            }
        });
       assertThat(bookMap.get("ISBN123"),is(book));
       assertThat(bookMap.get("ISBN456"),is(book2));
       assertThat(bookMap.get("ISBN789"),is(book3));
    }

3、把Set中对象作为key,把field作为value

    @Test
    public void asMapTest(){
        Set<Book> bookSet = Sets.newHashSet(books);
        Map<Book,String> bookToIsbn = Maps.asMap(bookSet,new Function<Book, String>() {
            @Override
            public String apply(Book book) {
                return book.getIsbn();
            }
        });
        assertThat(bookToIsbn.get(book),is("ISBN123"));
        assertThat(bookToIsbn.get(book2),is("ISBN456"));
        assertThat(bookToIsbn.get(book3),is("ISBN789"));
    }

4、对Map的Value进行转换

    @Test
    public void transformValuesTest(){
        Map<String,Book> bookMap = Maps.uniqueIndex(books,new Function<Book, String>() {
            @Override
            public String apply(Book book) {
                return book.getIsbn();
            }
        });

        Map<String,String> map = Maps.transformValues(bookMap,new Function<Book, String>() {
            @Override
            public String apply(Book book) {
                 return book.getTitle();
            }
        });

        assertThat(map.get("ISBN123"),is("book1"));
        assertThat(map.get("ISBN456"),is("book2"));
        assertThat(map.get("ISBN789"),is("book3"));
    }

5、对Map的Entry进行转换

    @Test
    public void transformEntriesTest(){
        Map<String,Book> bookMap = Maps.uniqueIndex(books,new Function<Book, String>() {
            @Override
            public String apply(Book book) {
                return book.getIsbn();
            }
        });

        Map<String,String> map = Maps.transformEntries(bookMap,new Maps.EntryTransformer<String,Book,String>(){
            @Override
            public String transformEntry(String key, Book value) {
                StringBuilder builder = new StringBuilder();
                return builder.append(key).append("|").append(value.getTitle()).toString();
            }
        });

        assertThat(map.get("ISBN123"),is("ISBN123|book1"));
        assertThat(map.get("ISBN456"),is("ISBN456|book2"));
        assertThat(map.get("ISBN789"),is("ISBN789|book3"));
    }

更多功能等待你的发现。

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012410733/article/details/51856827
文章标签: Guava Maps Java
个人分类: Guava
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭