Guava之Maps教程

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/neweastsun/article/details/79944839

Guava之Maps教程

本文我们学习Guava Maps对java Map封装和升级,实现强大、简洁应用方式。
首先让我们看看Guava不用new创建HashMap:

Map<String, String> aNewMap = Maps.newHashMap();

ImmutableMap

创建不可变Map:

    @Test
    public void whenCreatingImmutableMap_thenCorrect() {
        Map<String, Integer> salary = ImmutableMap.<String, Integer> builder()
                .put("John", 1000)
                .put("Jane", 1500)
                .put("Adam", 2000)
                .put("Tom", 2000)
                .build();

        assertEquals(1000, salary.get("John").intValue());
        assertEquals(2000, salary.get("Tom").intValue());
    }

salary初始化后,不能在更改,调用put方法会报java.lang.UnsupportedOperationException异常。

SortedMap

本节我们看下创建SortMap,下面示例我们使用Guava相应builder创建一个有序map:

@Test
public void whenUsingSortedMap_thenKeysAreSorted() {
    ImmutableSortedMap<String, Integer> salary = new ImmutableSortedMap
      .Builder<String, Integer>(Ordering.natural())
      .put("John", 1000)
      .put("Jane", 1500)
      .put("Adam", 2000)
      .put("Tom", 2000)
      .build();

    assertEquals("Adam", salary.firstKey());
    assertEquals(2000, salary.lastEntry().getValue().intValue());
}

我们看到salary中记录按照字母进行排序。

BiMap

本节我们讨论如何使用BiMap,BiMap也可以反向把值映射到键,只要确保值唯一。
下面示例中,我们创建BiMap,并使用其inverse()方法:

@Test
public void whenCreateBiMap_thenCreated() {
    BiMap<String, Integer> words = HashBiMap.create();
    words.put("First", 1);
    words.put("Second", 2);
    words.put("Third", 3);

    assertEquals(2, words.get("Second").intValue());
    assertEquals("Third", words.inverse().get(3));
}

BiMap是双向映射,但要确保值和键都唯一。

Multimap

本节我们使用Multimap ,对每个键关联多个值:

@Test
public void whenCreateMultimap_thenCreated() {
    Multimap<String, String> multimap = ArrayListMultimap.create();
    multimap.put("fruit", "apple");
    multimap.put("fruit", "banana");
    multimap.put("pet", "cat");
    multimap.put("pet", "dog");

    assertThat(multimap.get("fruit"), containsInAnyOrder("apple", "banana"));
    assertThat(multimap.get("pet"), containsInAnyOrder("cat", "dog"));
}

Multimap的get方法返回 java.util.Collection,上述测试代码除了junit,还需要增加:

testCompile group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3'

Table

当需要多余一个键索引值时,需要Table。下面示例中,我们使用Table存储城市之间距离:

@Test
public void whenCreatingTable_thenCorrect() {
    Table<String,String,Integer> distance = HashBasedTable.create();
    distance.put("London", "Paris", 340);
    distance.put("New York", "Los Angeles", 3940);
    distance.put("London", "New York", 5576);

    assertEquals(3940, distance.get("New York", "Los Angeles").intValue());
    assertThat(distance.columnKeySet(), 
      containsInAnyOrder("Paris", "New York", "Los Angeles"));
    assertThat(distance.rowKeySet(), containsInAnyOrder("London", "New York"));
}

Table

@Test
public void whenTransposingTable_thenCorrect() {
    Table<String,String,Integer> distance = HashBasedTable.create();
    distance.put("London", "Paris", 340);
    distance.put("New York", "Los Angeles", 3940);
    distance.put("London", "New York", 5576);

    Table<String, String, Integer> transposed = Tables.transpose(distance);

    assertThat(transposed.rowKeySet(), 
      containsInAnyOrder("Paris", "New York", "Los Angeles"));
    assertThat(transposed.columnKeySet(), containsInAnyOrder("London", "New York"));
}

ClassToInstanceMap

本节介绍ClassToInstanceMap,把类作为键映射至对象:

@Test
public void whenCreatingClassToInstanceMap_thenCorrect() {
    ClassToInstanceMap<Number> numbers = MutableClassToInstanceMap.create();
    numbers.putInstance(Integer.class, 1);
    numbers.putInstance(Double.class, 1.5);

    assertEquals(1, numbers.get(Integer.class));
    assertEquals(1.5, numbers.get(Double.class));
}

Integer,Double都继承子Number,ClassToInstanceMap让不同的子类作为key。

Multimap分组

通过Multimap对List进行分组,下面示例中是使用Multimaps.index方法依据list元素的长度进行分组。

@Test
public void whenGroupingListsUsingMultimap_thenGrouped() {
    List<String> names = Lists.newArrayList("John", "Adam", "Tom");
    Function<String,Integer> func = new Function<String,Integer>(){
        public Integer apply(String input) {
            return input.length();
        }
    };
    Multimap<Integer, String> groups = Multimaps.index(names, func);

    assertThat(groups.get(3), containsInAnyOrder("Tom"));
    assertThat(groups.get(4), containsInAnyOrder("John", "Adam"));
}

总结

本文介绍Guava库中使用Map的有用示例,从简单的新建HashMap到复杂Map相关应用,有些功能是jdk没有提供,实际中却非常有用的。

阅读更多
想对作者说点什么?

博主推荐

换一批

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