stream操作集合

package test002;

/**
 * @author LSG
 * @date 2022-11-02 23:45
 */
public class User {

    Long stuId;
    String stuName;
    Integer stuAge;

    public Long getStuId() {
        return stuId;
    }

    public void setStuId(Long stuId) {
        this.stuId = stuId;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public Integer getStuAge() {
        return stuAge;
    }

    public void setStuAge(Integer stuAge) {
        this.stuAge = stuAge;
    }

    public User() {
    }

    public User(Long stuId, String stuName, Integer stuAge) {
        this.stuId = stuId;
        this.stuName = stuName;
        this.stuAge = stuAge;
    }

    @Override
    public String toString() {
        return "User{" +
                "stuId=" + stuId +
                ", stuName='" + stuName + '\'' +
                ", stuAge=" + stuAge +
                '}';
    }
}
package test002;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 旧集合中没有,新集合中有,需要在旧集合新增
 * 旧集合中有,新集合中没有,需要在旧集合删除
 * 旧集合中有,新集合中有,需要在旧集合更新
 * @author LSG
 * @date 2022-11-02 23:47
 */
public class Test {
    public static void main(String[] args) {
        List<User> newUsers = new ArrayList<>(4);
        User user1 = new User(10L,"aaa",10);
        User user2 = new User(11L,"bbb",10);
        User user3 = new User(12L,"eee",10);
        newUsers.add(user1);
        newUsers.add(user2);
        newUsers.add(user3);
        List<User> oldUsers = new ArrayList<>(4);
        User user4 = new User(13L,"aaa",10);
        User user5 = new User(14L,"eee",10);
        User user6 = new User(14L,"ccc",10);
        oldUsers.add(user4);
        oldUsers.add(user5);
        oldUsers.add(user6);
        List<User> insertMaterials = newUsers.stream()
                .filter(a-> oldUsers.stream()
                        .map(User::getStuName)
                        .noneMatch(stuName-> Objects.equals(a.getStuName(),stuName)))
                .collect(Collectors.toList());
        System.out.println(insertMaterials);

        List<User> updateMaterials = newUsers.stream()
                .filter(a-> oldUsers.stream()
                        .map(User::getStuName)
                        .anyMatch(stuName-> Objects.equals(a.getStuName(),stuName)))
                .collect(Collectors.toList());
        System.out.println(updateMaterials);

        List<User> deleteMaterials = oldUsers.stream()
                .filter(a-> newUsers.stream()
                        .map(User::getStuName)
                        .noneMatch(stuName-> Objects.equals(a.getStuName(),stuName)))
                .collect(Collectors.toList());
        System.out.println(deleteMaterials);
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
StreamJava 8引入的一种处理数据集合的API,它可以让我们更加方便、高效地操作集合。以下是使用Stream操作集合的常用方式: 1. 遍历集合:使用forEach()方法遍历集合中的每个元素,然后对每个元素执行指定的操作。 2. 过滤集合:使用filter()方法过滤集合中的元素,然后返回满足条件的元素组成的新集合。 3. 映射集合:使用map()方法对集合中的每个元素进行映射,然后返回由映射结果组成的新集合。 4. 排序集合:使用sorted()方法对集合中的元素进行排序,然后返回排序后的新集合。 5. 统计集合:使用count()、sum()、max()、min()等方法对集合中的元素进行统计。 6. 匹配集合:使用anyMatch()、allMatch()、noneMatch()等方法对集合中的元素进行匹配。 以下是使用Stream操作集合的代码示例: 1. 遍历集合: ```java List<String> list = Arrays.asList("Java", "Python", "C++", "Ruby"); list.stream().forEach(item -> System.out.println(item)); ``` 输出结果: ``` Java Python C++ Ruby ``` 2. 过滤集合: ```java List<String> list = Arrays.asList("Java", "Python", "C++", "Ruby"); List<String> filteredList = list.stream().filter(item -> item.startsWith("J")).collect(Collectors.toList()); System.out.println(filteredList); ``` 输出结果: ``` [Java] ``` 3. 映射集合: ```java List<String> list = Arrays.asList("Java", "Python", "C++", "Ruby"); List<Integer> lengthList = list.stream().map(item -> item.length()).collect(Collectors.toList()); System.out.println(lengthList); ``` 输出结果: ``` [4, 6, 3, 4] ``` 4. 排序集合: ```java List<String> list = Arrays.asList("Java", "Python", "C++", "Ruby"); List<String> sortedList = list.stream().sorted().collect(Collectors.toList()); System.out.println(sortedList); ``` 输出结果: ``` [C++, Java, Python, Ruby] ``` 5. 统计集合: ```java List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); long count = list.stream().count(); int sum = list.stream().mapToInt(item -> item).sum(); int max = list.stream().mapToInt(item -> item).max().getAsInt(); int min = list.stream().mapToInt(item -> item).min().getAsInt(); System.out.println("count: " + count); System.out.println("sum: " + sum); System.out.println("max: " + max); System.out.println("min: " + min); ``` 输出结果: ``` count: 5 sum: 15 max: 5 min: 1 ``` 6. 匹配集合: ```java List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); boolean anyMatch = list.stream().anyMatch(item -> item > 3); boolean allMatch = list.stream().allMatch(item -> item > 0); boolean noneMatch = list.stream().noneMatch(item -> item > 5); System.out.println("anyMatch: " + anyMatch); System.out.println("allMatch: " + allMatch); System.out.println("noneMatch: " + noneMatch); ``` 输出结果: ``` anyMatch: true allMatch: true noneMatch: true ``` 以上就是使用Stream操作集合的方式,希望能对您有所帮助。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值