package com.ydlclass.feature;
import org.junit.Before;
import org.junit.Test;
import java.util.*;
import java.util.function.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class LambdaTest {
List<Person> personList = new ArrayList<>();
List<Integer> simpleList = Arrays.asList(15,22,9,11,33,52,14);
@Before
public void initData(){
personList.add(new Person("张三",3000,23,"男","太原"));
personList.add(new Person("李四",7000,34,"男","西安"));
personList.add(new Person("王五",5200,22,"女","西安"));
personList.add(new Person("小黑",1500,33,"女","上海"));
personList.add(new Person("狗子",8000,44,"女","北京"));
personList.add(new Person("铁蛋",6200,36,"女","南京"));
}
@Test
public void otherTest(){
personList.stream().sorted((o1, o2) -> o2.getSalary() - o1.getSalary()).
limit(3).forEach(System.out::println);
//首先去除了重复,跳过之后使用
List<Integer> list = Arrays.asList(1, 2, 3, 4, 4, 5, 1);
list.stream().distinct().skip(2).forEach(System.out::println);
//找出男员工和女员工中工资最高的人
Map<String, List<Person>> collect = personList.stream().collect(Collectors.groupingBy(new Function<Person, String>() {
@Override
public String apply(Person person) {
return person.getSex();
}
}));
collect.get("女").stream().sorted((o1, o2) -> o2.getSalary() - o1.getSalary()).limit(1).forEach(System.out::println);
collect.get("男").stream().sorted((o1, o2) -> o2.getSalary() - o1.getSalary()).limit(1).forEach(System.out::println);
//方式二
Map<String, List<Person>> collect1 = personList.stream().collect(Collectors.groupingBy(person -> person.getSex()));
Map<String,Integer> map = new HashMap<>(2);
for (Map.Entry<String, List<Person>> stringListEntry : collect1.entrySet()) {
String key = stringListEntry.getKey();
IntStream intStream = stringListEntry.getValue().stream().mapToInt(new ToIntFunction<Person>() {
@Override
public int applyAsInt(Person value) {
return value.getSalary();
}
});
int value = intStream.max().orElse(0);
map.put(key,value);
}
System.out.println(map);
}
}