Java 8 将Map转换为List

本文详细介绍如何在Java中将Map转换为List,包括使用传统方法和Java 8的流特性进行转换。示例代码展示了如何提取Map的键和值,并进行过滤操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将一个Java示例转换MapList

汇总:

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.映射到List

为了简单MapList转换,只需使用下面的代码:

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("\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);
    }
}

Output

1. Export Map Key to List...
50
20
40
10
30
2. Export Map Value to List...
dragonfruit
orange
watermelon
apple
banana

2.Java 8 - 映射到List

对于Java 8,您可以将其Map转换为流,将其处理并将其返回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("\n1. Export Map Key to List...");
        List<Integer> result = map.keySet().stream()
                .collect(Collectors.toList());
        result.forEach(System.out::println);
        System.out.println("\n2. Export Map Value to List...");
        List<String> result2 = map.values().stream()
                .collect(Collectors.toList());
        result2.forEach(System.out::println);
        System.out.println("\n3. 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 - 将Map转换成List

这个例子有点极端,map.entrySet()用来转换MapList

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

 

转载自:https://blog.youkuaiyun.com/hatsune_miku_/article/details/73435627

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值