package com.sinosoft.web.controller.test.lambda;
import com.sinosoft.common.utils.DateUtils;
import org.junit.Test;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class LambdaForeachList {
@Test
public void lambdaList() {
List list = new ArrayList<>();
list.add(new Empoyee("a", 3, 2));
list.add(new Empoyee("b", 2, 1));
list.add(new Empoyee("c", 1, 3));
list.add(new Empoyee("d", 4, 4));
list.add(new Empoyee("e", 5, 5));
list.add(new Empoyee("e", 5, 5));
/*
1 使用foreach进行遍历集合
item可以是任意值 类似于for循环中的循环值
*/
list.forEach(item -> {
System.out.println(item);
});
System.out.println("#####################################");
list.forEach(item -> {
item.setName(item.getName() + "测试");
System.out.println(item.toString());
});
/*
使用流操作
*/
//去重 distinct()去重;collect.(Collectors.toList()).封装集合
List distinctList = list.stream().distinct().collect(Collectors.toList());
System.out.println("去重:++++++++++++++++++++++++++++++");
distinctList.forEach(item -> {
System.out.println(item);
});
//排序 sorted((第一个对象),第二个对象)->返回值) (升降序看是第几个对象与第几个对象比较)
List sortedList = list.stream().sorted((o1, o2) -> o1.getAge() - o2.getAge()).collect(Collectors.toList());
System.out.println("排序:-----------------------------------");
sortedList.forEach(item -> {
System.out.println(item);
});
//过滤 filter(item->{}) item为每一项 按照自己的需求来筛选list中的数据
List filterList = list.stream().filter(item -> item.getAge() > 3).collect(Collectors.toList());
System.out.println("过滤:(((((((((((((((((((((((");
filterList.forEach(item -> System.out.println(item));
//map 提取对象中的某一个元素 用每一项来获得属性(也可以直接用对象::get属性)
List mapList = list.stream().map(Empoyee::getName).collect(Collectors.toList());
System.out.println(" 使用map提取对象中的某一个属性进行存值))))))))))))))))))))))))))))))");
mapList.forEach(item -> System.out.println(item));
//统计 sum() mapToDouble()转换成double 还有其他类型转换
double sum = list.stream().mapToDouble(Empoyee::getAge).sum();
Long sumLong = list.stream().mapToLong(Empoyee::getSize).sum();
System.out.println("总和(double):" + sum);
System.out.println("总和(long):" + sumLong);
//分组 Collectors.groupingBy(属性名)
Map> map = list.stream().collect(Collectors.groupingBy(Empoyee::getAge));
//todo 使用lambda解析map
//多重分组 Collectors.groupingBy(属性,Collectors.groupingBy(属性))
//Map>>mapMap = list.stream().collect(Collectors.groupingBy(i->i.getAge(),Collectors.groupingBy(l->l.getClass().getField("size"))));
//分组并计算综合
//Map> mapStatistics = list.stream().collect(Collectors.groupingBy(a->a.ge));
//集合比较的简写方式:
list.sort((o1, o2) -> {
return o1.getAge() - o2.getAge();
});
System.out.println("sort:OOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
list.forEach(item -> {
System.out.println(item);
});
}
/**
* 使用lambda表达式遍历map集合
*/
@Test
public void testLambdaMap() {
Map map = new HashMap<>();
map.put("1", "a");
map.put("2", "b");
map.put("3", "c");
//第一种 使用map的foreach方法加上Java8的lambda表达式
map.forEach((k, v) -> {
System.out.println(k + "-" + v);
});
//第二种 使用Map.Entry来遍历map的条目
for (Map.Entry it : map.entrySet()) {
System.out.println(it.getKey() + "-" + it.getValue());
}
//第三种 可以使用for结合map的keySet和values的方法来遍历
for (String a : map.keySet()) {
System.out.println(a);
}
for (String b : map.values()) {
System.out.println(b);
}
//第四种 使用迭代器
Iterator> iterator = map.entrySet().iterator();//返回所有的entry实体
while (iterator.hasNext()) {
Map.Entry next1 = iterator.next();
String key = next1.getKey();
String value = next1.getValue();
System.out.println(key);
System.out.println(value);
}
//通过键来遍历
Iterator keyIterator = map.keySet().iterator();
while (keyIterator.hasNext()) {
System.out.println(keyIterator.next());
}
}
/**
* stream 集合的生成流
*/
/**
* 集合有两种方式生成流:
* stream(): 为集合创建串行流
* parallelSteam(): 为集合创建并行流
* Steam():包含了大部分的中间操作和终止操作:
* 中间操作:
* 此类型返回的都是Steam:
* map(mapToInt ,flatMap等)、filter distinct sorted peek limit skip parallel sequential unordered
* 终止操作:
* foreach foreachedOrdered toArray reduce collect min max count anyMatch allMatch noneMatch findFirst findAny iterator
*
*
*/
@Test
public void male(){
Male m1 = new Male(1,"肖战",15,"浙江");
Male m2 = new Male(2,"王一博",15,"湖北");
Male m3 = new Male(3,"杨紫",17,"北京");
Male m4 = new Male(4,"李现",17,"浙江");
List list = new ArrayList<>();
list.add(m1);
list.add(m2);
list.add(m3);
list.add(m4);
/**
* 查询地址是浙江的集合
*/
//Stream maleStream = list.stream().filter(s -> "浙江".equals(s.getAddress()));
//maleStream.forEach(System.out::println);
/**
* 查询年龄是15岁的集合
*/
//List collect = list.stream().filter(item -> item.getAge() == 15).collect(Collectors.toList());
//collect.forEach(System.out::println);
/**
* map 转换
*/
//List collect = list.stream().map(a -> "地址:" + a.getAddress()).collect(Collectors.toList());
//collect.forEach(System.out::println);
/**
* distinct去重
* 简单的字符串去重
*/
//List stringList = Arrays.asList("111","222","333","111","222");
//stringList.stream().distinct().forEach(System.out::println);
/**
* sorted排序
*/
//List stringList = Arrays.asList("111","222","333","111","222");
//stringList.stream().sorted().forEach(System.out::println);
/**
* limit限制返回的个数
*/
//List stringList = Arrays.asList("111","222","333","111","222");
//stringList.stream().limit(2).forEach(System.out::println);
/**
* skip 删除元素
*/
//List stringList = Arrays.asList("111","222","333","444","555");
//stringList.stream().skip(3).forEach(System.out::println);
//System.out.println(stringList.size()+"长度");
/**
* reduce 聚合
*/
//List stringList = Arrays.asList("欢","迎","你");
//String reduce = stringList.stream().reduce("北京", (a, b) -> a + b);
//System.out.println(reduce);
}
@Test
public void sys(){
long l = System.currentTimeMillis();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String format = simpleDateFormat.format(l);
System.out.println(format);
}
@Test
public void minPay(){
List list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
List collect = list.stream().sorted((a, b) -> b - a).collect(Collectors.toList());
for (Integer i:collect
) {
System.out.println(i);
}
}
}