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("=====第一种Map的Key遍历,遍历Key和Value=====");//第一种Map的Key遍历,遍历Key和Valuefor (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遍历key和valueSystem.out.println("=====第三种通过Map.entrySet使用iterator遍历key和value=====");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());}
//第四种获取Map的Key或者ValueSystem.out.println("=====第四种获取Map的Key=====");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 老版本的唯一指定遍历方式,第二种是他的升级,也就是说他们的效率相差不多。但是呢现在大家都习惯于用第二种方式啦。
至于具体的问题,具体分析,具体决策啦。。
- Guava(四):集合基础总结之Map
- Guava(四):集合基础总结之List
- Java基础---集合框架四(Map)
- JAVA基础---集合(四)--Map
- guava (四)--集合中的FluentIterable类
- Java基础篇(四——2)——集合框架之Map接口
- java基础之集合(Set,Map,List)总结
- Guava学习之Map
- java基础集合之Map
- java基础-- 集合框架 之 Map集合
- Java集合框架(四)之Map详解
- 读JSE源码(四)集合之Map接口
- 集合详解(四):Map
- 黑马程序员《java基础总结(九)》(Map集合)
- 黑马程序员——JAVA基础------集合框架(四)----Map集合
- java中的集合之Map接口(四)
- JAVA常用集合框架用法详解基础篇四之Map接口
- 黑马程序员:Java基础总结----Map集合
- 局部线性回归
- C/C++中的一些区别
- 自定义ViewGroup
- 顺序表的基本运算2
- NYIST OJ 1000 又见Fibonacci数列
- Guava(四):集合基础总结之Map
- Struts2:命名空间的使用
- Round A APAC Test 2016: Problem B. gCube
- 递归和非递归实现链表反转
- 点评系统 Modoer v3.6 旗舰版手机WEB端加微信公众平台接口
- 有关博客的一些断想
- MySQL中查看自带的SQL模式
- java学习之异常
- Java接口(interface)