Retrieve all values from HashMap keys in an ArrayList Java

前端 未结 9 1380
野性不改
野性不改 2021-01-31 14:21

Good day, this is kind of confusing me now(brain freeze!) and seem to be missing something. Have an ArrayList which i populate with a HashMap. now i put in my HashMap and arrayl

9条回答
  •  既然无缘
    2021-01-31 15:11

    Java 8 solution for produce string like "key1: value1,key2: value2"

    private static String hashMapToString(HashMap hashMap) {
    
        return hashMap.keySet().stream()
                .map((key) -> key + ": " + hashMap.get(key))
                .collect(Collectors.joining(","));
    
    }
    

    and produce a list simple collect as list

    private static List hashMapToList(HashMap hashMap) {
    
        return hashMap.keySet().stream()
                .map((key) -> key + ": " + hashMap.get(key))
                .collect(Collectors.toList());
    
    }
    

提交回复
热议问题