1.1 forEach
List<Menu> menuList = new ArrayList<Menu>();
menuList.add(new Menu("GN001D000","0","系统管理","/admin","Y",1, "GN001D000"));
menuList.add(new Menu("GN001D100","GN001D000","权限管理","/admin","Y",2, "GN001D100"));
menuList.add(new Menu("GN001D110","GN001D100","密码修改","/admin","Y",3, "GN001D110"));
menuList.add(new Menu("GN001D120","GN001D100","新加用户","/admin","Y",4, "GN001D120"));
menuList.add(new Menu("GN001D200","GN001D000","系统监控","/admin","Y",5, "GN001D200"));
menuList.add(new Menu("GN001D210","GN001D200","在线用户","/admin","Y",6, "GN001D210"));
menuList.add(new Menu("GN002D000","0","订阅区","/admin","Y",7, "GN002D000"));
menuList.add(new Menu("GN002D000","0","未知领域","/admin","Y",8, "GN002D000"));
Map<String, List<Menu>> map = new HashMap<>();
JSONObject json = new JSONObject();
menuList.forEach(menu -> {
if (json.containsKey(menu.getId())) {
((List<Menu>)json.get(menu.getId())).add(menu);
} else {
List<Menu> list = new ArrayList<Menu>();
list.add(menu);
json.put(menu.getId(), list);
}
});
1.2 filter
menuList.stream().filter(menu -> menu.getId() != null).collect(Collectors.toList());
1.3 map
menuList.stream().filter(menu -> menu.getId() != null).map(Menu::getId).collect(Collectors.toList());
menuList.stream().collect(Collectors.toMap(Menu::getText, Function.identity()));
Map<String, Object> map = new HashMap<String, Object>();
map.put("1", "壹");
map.put("2", "贰");
map.put("3", "叁");
map.put("4", null);
System.out.println(map.entrySet().stream().filter(m -> null != m.getValue()).collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue())));
System.out.println(map.entrySet().stream().map(m -> m.getKey()).collect(Collectors.joining()));
System.out.println(map.entrySet().stream().filter(m -> null != m.getValue()).map(m -> m.getValue()).collect(Collectors.toList()));
1.4 排序
Map<String, List<Menu>> collect = menuList.stream().collect(Collectors.groupingBy(Menu::getId));
System.out.println(Arrays.toString(menuList.toArray()));