1. 程式人生 > >Jdk8 lambda 表示式例子

Jdk8 lambda 表示式例子

public static void test(){
	List<DwMmDefVO> vos = new ArrayList<DwMmDefVO>();
	DwMmDefVO temp = new DwMmDefVO();
	temp.setDsName("name1");
	temp.setDefType("Type1");
	temp.setPk("PK1");
	temp.setPkDs("DS1");
	temp.setPkDef("def1");
	vos.add(temp);
		
	//遍歷
	vos.forEach(vo -> {  
            System.out.println(vo.getDsName()+ "---" + vo.getPath());  
     });
		
	//獲取(key value) 第三個引數能解決重複key的問題
    vos.stream().collect(Collectors.toMap(DwMmDefVO::getPk,DwMmDefVO::getDsName,
        (oldValue, newValue) -> oldValue));

    //獲取(key Object)
	Map studentMap = vos.stream().collect(Collectors.toMap(DwMmDefVO:: getPkDef, (k) -> k)); 
		
	//獲取某一列資料
	List<String> strs = vos.stream().map(DwMmDefVO::getDsName)
			.collect(Collectors.toList());
		
	//獲取固定字元資料
	vos = vos.stream().filter(x -> x.getDsName().equals("0"))
		.distinct().collect(Collectors.toList());
		
	//排序
	vos = vos.stream().sorted((s1,s2) -> s1.getPkDs().compareTo(s2.getPkDs()))
		.collect(Collectors.toList());
				
	//分組
	Map<String,List<DwMmDefVO>> group = vos.stream()
		.collect(Collectors.groupingBy(DwMmDefVO::getDefType));
	}