// Collection新增方法
public class Lambda1 {
public static List<Integer> createListByInteger() {
List<Integer> list = Lists.newArrayList(1, 2, 32, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0);
return list;
}
public static List<String> createListByString() {
List<String> list = Lists.newArrayList("1", "2", "32", "3", "4", "5", "6", "7", "8", "9", "10", "11", "0");
return list;
}
public static void collectionAdd() {
List<Integer> list = createListByInteger();
// 删除列表中大于5的数
list.removeIf(item -> item > 5);
System.out.println(list.toString());
System.out.println("*******************************************");
// 对每个元素进行替换
List<Integer> list1 = createListByInteger();
list1.replaceAll(item -> {
return item > 5 ? item - 10 : item;
});
System.out.println(list1.toString());
System.out.println("*******************************************");
// 排序
List<Integer> list2 = createListByInteger();
// 正序排列
list2.sort((a1, a2) -> a1 - a2);
System.out.println(list2.toString());
System.out.println("###########################################");
// 倒叙排列
Comparator<Integer> comparator = (a1, a2) -> a1.compareTo(a2);
list2.sort(comparator.reversed());
System.out.println(list2.toString());
System.out.println("*******************************************");
}
public static void main(String[] args) {
collectionAdd();
}
}
欢迎关注我的微信公众号,更多内容将在微信公众号中发布: