package com.hema.cloudification.suandok.application.gate.exchange.http.lamda;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang.ArrayUtils;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.groupingBy;
public class 员工筛选 {
public static void main(String[] args) {
int[] arr={1,2,3};
Arrays.stream(ArrayUtils.remove(arr, 1)).forEach(System.out::println);
System.out.println("----------------");
Arrays.stream(ArrayUtils.removeElement(arr, 1)).forEach(System.out::println);
List<Person> personList = new ArrayList<Person>();
personList.add(new Person("Tom", 8900, 15, "male", "New York"));
personList.add(new Person("Jack", 7000, 15, "male", "Washington"));
personList.add(new Person("Lily", 7800, 17, "female", "Washington"));
personList.add(new Person("Anni", 8200, 18, "female", "New York"));
personList.add(new Person("Owen", 9500, 17, "male", "New York"));
personList.add(new Person("Alisa", 7900, 22, "female", "New York"));
//从员工集合中筛选出salary大于8000的员工,并放置到新的集合里。
System.out.println(personList.stream().filter(item -> item.getSalary() > 8000).collect(Collectors.toList()));
//统计员工的最高薪资、平均薪资、薪资之和。
System.out.println("最大薪资1:" + personList.stream().filter(Objects::nonNull).max(Comparator.comparing(Person::getSalary)));
System.out.println("最大薪资2:" + personList.stream().filter(Objects::nonNull).mapToInt(Person::getSalary).max());
System.out.println("平均薪资:" + personList.stream().mapToInt(Person::getSalary).average());
System.out.println("薪资之和:" + personList.stream().mapToInt(Person::getSalary).sum());
//将员工按薪资从高到低排序,同样薪资者年龄小者在前。
personList.sort(Comparator.comparing(Person::getSalary).reversed().thenComparing(Person::getAge));
//将员工按性别分类,将员工按性别和地区分类,将员工按薪资是否高于8000分为两部分。
System.out.println("将员工按性别分类:" + personList.stream().collect(Collectors.groupingBy(Person::getSex)));
System.out.println("将员工按性别和地区分类:" + personList.stream().collect(groupingBy(Person::getSex, groupingBy(Person::getArea))));
System.out.println("将员工按薪资是否高于8000分为两部分:" + personList.stream().collect(Collectors.partitioningBy(x -> x.getSalary() >= 8000)));
System.out.println("返回所有集合的元素数量:"+personList.stream().collect(Collectors.counting()));
System.out.println("-------");
Arrays.stream(personList.toArray()).forEach(System.out::println);
System.out.println(personList.toArray().getClass());
/* //1 构造数组
List<Employee> list = Arrays.asList(new Employee(1L, "e1"), new Employee(2L, "e2"), new Employee(3L, "e3"));
//2 所有name收集成list。toList
List<String> nameList = list.stream().map(x -> x.getName()).collect(Collectors.toList());
//3 name收集成set 。toSet
Set<String> nameSet = list.stream().map(x -> x.getName()).collect(Collectors.toSet());
//4 list转为map
Map<Long, String> map = list.stream().collect(Collectors.toMap(Employee::getId, Employee::getName));
// key重复时报错的解决
Map<Long, String> map2 = list.stream().collect(Collectors.toMap(Employee::getId, Employee::getName, (k2, k1) -> k1));
//5 根据id分组
Map<Long, List<Employee>> groupByMap = list.stream().collect(Collectors.groupingBy(Employee::getId));
//6 分区 分区是分组的特殊情况。 它最多可以分为两组,如id大于2和不大于2的
Map<Boolean, List<Employee>> part = list.stream().collect(Collectors.partitioningBy(x -> x.getId() > 2));
//7 name用逗号相连
String join = list.stream().map(Employee::getName).collect(Collectors.joining(","));*/
/**
* 转换成集合
*/
/* //转换成set集合
Set<Book> collect2 = books.stream().collect(Collectors.toSet());//HashSet
Set<Book> set = books.stream().collect(Collectors.toCollection(HashSet::new));
//
List<Book> collect3 = books.stream().collect(Collectors.toList());//ArrayList
LinkedList<Book> list = books.stream().collect(Collectors.toCollection(LinkedList::new));
ArrayList<Book> collect = books.stream().collect(Collectors.toCollection(ArrayList::new));
CopyOnWriteArrayList<Book> collect1 = books.stream().collect(Collectors.toCollection(CopyOnWriteArrayList::new));*/
/**
*转换成map
*/
/* //调用merge的时候有空值校验,如果值为null会报空指针//HashMap
Map<String, Double> collect = books.stream().collect(Collectors.toMap(Book::getName, item -> item.getPrice() == null ? 0 : item.getPrice()));
System.out.println("书名和价格: " + collect);
Map<String, String> collect1 = books.stream().collect(Collectors.toMap(Book::getAuthor, Book::getName, (one, two) -> one));//HashMap
System.out.println("作者和书名-有重复的选择第一个书名: " + collect1);
ConcurrentHashMap<String, String> collect2 = books.stream().collect(Collectors.toMap(Book::getAuthor, Book::getName,
(one, two) -> one.concat(two), ConcurrentHashMap::new));
System.out.println("作者和书名-有重复的选择第一个书名-指定返回值类型: " + collect2);
*/
/**
* 合并两个集合,去掉相同元素
*/
List<String> list1 = Arrays.asList("1","2","3","4");
List<String> list2 = Arrays.asList("3","4","5","6");
List<List<String>> list = Arrays.asList(list1,list2);
Set<String> collect = list.stream().
map(item-> Lists.newArrayList(item.get(0),item.get(1),item.get(2),item.get(3)))
.flatMap(Collection::stream)
.collect(Collectors.toSet());
}
}
@Data
class Person {
private String name; // 姓名
private int salary; // 薪资
private int age; // 年龄
private String sex; //性别
private String area; // 地区
// 构造方法
public Person(String name, int salary, int age, String sex, String area) {
this.name = name;
this.salary = salary;
this.age = age;
this.sex = sex;
this.area = area;
}
// 省略了get和set,请自行添加
}
@Data
class Per {
private String sex;
private String area;
public Per(String sex, String area) {
this.sex = sex;
this.area = area;
}
}