stream操作list过滤其中的数据并转换为新的list

代码如下:

package com.example.demo.test;


import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

public class StreamTest9 {

    public static void main(String[] args) {

        List<ProcessNode> processNodeList=getProcessNodeList();

        /**
         * 过滤某些节点并转换为新的list返回
         */
        List<Node> nodes = processNodeList.stream().filter(p->!p.getCode().equals("1002")).
                filter(p -> !p.getCode().equals("1003")).map(p -> {
            Node node = new Node();
            node.setNodeCode(p.getCode());
            node.setNodeName(p.getName());
            node.setNodeStatus(p.getStatus());
            return node;
        }).collect(Collectors.toList());

        System.out.println(JSONObject.toJSONString(nodes));

    }

    public static List<ProcessNode> getProcessNodeList(){

        List<ProcessNode> processNodeList = new ArrayList<ProcessNode>();

        ProcessNode processNode1=new ProcessNode("1","1001","请假条提交","PASS","通过",new Date());
        ProcessNode processNode2=new ProcessNode("2","1002","班委审批","PASS","通过",new Date());
        ProcessNode processNode3=new ProcessNode("3","1003","班主任审批","PASS","通过",new Date());
        ProcessNode processNode4=new ProcessNode("4","1004","年级主任审批","PASS","通过",new Date());
        ProcessNode processNode5=new ProcessNode("5","1005","校长审批","PASS","通过",new Date());

        processNodeList.add(processNode1);
        processNodeList.add(processNode2);
        processNodeList.add(processNode3);
        processNodeList.add(processNode4);
        processNodeList.add(processNode5);

        return processNodeList;
    }

}

/**
 * 申请节点详情
 */
class ProcessNode{

    /**
     * 流程ID
     */
    private String id;

    /**
     * 节点编码
     */
    private String code;

    /**
     * 节点名称
     */
    private String name;

    /**
     * 审批状态
     */
    private String status;
    /**
     * 审批结果
     */
    private String result;
    /**
     * 创建日期
     */
    private Date create;

    public ProcessNode() {
    }

    public ProcessNode(String id, String code, String name, String status, String result, Date create) {
        this.id = id;
        this.code = code;
        this.name = name;
        this.status = status;
        this.result = result;
        this.create = create;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public Date getCreate() {
        return create;
    }

    public void setCreate(Date create) {
        this.create = create;
    }
}

class Node{

    private String nodeCode;

    private String nodeName;

    private String nodeStatus;

    public Node() {
    }

    public Node(String nodeCode, String nodeName, String nodeStatus) {
        this.nodeCode = nodeCode;
        this.nodeName = nodeName;
        this.nodeStatus = nodeStatus;
    }

    public String getNodeCode() {
        return nodeCode;
    }

    public void setNodeCode(String nodeCode) {
        this.nodeCode = nodeCode;
    }

    public String getNodeName() {
        return nodeName;
    }

    public void setNodeName(String nodeName) {
        this.nodeName = nodeName;
    }

    public String getNodeStatus() {
        return nodeStatus;
    }

    public void setNodeStatus(String nodeStatus) {
        this.nodeStatus = nodeStatus;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java 开发者 配置jdk1.8 一下是jdk1.8的特性Lamdbd表达式 forEach循环遍历 List<String> names = new ArrayList(); names.forEach(String x -> { System.out.println(x); System.out.println(x); }); Map<String,String> map = new HashMap(); map.forEach((x,y) -> System.out.println(x+":"+y)); 集合里放对象 List<Book> books; books.forEach(x -> System.out.println(x.getBookName())); List<Map> books = new ArrayList(); Map map = new HashMap(); map.put("name","aaa"); map.put("price",33); books.forEach(x -> x.forEach(y,z) -> System.out.println(y+":"+z)); 方法引用 names.forEach(System.out::println); 等同于 names.forEach(x->System.out.println(x)) removeIf 条件移除 List<Book> books; books.removeIf(x -> x.getBookPrice() > 22); stream 对集合进行处理,并不会改变集合,类似于数据库的select 创建stream 集合.stream() 操作stream filter 过滤 map 转换 skip 跳过 distinct 去重 limit 取几条 结果处理 foreach 输 collect 收集 count 总条数 List<String> names = ; names.add("zhangsan"); names.add("zhangsan"); List nNews = names.stream.distinct().collect(Collectors.toList()); Collectors.toSet() //取名字长度大于4的,跳过第1个,取2个 List<String> nNames = names.stream.filter(x->x.length()>4).skip(1),limt(2).collect(Collectors.toList()); //把长度转换成一个集合 List<Integer> c = names.stream.map(x->x.length()).collect(Collectors.toList()); List<Integer> c = names.stream.map(String::length).collect(Collectors.toList());

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值