前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >guava:java:java.util.Map和java.util.Set的Key类型转换

guava:java:java.util.Map和java.util.Set的Key类型转换

作者头像
10km
发布2018-01-03 14:36:36
9880
发布2018-01-03 14:36:36
举报
文章被收录于专栏:10km的专栏10km的专栏

昨天写了一博客《java:java.util.Map和java.util.Set的Key类型转换》,主要是想实现以java.util.MapKey类型转换,今天有空有研究了一下guava的代码,发现基于guava提供的API也是可以实现Key类型转换的: 关键就是Maps提供了uniqueIndex方法,可以将Map转换成Key不同的Map。

代码语言:javascript
复制
package net.gdface.facelog;

import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.junit.Test;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterators;
import com.google.common.collect.Maps;
import com.google.common.collect.Maps.EntryTransformer;

public class TestTransform {
    /** 
     * convert {@code Map<K1,V>} to {@code Map<K2,V>}   
     * @return {@linkplain ImmutableMap}
     */
    public static final <K1,K2,V>Map<K2,V> transform(Map<K1,V>fromMap,final Function<K1,K2>transformer){
        checkNotNull(fromMap,"fromMap is null");
        checkNotNull(transformer,"transformer is null");
        // 新的Map对象Key类型已经是K2了,只是Value类型成了Entry
        ImmutableMap<K2, Entry<K1, V>> k2Entry = Maps.uniqueIndex(fromMap.entrySet(), new Function<Entry<K1, V>,K2>(){
            @Override
            public K2 apply(Entry<K1, V> input) {               
                return transformer.apply(input.getKey());
            }});
        // 再做一次转换将Entry<K1, V>转换成V,这个过程并没有创建新的Map,只是创建了k2Entry的代理对象
        Map<K2, V> k2V = Maps.transformEntries(k2Entry, new EntryTransformer<K2,Entry<K1,V>,V>(){
            @Override
            public V transformEntry(K2 key, Entry<K1, V> value) {
                return value.getValue();
            }});
        return k2V;
    }
    /** 
     * convert {@code Set<E1>} to {@code Set<E2>}
     * @return {@link ImmutableSet} 
     */
    public static final <E1,E2>Set<E2> transform(final Set<E1>fromSet,final Function<E1,E2>transformer){
        checkNotNull(fromSet,"fromMap is null");
        checkNotNull(transformer,"transformer is null");
        return ImmutableSet.copyOf(Iterators.transform(fromSet.iterator(), transformer));
    }
    @Test
    public void test() {
        Map<String, String> m1 = Maps.<String,String>newLinkedHashMap();
        m1.put("1", "apple");
        m1.put("2", "orangle");
        m1.put("3", "banana");
        System.out.println(m1);
        Map<Integer, String> m2 = transform(m1, new Function<String,Integer>(){

            @Override
            public Integer apply(String input) {
                return Integer.valueOf(input);
            }});
        System.out.println(m2.toString());
    }

}

注意: 这个方法相比上篇博客中的方法要简单的多但是有区别的: 返回的Map对象是一个新的对象,与原Map对象没有任何关联,并且是不可变的(immutable)。 而上一篇博客中的方法返回的Map对象则是原对象的代理对象,并且是可变的(mutable),对新对象的任何操作实际都是对原对象的操作。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年10月21日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档