【DateStructure】 Charnming usages of Map collection in Java
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:
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import java.util.SortedMap;
- import java.util.TreeMap;
- public class MapUtil
- {
- private static final Map<String, String> contents = new HashMap<String, String>();
- @SuppressWarnings("unchecked")
- public static void initMap()
- {
- Map testMap = new HashMap<String, String>();
- testMap.put("Albert", "Shao");
- contents.putAll(testMap);
- }
- /**
- * Four methods to list map.
- * output:
- * Albert:Shao
- Albert:Shao
- Shao
- Albert:Shao
- * @time Jul 18, 2014 11:39:46 AM
- * @return void
- */
- public static void listMap()
- {
- Map<String, String> testMap = new HashMap<String, String>();
- testMap.put("Albert", "Shao");
- for (Map.Entry<String, String> entry : testMap.entrySet())
- {
- System.out.println(entry.getKey() + ":" + entry.getValue());
- }
- for (String key : testMap.keySet())
- {
- System.out.println(key + ":" + testMap.get(key));
- }
- for (String value : testMap.values())
- {
- System.out.println(value);
- }
- Iterator<Map.Entry<String, String>> keyIt = testMap.entrySet()
- .iterator();
- while (keyIt.hasNext())
- {
- Map.Entry<String, String> entry = keyIt.next();
- System.out.println(entry.getKey() + ":" + entry.getValue());
- }
- }
- /**
- * Use the treeMap order by key asc.
- * Watch out: if key is repeated, the latter element will replace the former.
- * output: {Apple=five, Banana=three, Grape=one, Pair=four}
- *
- * @time Jul 18, 2014 11:37:51 AM
- * @return void
- */
- public static void sort()
- {
- SortedMap<String, String> sortMap = new TreeMap<String, String>();
- sortMap.put("Pair", "four");
- sortMap.put("Apple", "two");
- sortMap.put("Grape", "one");
- sortMap.put("Banana", "three");
- sortMap.put("Apple", "five");
- System.out.println(sortMap);
- }
- /**
- * Sort the Map by map.value, then set the result to map.
- * output : [Apple=1, Pair=2, Banana=3, Grape=4]
- *
- * @time Jul 18, 2014 11:36:28 AM
- * @return void
- */
- public static void sortByValue()
- {
- Map<String, Integer> testMap = new HashMap<String, Integer>();
- testMap.put("Pair", 2);
- testMap.put("Apple", 1);
- testMap.put("Grape", 4);
- testMap.put("Banana", 3);
- List<Map.Entry<String, Integer>> entryList = new ArrayList<Map.Entry<String, Integer>>(
- testMap.entrySet());
- Collections.sort(entryList,
- new Comparator<Map.Entry<String, Integer>>()
- {
- public int compare(Map.Entry<String, Integer> c1,
- Map.Entry<String, Integer> c2)
- {
- return (c1.getValue() - c2.getValue());
- }
- });
- System.out.println(entryList);
- }
- /**
- *Sort map by value when value is object.
- * use compareTo method to replace simple '-'
- * output:[Apple=AB, Grape=AF, Pair=BB, Banana=XY]
- *
- * @time Jul 18, 2014 11:48:35 AM
- * @return void
- */
- public static void sortByObject()
- {
- Map<String, String> testMap = new HashMap<String, String>();
- testMap.put("Pair", "BB");
- testMap.put("Apple", "AB");
- testMap.put("Grape", "AF");
- testMap.put("Banana", "XY");
- List<Map.Entry<String, String>> entryList = new ArrayList<Map.Entry<String, String>>(
- testMap.entrySet());
- Collections.sort(entryList,
- new Comparator<Map.Entry<String, String>>()
- {
- public int compare(Map.Entry<String, String> c1,
- Map.Entry<String, String> c2)
- {
- return (c1.getValue().compareTo(c2.getValue()));
- }
- });
- System.out.println(entryList);
- }
- public static void main(String[] args)
- {
- MapUtil.listMap();
- MapUtil.sort();
- MapUtil.sortByValue();
- MapUtil.sortByObject();
- }
- }
If you want to further know about the usage of list methods, you could view my another blogs.
【DateStructure】 Charnming usages of Map collection in Java的更多相关文章
- 如何用javac 和java 编译运行整个Java工程 (转载)【转】在Linux下编译与执行Java程序
如何用javac 和java 编译运行整个Java工程 (转载) http://blog.csdn.net/huagong_adu/article/details/6929817 [转]在Linux ...
- 【BZOJ1125】[POI2008]Poc hash+map+SBT
[BZOJ1125][POI2008]Poc Description n列火车,每条有l节车厢.每节车厢有一种颜色(用小写字母表示).有m次车厢交换操作.求:对于每列火车,在交换车厢的某个时刻,与其颜 ...
- 【算法】哈希表的诞生(Java)
参考资料 <算法(java)> — — Robert Sedgewick, Kevin Wayne <数据结构> ...
- 【Unity】Unity中C#与Android中Java的互相调用遇到的一些问题
1.有关调用的一些问题: (1).在C#中直接调用java中的代码,无返回值: 在java中: public static void setAge(Context context , int leve ...
- 【dom4j】解析xml为map
dom4j解析xml文件 <?xml version="1.0" encoding="utf-8"?> <workflows> < ...
- 【python】lamda表达式,map
一个很好的博客:http://blog.csdn.net/mathboylinlin/article/details/9413551 博客不让转载,我只摘抄了里面几个例子,更多内容到博客里去看 lam ...
- 【iOS】苹果,百度Map定位使用与总结
iOS中使用较多的3款地图,google地图.百度地图.苹果自带地图(高德).当中苹果自带地图在中国使用的是高德的数据.苹果在iOS 6之后放弃了使用谷歌地图,而改用自家的地图.在国内使用的较多的就是 ...
- 【原创】【Android】揭秘 ART 细节 ---- Garbage collection
背景 Dalvik :http://zh.wikipedia.org/wiki/Dalvik%E8%99%9A%E6%8B%9F%E6%9C%BA ART :http://source.andro ...
- 【384】reduce归纳、map映射、filter筛选 的用法
参考:4. Map, Filter and Reduce — Python Tips 0.1 documentation 参考:Python的functools.reduce用法 Map:映射,对于列 ...
随机推荐
- 大数据为什么要选择Spark
大数据为什么要选择Spark Spark是一个基于内存计算的开源集群计算系统,目的是更快速的进行数据分析. Spark由加州伯克利大学AMP实验室Matei为主的小团队使用Scala开发开发,其核心部 ...
- MySQL优化GROUP BY-松散索引扫描与紧凑索引扫描
满足GROUP BY子句的最一般的方法是扫描整个表并创建一个新的临时表,表中每个组的所有行应为连续的,然后使用该临时表来找到组并应用累积函数(如果有).在某些情况中,MySQL能够做得更好,即通过索引 ...
- Python字符串与数字拼接
Python不像JS或者PHP这种弱类型语言里在字符串连接时会自动转换类型,而是直接报错.要解决这个方法只有提前把int转成string,然后再拼接字符串即可. 如代码: 1 2 3 4 5 # co ...
- dog-fooding-our-api-authentication
Dog-fooding our API - Authentication http://blog.mirajavora.com/authenticate-web-api-using-access-to ...
- BNUOJ27873:A Special "Happy Birthday" Song!!!
There are n people (excluding myself) in my 30th birthday party. They sing the traditional "hap ...
- poj 1064 Cable master ,二分 精度!!!
给出n根绳子,求把它们分割成K条等长的绳子的最大长度是多少? 二分 用 for(int i=0; i<100; ++i) 取代 while(r-l>eps) 循环100次精度能达到1e ...
- AngularJs(三) deployd 服务的使用
使用服务建立数据 在AngularJS(二)中,我搭建好了deployd服务,现在启动服务,创建正在的数据(开始是使用模拟数据),使用cmd命令 一.开启Mongodb数据. 贴图: 二:测试是否正常 ...
- CentOS 7解决Local Time与实际时间相差8小时问题
通过date -s “2014-12-06 15:00:00”以及timedatectl set-time “2014-12-06 15:00:00” ,以及ntp等方式均知识临时有效,苦恼了我半天. ...
- java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=Aict/listPagedAict.action
原因:请求的URL地址不完整,没有找到host. 排查解决:发现HTTP请求的URL少加了项目名,导致URL地址不完整.
- LINUX下查看CPU使用率的命令[Z]
1.top 使用权限:所有使用者 使用方式:top [-] [d delay] [q] [c] [S] [s] [i] [n] [b] 说明:即时显示process的动态 d :改变显示的更新速度,或 ...
