Java8 对Map的转换封装
来源:互联网 发布:网络言情小说排行榜 编辑:程序博客网 时间:2024/05/29 08:44
1、Map 与 List 的转换
2、Map 的排序
3、参考文章:
http://www.mkyong.com/java8/java-8-convert-list-to-map/
http://www.mkyong.com/tutorials/java-8-tutorials/
@Test public void testToMapSort() { Map<String, Integer> unsortMap = new HashMap<>(); unsortMap.put("z", 10); unsortMap.put("b", 5); unsortMap.put("a", 6); unsortMap.put("c", 20); unsortMap.put("d", 1); unsortMap.put("e", 7); unsortMap.put("y", 8); unsortMap.put("n", 99); unsortMap.put("g", 50); unsortMap.put("m", 2); unsortMap.put("f", 9); System.out.println("Original..."); System.out.println(unsortMap); // sort by keys, a,b,c..., and return a new LinkedHashMap // toMap() will returns HashMap by default, we need LinkedHashMap to keep the order. Map<String, Integer> result = unsortMap.entrySet().stream() .sorted(Map.Entry.comparingByKey()) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new)); // Not Recommend, but it works. //Alternative way to sort a Map by keys, and put it into the "result" map Map<String, Integer> result2 = new LinkedHashMap<>(); unsortMap.entrySet().stream() .sorted(Map.Entry.comparingByKey()) .forEachOrdered(x -> result2.put(x.getKey(), x.getValue())); System.out.println("Sorted..."); System.out.println(result); System.out.println(result2); } @Test public void testToMapSort2() { Map<String, Integer> unsortMap = new HashMap<>(); unsortMap.put("z", 10); unsortMap.put("b", 5); unsortMap.put("a", 6); unsortMap.put("c", 20); unsortMap.put("d", 1); unsortMap.put("e", 7); unsortMap.put("y", 8); unsortMap.put("n", 99); unsortMap.put("g", 50); unsortMap.put("m", 2); unsortMap.put("f", 9); System.out.println("Original..."); System.out.println(unsortMap); //sort by values, and reserve it, 10,9,8,7,6... Map<String, Integer> result = unsortMap.entrySet().stream() .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new)); //Alternative way Map<String, Integer> result2 = new LinkedHashMap<>(); unsortMap.entrySet().stream() .sorted(Map.Entry.<String, Integer>comparingByValue().reversed()) .forEachOrdered(x -> result2.put(x.getKey(), x.getValue())); System.out.println("Sorted..."); System.out.println(result); System.out.println(result2); } @Test public void testMap2List() { Map<Integer, String> map = new HashMap<>(); map.put(10, "apple"); map.put(20, "orange"); map.put(30, "banana"); map.put(40, "watermelon"); map.put(50, "dragonfruit"); System.out.println("\n1. Export Map Key to List..."); List<Integer> result = new ArrayList(map.keySet()); result.forEach(System.out::println); System.out.println("\n2. Export Map Value to List..."); List<String> result2 = new ArrayList(map.values()); result2.forEach(System.out::println); } @Test public void testConvertMapToList() { Map<Integer, String> map = new HashMap<>(); map.put(10, "apple"); map.put(20, "orange"); map.put(30, "banana"); map.put(40, "watermelon"); map.put(50, "dragonfruit"); // split a map into 2 List List<Integer> resultSortedKey = new ArrayList<>(); List<String> resultValues = map.entrySet().stream() //sort a Map by key and stored in resultSortedKey .sorted(Map.Entry.<Integer, String>comparingByKey().reversed()) .peek(e -> resultSortedKey.add(e.getKey())) .map(x -> x.getValue()) // filter banana and return it to resultValues .filter(x -> !"banana".equalsIgnoreCase(x)) .collect(Collectors.toList()); resultSortedKey.forEach(System.out::println); resultValues.forEach(System.out::println); } //统计字符的出现次数 @Test public void testFrequency() { List<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); list.add("c"); list.add("d"); list.add("b"); list.add("c"); list.add("a"); list.add("a"); list.add("a"); System.out.println("\nExample 1 - Count 'a' with frequency"); System.out.println("a : " + Collections.frequency(list, "a")); System.out.println("\nExample 2 - Count all with frequency"); Set<String> uniqueSet = new HashSet<String>(list); System.out.println("==========>" + uniqueSet); for (String temp : uniqueSet) { System.out.println(temp + ": " + Collections.frequency(list, temp)); } }
阅读全文
0 0
- Java8 对Map的转换封装
- java8 的 map 排序
- java8使用lambda对Map排序
- java8之list和map集合转换
- Eclipse对java8的支持
- Eclipse 对 Java8 的支持
- java8对接口的改变
- Java8种基本数据类型的封装,Cache
- js封装的MAP
- java8中map的新方法--replace
- jAVA8的 filter 和map函数
- java8中map的新方法--replace
- java8中 map和flatmap的理解
- Java8新增的Map集合以及各Map的实现
- JavaBean和Map转换封装类
- JavaBean和Map转换封装类
- JavaBean和Map转换封装类
- java8中对ConcurrentHashMap的改进
- IntelliJ IDEA 2017激活
- 8.fireworks
- Java中的Enum的简单使用
- 剑指offer——二叉树中和为某一值的路径
- 不敢死队问题
- Java8 对Map的转换封装
- java dbutil 1.1.1 版本更新
- 深入理解面向对象析构函数回收机制
- SpringMVC确定目标方法POJO类型入参的过程
- 交互设计不只是解决问题
- 解决和说明> eth.getCompilers() Error: The method eth_getCompilers does not exist/is not available
- Linux服务器启动tomcat很慢解决方法
- 服务器架设
- 字符串调用split以点逗"."符号作为拆分符时返回的是空数组