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

灰太狼 2024-02-17 16:15 12阅读 0赞

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

首先说一下这几个方法:

1:创建Map方法:

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

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

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

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

  1. entriesOnlyOnLeft()

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

  1. entriesOnlyOnRight()

好了上代码:

  1. import com.google.common.collect.ImmutableMap;
  2. import com.google.common.collect.MapDifference;
  3. import com.google.common.collect.Maps;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. /**
  7. * Created by luyangli on 15-9-19.
  8. */
  9. public class MapsTest {
  10. public static void main(String[] args) {
  11. Map<String,String> map = new HashMap<String, String>();
  12. map.put("3","c");
  13. map.put("1","a");
  14. map.put("2","b");
  15. map.put("4","t");
  16. System.out.println("========原生Map=======");
  17. for (Map.Entry<String, String> entry : map.entrySet()) {
  18. System.out.println(entry.getKey() + ":" + entry.getValue());
  19. }
  20. Map<String,String> guavaMap = Maps.newHashMap();
  21. guavaMap.put("3","c");
  22. guavaMap.put("1","a");
  23. guavaMap.put("2","b");
  24. guavaMap.put("5","t");
  25. System.out.println("========Guava Map=======");
  26. for (Map.Entry<String, String> entry : guavaMap.entrySet()) {
  27. System.out.println(entry.getKey() + ":" + entry.getValue());
  28. }
  29. MapDifference<String,String> diffMap = Maps.difference(map,guavaMap);
  30. System.out.println("========Guava diff Map=======");
  31. for (Map.Entry<String, String> entry : diffMap.entriesInCommon().entrySet()) {
  32. System.out.println(entry.getKey() + ":" + entry.getValue());
  33. }
  34. System.out.println("========Guava diff Left Map=======");
  35. for (Map.Entry<String, String> entry : diffMap.entriesOnlyOnLeft().entrySet()) {
  36. System.out.println(entry.getKey() + ":" + entry.getValue());
  37. }
  38. System.out.println("========Guava diff Right Map=======");
  39. for (Map.Entry<String, String> entry : diffMap.entriesOnlyOnRight().entrySet()) {
  40. System.out.println(entry.getKey() + ":" + entry.getValue());
  41. }
  42. // Map<String, Integer> left = ImmutableMap.of("a", 1, "b", 2, "c", 3);
  43. // Map<String, Integer> right = ImmutableMap.of("a", 1, "b", 2, "c", 3);
  44. // MapDifference<String, Integer> diff = Maps.difference(left, right);
  45. // Map<String,Integer> map2 = diff.entriesInCommon();
  46. // System.out.println("========Guava diff Map=======");
  47. // for (Map.Entry<String, Integer> entry : map2.entrySet()) {
  48. // System.out.println(entry.getKey() + ":" + entry.getValue());
  49. // }
  50. }
  51. }

我们来看一下效果:

  1. ========原生Map=======
  2. 3:c
  3. 2:b
  4. 1:a
  5. 4:t
  6. ========Guava Map=======
  7. 3:c
  8. 2:b
  9. 1:a
  10. 5:t
  11. ========Guava diff Map=======
  12. 3:c
  13. 2:b
  14. 1:a
  15. ========Guava diff Left Map=======
  16. 4:t
  17. ========Guava diff Right Map=======
  18. 5:t
  19. ========Guava diff Map=======
  20. b:2
  21. c:3
  22. a:1

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

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

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

  1. System.out.println("=====第一种Map的Key遍历,遍历Key和Value=====");
  2. //第一种Map的Key遍历,遍历Key和Value
  3. for (String key : map.keySet()) {
  4. System.out.println("Key : " + key + " and value : " + map.get(key));
  5. }
  6. //第二种使用entries进行遍历
  7. System.out.println("=====第二种使用entries进行遍历=====");
  8. for (Map.Entry<String, String> entry : map.entrySet()) {
  9. System.out.println("Key : " + entry.getKey() + " and value : " + entry.getValue());
  10. }
  11. //第三种通过Map.entrySet使用iterator遍历key和value
  12. System.out.println("=====第三种通过Map.entrySet使用iterator遍历key和value=====");
  13. Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
  14. while (it.hasNext()){
  15. Map.Entry<String,String> entry = it.next();
  16. System.out.println("Key : " + entry.getKey() + " and value : " + entry.getValue());
  17. }
  18. //第四种获取Map的Key或者Value
  19. System.out.println("=====第四种获取Map的Key=====");
  20. for (String key : guavaMap.keySet()) {
  21. System.out.println("Key : " + key);
  22. }
  23. System.out.println("=====第四种获取MapValue=====");
  24. for (String value : guavaMap.values()) {
  25. System.out.println("value : " + value);
  26. }

看一下结果:

=====第一种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 条评论,12人围观)

还没有评论,来说两句吧...

相关阅读

    相关 java基础集合Map

    ps:案例来源于毕向东老师Java基础教程 Map简介:该集合存储键值对。一对一对往里存。而且要保证键的唯一性。 知识点一:Map集合只是框架 Map |-

    相关 Java集合Map

    一、Map接口 1.1 Map概述 Map 的字面翻译是映射(地图就是一种映射)。将键映射到值的对象,一个映射不能包含重复的键 (如果有添加有重复

    相关 Map集合 总结

    (本人第一次写博客,部分内容有参照李刚老师的疯狂java系列图书,如有遗漏错误,请多指教,谢谢。) Java的集合类可分为Set、List、Map、Queue,其中Set、L