package com.iflytek.zhangh;
import static java.util.stream.Collectors.toList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** * 测试类 * @author zhangh * @date 2018年8月31日下午12:48:15 */
public class JDK8 {
/** * JDK 新特性 * @author zhangh * @date 2018年8月31日下午12:48:03 * @param args */
public static void main(String[] args) {
Map<String, Object> map = new HashMap<>( );
map.put("age", "11");
map.put("name", "demo1");
Map<String, Object> map1 = new HashMap<>( );
map1.put("age", "22");
map1.put("name", "demo2");
Map<String, Object> map2 = new HashMap<>( );
map2.put("age", "33");
map2.put("name", "demo3");
Map<String, Object> map3 = new HashMap<>( );
map3.put("age", "44");
map3.put("name", "demo2");
List<Map<?, ?>> list = new ArrayList<>();
list.add(map);
list.add(map1);
list.add(map2);
list.add(map3);
//筛选name =demo2 且 取出age的值拼成一个list
List<String> li = list.stream().filter(m->m.get("name").equals("demo2")).map(m->m.get("age").toString()).collect(toList());
System.out.println(li.toString());
}
}
输出:
[22, 44]