zoukankan      html  css  js  c++  java
  • Java 8 – Convert Map to LIST

    Java 8 – Convert Map to LIST

    Few Java examples to convert a Map to a List

    Map<String, String> map = new HashMap<>();

    // Convert all Map keys to a List
    List<String> result = new ArrayList(map.keySet());

    // Convert all Map values to a List
    List<String> result2 = new ArrayList(map.values());

    // Java 8, Convert all Map keys to a List
    List<String> result3 = map.keySet().stream()
    .collect(Collectors.toList());

    // Java 8, Convert all Map values to a List
    List<String> result4 = map.values().stream()
    .collect(Collectors.toList());

    // Java 8, seem a bit long, but you can enjoy the Stream features like filter and etc.
    List<String> result5 = map.values().stream()
    .filter(x -> !"apple".equalsIgnoreCase(x))
    .collect(Collectors.toList());

    // Java 8, split a map into 2 List, it works!
    // refer example 3 below

    1. Map To List
    For a simple Map to List conversion, just uses the below code :

    ConvertMapToList.java
    package com.mkyong;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    public class ConvertMapToList {

    public static void main(String[] args) {

    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(" 1. Export Map Key to List...");

    List<Integer> result = new ArrayList(map.keySet());

    result.forEach(System.out::println);

    System.out.println(" 2. Export Map Value to List...");

    List<String> result2 = new ArrayList(map.values());

    result2.forEach(System.out::println);

    }

    }

    Output

    1. Export Map Key to List...
    50
    20
    40
    10
    30

    2. Export Map Value to List...
    dragonfruit
    orange
    watermelon
    apple
    banana

    Java 8 – Map To List
    For Java 8, you can convert the Map into a stream, process it and returns it back as a List

    ConvertMapToList.java
    package com.mkyong;

    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.stream.Collectors;

    public class ConvertMapToList {

    public static void main(String[] args) {

    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(" 1. Export Map Key to List...");

    List<Integer> result = map.keySet().stream()
    .collect(Collectors.toList());

    result.forEach(System.out::println);

    System.out.println(" 2. Export Map Value to List...");

    List<String> result2 = map.values().stream()
    .collect(Collectors.toList());

    result2.forEach(System.out::println);

    System.out.println(" 3. Export Map Value to List..., say no to banana");
    List<String> result3 = map.keySet().stream()
    .filter(x -> !"banana".equalsIgnoreCase(x))
    .collect(Collectors.toList());

    result3.forEach(System.out::println);

    }

    }

    Output

    1. Export Map Key to List...
    50
    20
    40
    10
    30

    2. Export Map Value to List...
    dragonfruit
    orange
    watermelon
    apple
    banana

    3. Export Map Value to List..., say no to banana
    dragonfruit
    orange
    watermelon
    apple

    3. Java 8 – Convert Map into 2 List
    This example is a bit extreme, uses map.entrySet() to convert a Map into 2 List

    ConvertMapToList.java
    package com.mkyong;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.stream.Collectors;

    //https://www.mkyong.com/java8/java-8-how-to-sort-a-map/
    public class ConvertMapToList {

    public static void main(String[] args) {

    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);

    }

    }

    Output

    //resultSortedKey
    50
    40
    30
    20
    10

    //resultValues
    dragonfruit
    watermelon
    orange
    apple

    http://www.mkyong.com/java8/java-8-convert-map-to-list/

  • 相关阅读:
    因为这几个TypeScript代码的坏习惯,同事被罚了500块
    如何设计好分布式数据库,这个策略很重要
    线程、多线程和线程池,看完这些你就能全部搞懂了
    章方:征服耶鲁教授的算法大神程序媛
    从零开始学python | 使用Python映射,过滤和缩减函数:所有您需要知道的
    c# 优化代码的一些规则——用委托表示回调[五]
    mysql 重新整理——索引优化explain字段介绍一 [九]
    mysql 重新整理——索引优化explain简单介绍 [八]
    mysql 重新整理——索引简介[七]
    mysql 重新整理——七种连接join连接[六]
  • 原文地址:https://www.cnblogs.com/shy1766IT/p/10024987.html
Copyright © 2011-2022 走看看