// 遍历
public static void main(String[] args) {
List strList = Arrays.asList("a", "b", "c", "d");
// s:随便定义
strList.stream().forEach(s -> {
System.out.println(s);
});
}
// 平方
public static void main(String[] args) {
List list = Arrays.asList(1, 2, 6, 9);
// s:随便定义
List newList = list.stream().map(s -> s * s).collect(Collectors.toList());
// 遍历输出
newList.stream().forEach(n -> {
System.out.println(n);
});
}
// 转换为大写
public static void main(String[] args) {
List strList = Arrays.asList("a", "b", "c", "d");
// s:随便定义
strList.stream().map(String::toUpperCase).collect(Collectors.toList()).forEach(s -> {
System.out.println(s);
});
}
// 过滤
public static void main(String[] args) {
List strList = Arrays.asList("ab", "bcd", "cde", "de");
// s:随便定义, 过滤长度不等于2的元素
strList.stream()
.filter(s -> s.length() != 2) // 过滤
.collect(Collectors.toList()) // 转成新的list
.forEach(s -> { // 遍历
System.out.println(s);
});
}
// 过滤:得到其中不为空的String
public static void main(String[] args) {
List strList = Arrays.asList("ab", "bcd", "cde", "de", "");
// s:随便定义, 过滤非空的元素
strList.stream()
.filter(s -> !s.isEmpty()) // 过滤
.collect(Collectors.toList()) // 转成新的list
.forEach(s -> { // 遍历
System.out.println(s);
});
}
// 返回特定结果的list
public static void main(String[] args) {
List strList = Arrays.asList("a", "b", "c", "d", "e", "f");
// s:随便定义,
List newList = strList.stream()
.skip(2) // 扔掉前面2个元素
.limit(3) // 返回前3个元素
.collect(Collectors.toList()); // 得到一个新的list
newList.stream().forEach(s -> {
System.out.println(s);
});
}
// 排序
public static void main(String[] args) {
List strList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
// ln1, ln2:随便定义,
List newList = strList.stream()
.sorted((ln1, ln2) -> (ln1-ln2)) // 排序
.collect(Collectors.toList()); // 返回新的list
// newList就是排序之后的list
}
Long id = userList.stream().max(Comparator.comparing(user::getId)).get().getId();
Long id = userList.stream().min(Comparator.comparing(user::getId)).get().getId();
/ 最大值,最小值
public static void main(String[] args) {
List strList = Arrays.asList("a","b","d","s","n","yu","luo","shuai","spot", "xhuai");
// s:随便定义,
int max = strList.stream()
.mapToInt(s -> s.length()) // 判断条件:长度
.max() // 获取判断条件的最大值
.getAsInt(); // 转成int长度
System.out.println(max);
// 返回一个新的list, 新的list中元素的长度是strList中元素长度最长的
List maxList = strList.stream().filter(s -> s.length() == max).collect(Collectors.toList());
System.out.println(maxList.size());
// 集合中最小长度
List strList2 = Arrays.asList("a","b","d","s","nd","yu","luo","shuai");
int min = strList2.stream()
.mapToInt(s -> s.length())
.min().getAsInt();
System.out.println(min);
}
// List集合查重
public static void main(String[] args) {
List strList = Arrays.asList("a","b","d","s","n","a","s","c","d", "b");
// 返回一个去掉重复元素之后的新的list
List noRepeatList = strList.stream()
.distinct()
.collect(Collectors.toList());
System.out.println(noRepeatList.size());
}
// 匹配
public static void main(String[] args) {
/**
* allMatch: Stream 中全部元素符合传入的predicate, 返回true
* anyMatch: Stream 中只要有一个元素符合传入的predicate, 返回true
* noneMatch: Stream 中没有一个元素符合传入的predicate, 返回true
*/
List strList = Arrays.asList("a", "b", "d", "s", "n", "a", "s", "c", "d", "b");
// 判断集合中有没有c元素, 返回值boolean
boolean isExits = strList.stream().anyMatch(s -> s.equals("c"));
System.out.println(isExits);
List strList2 = Arrays.asList("a", "b", "d", "s", "n", "a", "s", "c", "d", "");
// 判断集合是否全不为空
boolean isFull = strList2.stream().allMatch(s -> s.isEmpty());
System.out.println(isFull);
boolean isNotEmpty = strList2.stream().noneMatch(s -> s.isEmpty());
System.out.println(isNotEmpty);
}