java中Stream的使用以及List转set和map方法

本文介绍了Java集合框架中Stream API的使用,通过实例展示了如何将List转换为Set和Map,利用Stream的filter、collect等方法实现数据过滤与收集。Stream API简化了集合操作,提高了代码的简洁性和效率。

Stream流的作用:
非常精简方便的去遍历集合实现过滤,排序等。
图解:
在这里插入图片描述
在这里插入图片描述
例:

ArrayList<User> users = new ArrayList<>();
        User user1 = new User("赵六",33);
        users.add(new User("张三",12));
        users.add(new User("李四",14));
        users.add(new User("王五",17));
        users.add(user1);

List转set:

 Stream<User> stream =users.stream();
        //转换为set集合
        Set<User> setUser = stream.collect(Collectors.toSet());
        setUser.forEach(m->System.out.println(m));

List转map:

//转map
        Stream<User> stream =users.stream();
        Map<String,User> maps = stream.collect(Collectors.toMap(User ->User.getName(),User ->User));
        maps.forEach((a,b) ->System.out.println(a+":"+b));
Java 8 中,可使用 Stream 流结合 `Collectors.toMap` 方法将 `List` 对象换为 `Map`。以下是几种常见的实现方式: ### key value 都是对象中的某个属性值 将 `List` 中对象的某个属性作为 `Map` 的键,另一个属性作为 `Map` 的值。示例代码如下: ```java import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; class User { private String userId; private String userName; public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } } public class ListToMapExample { public static void main(String[] args) { List<User> userList = new ArrayList<>(); User user1 = new User(); user1.setUserId("1"); user1.setUserName("张三"); userList.add(user1); User user2 = new User(); user2.setUserId("2"); user2.setUserName("李四"); userList.add(user2); Map<String, String> userMap = userList.stream().collect(Collectors.toMap(User::getUserId, User::getUserName)); System.out.println(userMap); } } ``` 上述代码中,通过 `Collectors.toMap(User::getUserId, User::getUserName)` 将 `List<User>` 换为 `Map<String, String>`,其中键是用户的 `userId`,值是用户的 `userName` [^1]。 ### 收集成实体本身的 map对象的某个属性作为 `Map` 的键,对象本身作为 `Map` 的值。示例代码如下: ```java import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; class Account { private Long id; private String username; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } } public class ListToEntityMapExample { public static Map<Long, Account> getIdAccountMap(List<Account> accounts) { return accounts.stream().collect(Collectors.toMap(Account::getId, account -> account)); } public static void main(String[] args) { List<Account> accountList = new ArrayList<>(); Account account1 = new Account(); account1.setId(1L); account1.setUsername("user1"); accountList.add(account1); Account account2 = new Account(); account2.setId(2L); account2.setUsername("user2"); accountList.add(account2); Map<Long, Account> accountMap = getIdAccountMap(accountList); System.out.println(accountMap); } } ``` 此代码中,使用 `Collectors.toMap(Account::getId, account -> account)` 将 `List<Account>` 换为 `Map<Long, Account>`,键是账户的 `id`,值是账户对象本身 [^3]。 ### 处理键冲突的情况 当 `List` 中的对象存在重复的键时,可提供一个合并函数来处理冲突。示例代码如下: ```java import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; class Item { private String key; private int value; public String getKey() { return key; } public void setKey(String key) { this.key = key; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } } public class ListToMapWithMergeFunction { public static void main(String[] args) { List<Item> itemList = new ArrayList<>(); Item item1 = new Item(); item1.setKey("A"); item1.setValue(1); itemList.add(item1); Item item2 = new Item(); item2.setKey("A"); item2.setValue(2); itemList.add(item2); Map<String, Integer> itemMap = itemList.stream().collect(Collectors.toMap(Item::getKey, Item::getValue, (existing, replacement) -> existing + replacement)); System.out.println(itemMap); } } ``` 上述代码中,使用 `Collectors.toMap(Item::getKey, Item::getValue, (existing, replacement) -> existing + replacement)` 处理键冲突,当出现重复键时,将对应的值相加。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值