When learning the usage of map collection in java, I found serveral beneficial methods that was encountered in the daily life. Now  I made a summary:

[java] view
plain
copy

  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Comparator;
  4. import java.util.HashMap;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.SortedMap;
  9. import java.util.TreeMap;
  10. public class MapUtil
  11. {
  12. private static final Map<String, String> contents = new HashMap<String, String>();
  13. @SuppressWarnings("unchecked")
  14. public static void initMap()
  15. {
  16. Map testMap = new HashMap<String, String>();
  17. testMap.put("Albert", "Shao");
  18. contents.putAll(testMap);
  19. }
  20. /**
  21. * Four methods to list map.
  22. * output:
  23. * Albert:Shao
  24. Albert:Shao
  25. Shao
  26. Albert:Shao
  27. * @time Jul 18, 2014 11:39:46 AM
  28. * @return void
  29. */
  30. public static void listMap()
  31. {
  32. Map<String, String> testMap = new HashMap<String, String>();
  33. testMap.put("Albert", "Shao");
  34. for (Map.Entry<String, String> entry : testMap.entrySet())
  35. {
  36. System.out.println(entry.getKey() + ":" + entry.getValue());
  37. }
  38. for (String key : testMap.keySet())
  39. {
  40. System.out.println(key + ":" + testMap.get(key));
  41. }
  42. for (String value : testMap.values())
  43. {
  44. System.out.println(value);
  45. }
  46. Iterator<Map.Entry<String, String>> keyIt = testMap.entrySet()
  47. .iterator();
  48. while (keyIt.hasNext())
  49. {
  50. Map.Entry<String, String> entry = keyIt.next();
  51. System.out.println(entry.getKey() + ":" + entry.getValue());
  52. }
  53. }
  54. /**
  55. * Use the treeMap order by key asc.
  56. * Watch out: if key is repeated, the latter element will replace the former.
  57. * output: {Apple=five, Banana=three, Grape=one, Pair=four}
  58. *
  59. * @time Jul 18, 2014 11:37:51 AM
  60. * @return void
  61. */
  62. public static void sort()
  63. {
  64. SortedMap<String, String> sortMap = new TreeMap<String, String>();
  65. sortMap.put("Pair", "four");
  66. sortMap.put("Apple", "two");
  67. sortMap.put("Grape", "one");
  68. sortMap.put("Banana", "three");
  69. sortMap.put("Apple", "five");
  70. System.out.println(sortMap);
  71. }
  72. /**
  73. * Sort the Map by map.value, then set the result to map.
  74. * output : [Apple=1, Pair=2, Banana=3, Grape=4]
  75. *
  76. * @time Jul 18, 2014 11:36:28 AM
  77. * @return void
  78. */
  79. public static void sortByValue()
  80. {
  81. Map<String, Integer> testMap = new HashMap<String, Integer>();
  82. testMap.put("Pair", 2);
  83. testMap.put("Apple", 1);
  84. testMap.put("Grape", 4);
  85. testMap.put("Banana", 3);
  86. List<Map.Entry<String, Integer>> entryList = new ArrayList<Map.Entry<String, Integer>>(
  87. testMap.entrySet());
  88. Collections.sort(entryList,
  89. new Comparator<Map.Entry<String, Integer>>()
  90. {
  91. public int compare(Map.Entry<String, Integer> c1,
  92. Map.Entry<String, Integer> c2)
  93. {
  94. return (c1.getValue() - c2.getValue());
  95. }
  96. });
  97. System.out.println(entryList);
  98. }
  99. /**
  100. *Sort map by value when value is object.
  101. * use compareTo method to replace simple '-'
  102. * output:[Apple=AB, Grape=AF, Pair=BB, Banana=XY]
  103. *
  104. * @time Jul 18, 2014 11:48:35 AM
  105. * @return void
  106. */
  107. public static void sortByObject()
  108. {
  109. Map<String, String> testMap = new HashMap<String, String>();
  110. testMap.put("Pair", "BB");
  111. testMap.put("Apple", "AB");
  112. testMap.put("Grape", "AF");
  113. testMap.put("Banana", "XY");
  114. List<Map.Entry<String, String>> entryList = new ArrayList<Map.Entry<String, String>>(
  115. testMap.entrySet());
  116. Collections.sort(entryList,
  117. new Comparator<Map.Entry<String, String>>()
  118. {
  119. public int compare(Map.Entry<String, String> c1,
  120. Map.Entry<String, String> c2)
  121. {
  122. return (c1.getValue().compareTo(c2.getValue()));
  123. }
  124. });
  125. System.out.println(entryList);
  126. }
  127. public static void main(String[] args)
  128. {
  129. MapUtil.listMap();
  130. MapUtil.sort();
  131. MapUtil.sortByValue();
  132. MapUtil.sortByObject();
  133. }
  134. }

