package com.model.threadpool;
import com.oracle.webservices.internal.api.databinding.DatabindingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
/**
* @Description:测试类
* @Author: 张紫韩
* @Crete 2021/6/8 18:52
*/
/**
* 题目:请按照给出的数据,找出同时满足以下条件的用户,也即以下条件全部满足的用户
* 偶数ID且年龄大于24 且用户名转为大写且用户名字母倒序
* 只输出一个用户名字
*/
public class StreamDemo {
public static void main(String[] args) {
User user1 = new User(11, "a", 23);
User user2 = new User(12, "b", 24);
User user3 = new User(13, "c", 22);
User user4 = new User(14, "d", 28);
User user5 = new User(16, "e", 26);
List<User> list = Arrays.asList(user1, user2, user3, user4, user5);
/*
1.普通方法的实现
StreamDemo streamDemo=new StreamDemo();
System.out.println(streamDemo.getUser(list));
*/
/*
list.forEach(user -> {
System.out.println("user.toString() = " + user.toString());
});
list.forEach(System.out::println);
*/
/* list.stream()
.filter(t -> {return t.getId()%2==0;})
.filter(v->{return v.getAge()>24;})
.forEach(System.out::println);*/
list.stream()
.filter(t -> {return t.getId()%2==0;})
.filter(v->{return v.getAge()>24;})
.map(user -> {return user.getUserName().toUpperCase(Locale.ROOT);})
.sorted((o1,o2)->{return o2.compareTo(o1);})
.limit(1)
.forEach(System.out::println);
//原来的值不会变化
list.forEach(System.out::println);
System.out.println("*******************************");
List<Integer> list1=Arrays.asList(1,2,3,4,5,6);
list1.stream().map(t->{return t * 2;}).forEach(System.out::println);
// list1=list1.stream().map(t->{return t * 2;}).collect(Collectors.toList());
/*
* map:映射改变,不改变原来的值,可以使用.collect(Collectors.toList())讲流转为list,
* 想要使用计算后的记过,但是必须有参数接受即:list1=list1.stream().map(t->{return t * 2;}).collect(Collectors.toList());
*
* 也可以使用流的方式直接运算,算完只有直接输出(流计算不会改变原来的值)
* */
}
public String getUser(List<User> list) {
List<User> res=new ArrayList<>();
for (User user : list) {
if (user.getId() % 2 == 0 && user.getAge() > 24) {
user.setUserName(user.getUserName().toUpperCase(Locale.ROOT));
res.add(user);
}
}
return res.get(res.size()-1).getUserName();
}
}
class User {
private Integer id;
private String userName;
private int age;
public User(Integer id, String userName, int age) {
this.id = id;
this.userName = userName;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", age=" + age +
'}';
}
}