package com.java8新特性内容概述
/**
*
*/
public class Employee {
private int id;
private String name;
private int age;
private double salary;
public Employee getInstance(){
return new Employee(1,"mimi",29,2222);
}
public Employee getInstance2(){
return new Employee(2,"Tom");
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Employee() {
System.out.println("Employee().....");
}
public Employee(int id) {
this.id = id;
System.out.println("Employee(int id).....");
}
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public Employee(int id, String name, int age, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Employee employee = (Employee) o;
if (id != employee.id)
return false;
if (age != employee.age)
return false;
if (Double.compare(employee.salary, salary) != 0)
return false;
return name != null ? name.equals(employee.name) : employee.name == null;
}
@Override
public int hashCode() {
int result;
long temp;
result = id;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + age;
temp = Double.doubleToLongBits(salary);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
}
package com.java8新特性内容概述;
import java.util.ArrayList;
import java.util.List;
/**
* 提供用于测试的数据
*/
public class EmployeeData {
public static List<Employee> getEmployees() {
List<Employee> list = new ArrayList<>();
list.add(new Employee(1001, "马化腾", 34, 6000.38));
list.add(new Employee(1002, "马云", 12, 9876.12));
list.add(new Employee(1003, "刘强东", 33, 3000.82));
list.add(new Employee(1004, "雷军", 26, 7657.37));
list.add(new Employee(1005, "李彦宏", 65, 5555.32));
list.add(new Employee(1006, "比尔盖茨", 42, 9500.43));
list.add(new Employee(1007, "任正非", 26, 4333.32));
list.add(new Employee(1008, "扎克伯格", 35, 2500.32));
return list;
}
}
@Test
public void test3(){
List<Employee> employeeList = EmployeeData.getEmployees();
//练习: 查找工资大于6000的员工,结果返回一个list 或者set
System.out.println("-----------------------List--工资大于6000-----------------------");
List<Employee> list = employeeList.stream().filter(s -> s.getSalary() > 6000).collect(Collectors.toList());
list.forEach(System.out::println);
System.out.println("--------------------set---工资小于6000---------------------------");
Set<Employee> employeeSet = employeeList.stream().filter(s -> s.getSalary() < 6000).collect(Collectors.toSet());
for (Employee employee : employeeSet) {
System.out.println(employee);
}
System.out.println("---------------------map-----------");
//转为map 方式1
List<Employee> employees = EmployeeData.getEmployees();
Map<Integer, Object> map = employees.stream().collect(Collectors.toMap(Employee::getId, Employee::toString));
Set<Integer> keySet = map.keySet();
Iterator<Integer> iterator = keySet.iterator();
while (iterator.hasNext()){
Integer key = iterator.next();
Object value = map.get(key);
System.out.println(key+"="+value);
}
System.out.println("----------------------- //转为map 方式2----------------------------------");
Map<Integer, Employee> map2 = employees.stream().collect(Collectors.toMap(e -> e.getId(), o -> o));
map2.forEach((key,value) -> System.out.println(key+"="+value));