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

来源:互联网 发布:织梦cms加千牛云教程 编辑:程序博客网 时间:2024/05/29 04:22

其实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:c2:b1:a4:t========Guava Map=======3:c2:b1:a5:t========Guava diff Map=======3:c2:b1:a========Guava diff Left Map=======4:t========Guava diff Right Map=======5:t========Guava diff Map=======b:2c:3a:1

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

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

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

System.out.println("=====第一种MapKey遍历,遍历KeyValue=====");//第一种MapKey遍历,遍历KeyValuefor (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遍历keyvalueSystem.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或者ValueSystem.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