import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Test {
public static void main(String[] args) {
List<Company> comList = new ArrayList<>();
comList.add(new Company(1, "娃哈哈公司"));
comList.add(new Company(2, "嘻唰唰公司"));
comList.add(new Company(3, "亮晶晶公司"));
/**
* 1. 普通遍历
*/
System.out.println("------ 题目 1. ------");
comList.forEach(a -> System.out.println(a.toString()));
/**
* 2. 将集合中的公司id提取出来
*/
List<Integer> co = comList.stream().map(u -> u.getId()).collect(Collectors.toList());
System.out.println("------ 题目 2. ------");
co.forEach(c -> System.out.println(c));
// 可添加去重distinct关键字
comList.stream().map(u -> u.getId()).distinct().collect(Collectors.toList());
/**
* 3. 将集合数据放入Map<id,Company>,key为id,value为Company对象
*/
Map<Object, Company> companyMap = new HashMap<>();
comList.forEach(com -> companyMap.put(com.getId(), com));
// 3.1 第二种写法
Map<Integer, Company> companyMap2 = comList.stream().collect(Collectors.toMap(Company::getId, c -> c));
// 3.2 key为id,value为公司名
Map<Integer, String> collect = comList.stream()
.collect(Collectors.toMap(Company::getId, c -> c.getCompanyName()));
System.out.println("------ 题目 3.2 ------");
System.out.println(collect.toString());
// 3.3 若key有重复,选择保留新值还是旧值
comList.add(new Company(3, "靓靓靓公司"));
Map<Integer, String> collect2 = comList.stream()
.collect(Collectors.toMap(Company::getId, c -> c.getCompanyName(), (oldV, newV) -> newV));
System.out.println("------ 题目 3.3 若key有重复,选择保留新值还是旧值 ------");
System.out.println(collect2.toString());
/**
* 4. 根据id排序
*/
List<Company> comNew = new ArrayList<>();
comNew.add(new Company(12, "娃哈哈公司"));
comNew.add(new Company(19, "阳光公司"));
comNew.add(new Company(8, "彩虹公司"));
comNew.add(new Company(29, "小白马公司"));
comNew.add(new Company(3, "亮晶晶公司"));
System.out.println("------ 题目 4.1 升序 ------");
comNew.sort((c1, c2) -> c1.getId().compareTo(c2.getId()));
comNew.forEach(c -> System.out.println(c.toString()));
System.out.println("------ 题目 4.2 降序 ------");
comNew.sort((c1, c2) -> c2.getId().compareTo(c1.getId()));
comNew.forEach(c -> System.out.println(c.toString()));
System.out.println("------ 题目 4.3 过滤 ------");
Stream<Company> filter = comNew.stream().filter(c->c.getId()<10);
System.out.println(filter.count());
// 转化为新集合
List<Company> collect3 = filter.collect(Collectors.toList());
// comNew.stream().map(mapper)
//创建线程
new Thread(() -> System.out.println("Hello world !")).start();
Thread thread = new Thread(() -> System.out.println("Hello world !"));
thread.start();
thread.join();
}
}
class Company {
public Company(int id, String companyName) {
this.id = id;
this.companyName = companyName;
}
Integer id;
String companyName;
public Integer getId() {
return id;
}
@Override
public String toString() {
return "Company [id=" + id + ", companyName=" + companyName + "]";
}
public void setId(Integer id) {
this.id = id;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String name) {
this.companyName = name;
}
}
java8-lambda表达式
最新推荐文章于 2024-07-19 14:15:00 发布