Guava(四):集合基础总结之Map

其实Guavad的集合操作适合我们平时使用的原生的集合是一样的,只是他将我们平时操作的集合更加的流畅优雅加单。其实Map就和List一样也是在创建的时候和其他的一些很赞的方法,但是呢好像这些方法我们平时的工作中用到的很少,但是呢我们还是来看看把。

首先说一下这几个方法:

1:创建Map方法:

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

2:集合diff方法:两个Map中都有的映射项,包括匹配的键与值

	MapDifference<String,String> diffMap = Maps.difference(map,guavaMap);

3:也是集合方法:键只存在于左边Map的映射项,也就是说左边参数map里面有的而右边没有的就展示:

	entriesOnlyOnLeft()

4:也是集合方法:键只存在于右边Map的映射项

	entriesOnlyOnRight()

好了上代码:

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.MapDifference;
import com.google.common.collect.Maps;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by luyangli on 15-9-19.
 */
public class MapsTest {
    public static void main(String[] args) {
        Map<String,String> map = new HashMap<String, String>();
        map.put("3","c");
        map.put("1","a");
        map.put("2","b");
        map.put("4","t");
        System.out.println("========原生Map=======");
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }

        Map<String,String> guavaMap = Maps.newHashMap();
        guavaMap.put("3","c");
        guavaMap.put("1","a");
        guavaMap.put("2","b");
        guavaMap.put("5","t");
        System.out.println("========Guava Map=======");
        for (Map.Entry<String, String> entry : guavaMap.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }

        MapDifference<String,String> diffMap = Maps.difference(map,guavaMap);
        System.out.println("========Guava diff Map=======");
        for (Map.Entry<String, String> entry : diffMap.entriesInCommon().entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
        System.out.println("========Guava diff Left Map=======");
        for (Map.Entry<String, String> entry : diffMap.entriesOnlyOnLeft().entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
        System.out.println("========Guava diff Right Map=======");
        for (Map.Entry<String, String> entry : diffMap.entriesOnlyOnRight().entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }

//        Map<String, Integer> left = ImmutableMap.of("a", 1, "b", 2, "c", 3);
//        Map<String, Integer> right = ImmutableMap.of("a", 1, "b", 2, "c", 3);
//        MapDifference<String, Integer> diff = Maps.difference(left, right);
//        Map<String,Integer> map2 = diff.entriesInCommon();
//        System.out.println("========Guava diff Map=======");
//        for (Map.Entry<String, Integer> entry : map2.entrySet()) {
//            System.out.println(entry.getKey() + ":" + entry.getValue());
//        }

     }
}

我们来看一下效果:

========原生Map=======
3:c
2:b
1:a
4:t
========Guava Map=======
3:c
2:b
1:a
5:t
========Guava diff Map=======
3:c
2:b
1:a
========Guava diff Left Map=======
4:t
========Guava diff Right Map=======
5:t
========Guava diff Map=======
b:2
c:3
a:1

好了先写在这,我要去健身房了,回来补上Map的一下排序。。。

好高心昨天在健身房要到了心仪的女生的微信号,好开心好开心,昨天我也是购拼的,昨天为了要微信号在健身房带了4个小时,累死了。

好了今天我们学习以下Map的几种遍历方法:我在今天整理了四种Map的遍历方式,现在原样奉上:

System.out.println("=====第一种MapKey遍历,遍历KeyValue=====");
//第一种MapKey遍历,遍历KeyValue
for (String key : map.keySet()) {
    System.out.println("Key : " + key + " and value : " + map.get(key));
}

//第二种使用entries进行遍历
System.out.println("=====第二种使用entries进行遍历=====");
for (Map.Entry<String, String> entry : map.entrySet()) {
    System.out.println("Key : " + entry.getKey() + " and value : " + entry.getValue());
}

//第三种通过Map.entrySet使用iterator遍历keyvalue
System.out.println("=====第三种通过Map.entrySet使用iterator遍历keyvalue=====");
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()){
    Map.Entry<String,String> entry = it.next();
    System.out.println("Key : " + entry.getKey() + " and value : " + entry.getValue());
}

//第四种获取MapKey或者Value
System.out.println("=====第四种获取MapKey=====");
for (String key : guavaMap.keySet()) {
    System.out.println("Key : " + key);
}

System.out.println("=====第四种获取MapValue=====");
for (String value : guavaMap.values()) {
    System.out.println("value : " + value);
}

看一下结果:

=====第一种Map的Key遍历,遍历Key和Value=====
Key : 3 and value : c
Key : 2 and value : b
Key : 1 and value : a
Key : 4 and value : t
=====第二种使用entries进行遍历=====
Key : 3 and value : c
Key : 2 and value : b
Key : 1 and value : a
Key : 4 and value : t
=====第三种通过Map.entrySet使用iterator遍历key和value=====
Key : 3 and value : c
Key : 2 and value : b
Key : 1 and value : a
Key : 4 and value : t
=====第四种获取Map的Key=====
Key : 3
Key : 2
Key : 1
Key : 5
=====第四种获取MapValue=====
value : c
value : b
value : a
value : t

其实我们的几种方法都是OK的,就是有效率之分啦,人家提供几种不同的方法就是有几种区别啦:1.就是方法升级,也就是更方便啦 2.就是有更高效的手段啦 3.....

所以我们现在来看一下我们的几种方法:第四种不是很难常用,就不在比较之内。

第一种虽然很简便,耶很好看清楚,但是呢这个效率是最低的,应为从Map中取出Key值在通过Key值来取出Value,这个是很费效率的操作,这就是简便的代价--牺牲效率。

第三种方法是之前map 老版本的唯一指定遍历方式,第二种是他的升级,也就是说他们的效率相差不多。但是呢现在大家都习惯于用第二种方式啦。

至于具体的问题,具体分析,具体决策啦。。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值