码迷,mamicode.com
首页 > 其他好文 > 详细

Stream系列(十)Count方法使用

时间:2019-12-03 14:22:16      阅读:442      评论:0      收藏:0      [点我收藏+]

标签:vector   target   get   Collector   src   form   ==   mic   cto   

计数器

视频讲解: https://www.bilibili.com/video/av77905733/

技术图片

EmployeeTestCase.java
package com.example.demo;

import lombok.extern.log4j.Log4j2;
import org.junit.Test;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.Assert.assertTrue;

@Log4j2
public class EmployeeTestCase extends BaseTestCase {
    @Test
    public void count(){
        long lCount = list.stream().count();
        assertTrue(lCount == 5);
        int iCount = 0;
        for(Employee employee :list){
            if(employee.getSalary()==2000){
                iCount ++;
            }
        }
        log.info("iCount:{}",iCount);
        long lCount2 = list.stream().filter(employee -> employee.getSalary() == 2000).count();
        assertTrue(iCount == lCount2);
    }
}

  

BaseTestCase.java
package com.example.demo;

import java.util.Arrays;
import java.util.List;

public class BaseTestCase {
    protected static final List<Employee> list = Arrays.asList(
            new Employee(1, "Alex", 1000),
            new Employee(2, "Michael", 2000),
            new Employee(3, "Jack", 1500),
            new Employee(4, "Owen", 1500),
            new Employee(5, "Denny", 2000));
}

  关注公众号,坚持每天3分钟学习

技术图片

Stream系列(十)Count方法使用

标签:vector   target   get   Collector   src   form   ==   mic   cto   

原文地址:https://www.cnblogs.com/JavaWeiBianCheng/p/11976392.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!