If you want to further know about the usage of list methods, you could view my another blogs.

http://blog.csdn.net/sxb0841901116/article/details/20635267

【DateStructure】 Charnming usages of Map collection in Java的更多相关文章

  1. 如何用javac 和java 编译运行整个Java工程 (转载)【转】在Linux下编译与执行Java程序

    如何用javac 和java 编译运行整个Java工程 (转载)  http://blog.csdn.net/huagong_adu/article/details/6929817 [转]在Linux ...

  2. 【BZOJ1125】[POI2008]Poc hash+map+SBT

    [BZOJ1125][POI2008]Poc Description n列火车,每条有l节车厢.每节车厢有一种颜色(用小写字母表示).有m次车厢交换操作.求:对于每列火车,在交换车厢的某个时刻,与其颜 ...

  3. 【算法】哈希表的诞生(Java)

    参考资料 <算法(java)>                           — — Robert Sedgewick, Kevin Wayne <数据结构>       ...

  4. 【Unity】Unity中C#与Android中Java的互相调用遇到的一些问题

    1.有关调用的一些问题: (1).在C#中直接调用java中的代码,无返回值: 在java中: public static void setAge(Context context , int leve ...

  5. 【dom4j】解析xml为map

    dom4j解析xml文件 <?xml version="1.0" encoding="utf-8"?> <workflows> < ...

  6. 【python】lamda表达式,map

    一个很好的博客:http://blog.csdn.net/mathboylinlin/article/details/9413551 博客不让转载,我只摘抄了里面几个例子,更多内容到博客里去看 lam ...

  7. 【iOS】苹果,百度Map定位使用与总结

    iOS中使用较多的3款地图,google地图.百度地图.苹果自带地图(高德).当中苹果自带地图在中国使用的是高德的数据.苹果在iOS 6之后放弃了使用谷歌地图,而改用自家的地图.在国内使用的较多的就是 ...

  8. 【原创】【Android】揭秘 ART 细节 ---- Garbage collection

      背景 Dalvik :http://zh.wikipedia.org/wiki/Dalvik%E8%99%9A%E6%8B%9F%E6%9C%BA ART :http://source.andro ...

  9. 【384】reduce归纳、map映射、filter筛选 的用法

    参考:4. Map, Filter and Reduce — Python Tips 0.1 documentation 参考:Python的functools.reduce用法 Map:映射,对于列 ...

随机推荐

  1. 处理json中的异常字符

    在很多场景中需要通过json传递数据,如果json中包含英文的",""'"之类的字符,会导致json解析失败 可以用一些在线的json格式检查网站检查是否含有异 ...

  2. jsp九九乘法表

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...

  3. 如何交换a和b两个整数的值,不用额外空间

    这个题貌似完全颠覆一般的Logic:交换两个整数需要一个额外的空间用于保存: t = b; b = a; a  = t; 粗看上去似乎没有办法,但是仔细想一下,既然不能用额外的空间,那么能用的方法就只 ...

  4. php-GD库的函数(一)

    <?php //getimagesize - 取得图片的大小[即长与宽] //print_r(getimagesize("./logo_i.gif")); //Array ( ...

  5. AndroidUI 布局动画-布局内容改变动画

    实现一个点击菜单动画添加按钮,点击按钮移除当前按钮的动画效果: <LinearLayout xmlns:android="http://schemas.android.com/apk/ ...

  6. (转)iOS开发ARC内存管理技术要点

    转自:http://www.cnblogs.com/flyFreeZn/p/4264220.html 本文来源于我个人的ARC学习笔记,旨在通过简明扼要的方式总结出iOS开发中ARC(Automati ...

  7. java 泛型深入之Set有用工具 各种集合泛型深入使用演示样例,匿名内部类、内部类应用于泛型探讨

    java 泛型深入之Set有用工具 各种集合泛型深入使用演示样例,匿名内部类.内部类应用于泛型探讨 //Sets.java package org.rui.generics.set; import j ...

  8. C# linq to xml

    XDocument doc = new XDocument( new XDeclaration("1.0", "utf-8", "yes") ...

  9. exit函数的妙用

    写了一个程序,用来推断一个文件是否存在: #include<stdio.h> main() {  FILE *fp;  fp = fopen ("/home/wang/my/ct ...

  10. wcf系列学习5天速成——第三天 事务的使用

    今天是速成的第三天,再分享一下WCF中比较常用的一种技术,也就是”事务“. 在B2B的项目中,一般用户注册后,就有一个属于自己的店铺,此时,我们就要插入两张表, User和Shop表. 当然,要么插入 ...