Guava入门~Maps

import bbejeck.guava.common.model.Book;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.MapDifference;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.junit.Test;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;


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 books = Lists.newArrayList(book,book2,book3);

    @Test
    public void uniqueIndexTest(){
    	Map emptyMap = Maps.newHashMap();
        Map bookMap = Maps.uniqueIndex(books,new Function() {
            @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));
       
       Map bookMap2 = Maps.newHashMap();
       bookMap2.put("ISBN123", book);
       bookMap2.put("ISBN456", book2);
       
       MapDifference diffMap = Maps.difference(bookMap, bookMap2);
       System.out.println(diffMap);
    }

    @Test
    public void asMapTest(){
        Set bookSet = Sets.newHashSet(books);
        Map bookToIsbn = Maps.asMap(bookSet,new Function() {
            @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"));
    }

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

        Map map = Maps.transformValues(bookMap,new Function() {
            @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"));
    }

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

        Map map = Maps.transformEntries(bookMap,new Maps.EntryTransformer(){
            @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"));
    }

}

新建Map

Map emptyMap = Maps.newHashMap();

差集

MapDifference diffMap = Maps.difference(bookMap, bookMap2);

List转Map

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

Set转Map

Set bookSet = Sets.newHashSet(books);
Map bookToIsbn = Maps.asMap(bookSet,new Function() {
    @Override
    public String apply(Book book) {
        return book.getIsbn();
    }
});

转换--转换值

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

转换--转换Entity

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

Book

package bbejeck.guava.common.model;

import com.google.common.base.Objects;
import com.google.common.collect.ComparisonChain;
import static com.google.common.base.Preconditions.*;
/**
 * User: Bill Bejeck
 * Date: 3/27/13
 * Time: 10:56 PM
 */
public class Book implements Comparable {

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

    public Book(String author, String title, String publisher, String isbn, double price) {
        this.author = checkNotNull(author,"Author can't be null");
        this.title = checkNotNull(title,"Title can't be null");
        this.publisher = checkNotNull(publisher,"Publisher can't be null");
        this.isbn = checkNotNull(isbn,"ISBN can't be null");
        checkArgument(price > 0.0,"Price must be more than zero");
        this.price = price;
    }

    public String getAuthor() {
        return author;
    }

    public String getTitle() {
        return title;
    }

    public String getPublisher() {
        return publisher;
    }

    public String getIsbn() {
        return isbn;
    }


    public double getPrice() {
        return price;
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(this)
                .omitNullValues()
                .add("title", title)
                .add("author", author)
                .add("publisher", publisher)
                .add("price",price)
                .add("isbn", isbn).toString();
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(title, author, publisher, isbn,price);
    }

    @Override
    public boolean equals(Object obj) {
        return Objects.equal(this, obj);
    }

    @Override
    public int compareTo(Book o) {
        return ComparisonChain.start()
                .compare(this.title, o.getTitle())
                .compare(this.author, o.getAuthor())
                .compare(this.publisher, o.getPublisher())
                .compare(this.isbn, o.getIsbn())
                .compare(this.price, o.getPrice())
                .result();
    }

    public static class Builder {
        private String author = "The Author";
        private String title = "A BOOK";
        private String publisher = "Publisher";
        private String isbn = "ABCDE1234567";
        private double price = 0.99;

        public Builder author(String author) {
            this.author = author;
            return this;
        }

        public Builder title(String title) {
            this.title = title;
            return this;
        }

        public Builder publisher(String publisher) {
            this.publisher = publisher;
            return this;
        }

        public Builder isbn(String isbn) {
            this.isbn = isbn;
            return this;
        }

        public Builder price(double price) {
            this.price = price;
            return this;
        }

        public Book build(){
            return new Book(this.author,this.title,this.publisher,this.isbn,this.price);
        }
    }

}

你可能感兴趣的:(Java,guava,junit,java)