java8-lambda表达式



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;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值