Stream流(函数编程)

Stream流(函数编程)

List<Test11> costBeforeTax = new ArrayList();
Test11 test11=new Test11(1L, "test1");
costBeforeTax.add(test11);
costBeforeTax.add(new Test11(2L, "test5"));
costBeforeTax.add(new Test11(3L, "test3"));
costBeforeTax.add(new Test11(4L, "test4"));
List<Test11> costBeforeTax1 = new ArrayList();
costBeforeTax1.add(test11);
costBeforeTax1.add(new Test11(2L, "test2"));
costBeforeTax1.add(new Test11(3L, "test3"));
costBeforeTax1.add(new Test11(4L, "test4"));

//交集
List<Test11> test11s=costBeforeTax.stream().filter(costBeforeTax1::contains).collect(Collectors.toList());	//同一个对象

//并集
List<Test11> list=Stream.of(costBeforeTax,costBeforeTax1).flatMap(Collection::stream).distinct().collect(Collectors.toList());

list.forEach(System.out::println);

distinct();//去重

//Test11{testId=1, testName='test1'}
//Test11{testId=2, testName='test5'}
//Test11{testId=3, testName='test3'}
//Test11{testId=4, testName='test4'}
//Test11{testId=2, testName='test2'}
//Test11{testId=3, testName='test3'}
//Test11{testId=4, testName='test4'}

//差集
costBeforeTax.stream().filter(test111 -> !costBeforeTax1.contains(test111)).collect(Collectors.toList()).forEach(System.out::println);
//Test11{testId=2, testName='test5'}
//Test11{testId=3, testName='test3'}
//Test11{testId=4, testName='test4'}
costBeforeTax.stream().forEach(System.out::println);
//Test11{testId=1, testName='test1'}
//Test11{testId=2, testName='test5'}
//Test11{testId=3, testName='test3'}
//Test11{testId=4, testName='test4'}
ArrayList list= (ArrayList) costBeforeTax.stream().map(test1 -> test1.getTestName()).collect(Collectors.toList());

list.forEach(System.out::println);
//test1
//test5
//test3
//test4
System.out.println(costBeforeTax.stream().map(
 test111 -> test111.getTestName()).collect(Collectors.joining(",")));

//test1,test5,test3,test4
Map map = costBeforeTax.stream().collect(Collectors.toMap(test1111 -> test1111.getTestId(), test1111 -> test1111.getTestName()));

System.out.println(map.entrySet()); //[1=test1, 2=test5, 3=test3, 4=test4]
count();//个数

//集合里任意一个值符合条件返回true
System.out.println(costBeforeTax.stream().anyMatch(test111 -> test111.getTestName().equals("test1")));  //true

//集合里每一个值都符合条件才会返回true,反之false
System.out.println(costBeforeTax.stream().allMatch(test111 -> test111.getTestName().equals("test1")));  //false

//集合里都不符合条件才会返回true,反之false
System.out.println(costBeforeTax.stream().noneMatch(test111 -> test111.getTestName().equals("test1")));  //false

//取列表中ID最大的哪个
System.out.println(costBeforeTax.stream().max(Comparator.comparing(t -> t.getTestId())).get()); //Test11{testId=4, testName='test4'}

//取列表中ID最小的哪个
System.out.println(costBeforeTax.stream().min(Comparator.comparing(t -> t.getTestId())).get()); //Test11{testId=1, testName='test1'}

//复杂比较Test11 test111=costBeforeTax.stream().max(new Comparator<Test11>() {
 @Override
 public int compare(Test11 o1, Test11 o2) {

     return 0;
 }
}).get();

//汇总,例子汇总ID
System.out.println(costBeforeTax.stream().mapToInt(x -> x.getTestId().intValue()).summaryStatistics());//IntSummaryStatistics{count=4, sum=10, min=1, average=2.500000, max=4}

//自定义函数接口
@FunctionalInterface
public interface Funtion11 {
 void study();
}

//使用
void testFuntion(){
 Funtion11 funtion11=()->System.out.println("In");
 funtion11.study();
}

内置函数接口

//消费型接口: Consumer< T> void accept(T t)有参数,无返回值的抽象方法;
Consumer<Test11> testConsumer=(p)-> System.out.println(p.toString());
testConsumer.accept(new Test11(1L,"Test1"));	//Test11{testId=1, testName='Test1'}

//供给型接口: Supplier < T> T get() 无参有返回值的抽象方法;
Supplier<Test11> test11Supplier=Test11::new;
test11Supplier.get();	//获取到一个对象

//断定型接口: Predicate<T> boolean test(T t):有参,但是返回值类型是固定的boolean
Predicate<Test11> test11Predicate=new Predicate<Test11>() {
 @Override
 public boolean test(Test11 test11) {
     return test11.getTestName().equals("test1");
 }
};
System.out.println(test11Predicate.test(new Test11(1L, "test1")));  //true
///判断字符串是否为空
Predicate<String> predicate=String::isEmpty;
System.out.println(predicate.test(""));

//函数型接口: Function<T,R> R apply(T t)有参有返回值的抽象方法;
Function<Long,String> longStringFunction=new Function<Long, String>() {
 @Override
 public String apply(Long aLong) {
     return aLong.toString();
 }
};

System.out.println(longStringFunction.apply(23L));  //23,String
//sorted((o1, o2) -> o1.getTestId().compareTo(o2.getTestId())):id小到大排序
//limit(2):取两个
costBeforeTax.stream().sorted((o1, o2) -> o1.getTestId().compareTo(o2.getTestId())).limit(2).forEach(System.out::println);

//取值并存入到TreeSet中
costBeforeTax.stream().map(t->t.getTestName()).collect(Collectors.toCollection(TreeSet::new))
 .forEach(System.out::println);

//操作chars():把字符串变为字符
System.out.println("hoeege".chars().distinct().mapToObj(c -> String.valueOf((char)c)).collect(Collectors.joining()));	//hoeg

//分组
//[test4=[Test11{testId=4, testName='test4'}], test3=[Test11{testId=2, testName='test3'}, Test11{testId=3, testName='test3'}], test1=[Test11{testId=1, testName='test1'}]]
System.out.println(costBeforeTax.stream().collect(Collectors.groupingBy(test111 -> test111.getTestName())).entrySet());


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值