package springboot.activiti.demo.utils;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
public class LambdaDemo {
private static final Logger LOGGER = LoggerFactory.getLogger(LambdaDemo.class);
public static void main(String[] args) {
ArrayList<Person> arrayList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Person person = new Person("Tom" + i, i + 10, i % 2 == 0 ? 0 : 1);
arrayList.add(person);
}
// 原始数据
LOGGER.info("===原来arrayList===:{}", JSON.toJSON(arrayList));
// 重新创建一个List
List<Person> newList = arrayList.stream().collect(Collectors.toList());
// 循环输出
newList.stream().forEach(p -> {
LOGGER.info("=person= 名字:{} -年龄:{} -性别:{}", p.getName(), p.getAge(), p.getSex() == 0 ? "男" : "女");
});
// 只获取男生
List<Person> boys = newList.stream().filter(p -> p.getSex() == 0).distinct().collect(Collectors.toList());
boys.stream().forEach(p -> {
LOGGER.info("=boys= 名字:{} -年龄:{} -性别:{}", p.getName(), p.getAge(), p.getSex() == 0 ? "男" : "女");
});
// 获取年龄大于16的
List<Person> more16List = newList.stream().filter(p -> Integer.valueOf(p.getAge()) > 16).distinct()
.collect(Collectors.toList());
more16List.stream().forEach(p -> {
LOGGER.info("=more16List= 名字:{} -年龄:{} -性别:{}", p.getName(), p.getAge(), p.getSex() == 0 ? "男" : "女");
});
// 性别排序
List<Person> sexList = newList.stream().sorted((p1, p2) -> p1.getSex().compareTo(p2.getSex()))
.collect(Collectors.toList());
sexList.stream().forEach(p -> {
LOGGER.info("=sexList= 名字:{} -年龄:{} -性别:{}", p.getName(), p.getAge(), p.getSex() == 0 ? "男" : "女");
});
// 年龄倒序
Comparator<Person> comparator = (p1, p2) -> p1.getAge().compareTo(p2.getAge());
newList.sort(comparator.reversed());
newList.stream().forEach(p -> {
LOGGER.info("=年龄倒序= 名字:{} -年龄:{} -性别:{}", p.getName(), p.getAge(), p.getSex() == 0 ? "男" : "女");
});
// 多条件排序
newList.sort(Comparator.comparing(Person::getSex).thenComparing(Person::getAge).reversed());
newList.stream().forEach(p -> {
LOGGER.info("多条件排序 名字:{} -年龄:{} -性别:{}", p.getName(), p.getAge(), p.getSex() == 0 ? "男" : "女");
});
// 性别分组
Map<Integer, List<Person>> sexGroup = newList.stream().collect(Collectors.groupingBy(Person::getSex));
LOGGER.info("sexGroup:{}", sexGroup);
// 对象去重,必须重写hasCode, equals方法
Person person1 = new Person("Tom", 10, 0);
Person person2 = new Person("Tom2", 16, 0);
Person person3 = new Person("Mary", 15, 1);
Person person4 = new Person("Tom", 10, 0);
Person person5 = new Person("Mary", 15, 1);
arrayList = new ArrayList<>();
arrayList.add(person1);
arrayList.add(person2);
arrayList.add(person3);
arrayList.add(person4);
arrayList.add(person5);
arrayList.stream().forEach(p -> {
LOGGER.info("=对象去重原来List= 名字:{} -年龄:{} -性别:{}", p.getName(), p.getAge(), p.getSex() == 0 ? "男" : "女");
});
// 去重
List<Person> collect = arrayList.stream().distinct().collect(Collectors.toList());
collect.stream().forEach(p -> {
LOGGER.info("=去重后List= 名字:{} -年龄:{} -性别:{}", p.getName(), p.getAge(), p.getSex() == 0 ? "男" : "女");
});
}
}
class Person {
private String name;
private Integer age;
private Integer sex;
public Person(String name, Integer age, Integer sex) {
super();
this.name = name;
this.age = age;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((age == null) ? 0 : age.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((sex == null) ? 0 : sex.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (age == null) {
if (other.age != null)
return false;
} else if (!age.equals(other.age))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (sex == null) {
if (other.sex != null)
return false;
} else if (!sex.equals(other.sex))
return false;
return true;
}
}
Java Lambda表达式 List遍历、过滤、排序、去重
最新推荐文章于 2023-11-25 16:32:48 发布