文章目录
()加上类型Arrays.asList(1,2,4,1,76).forEach(e-> System.out.println(e));
Arrays.asList(1,2,4,1,76).forEach((Integer e)-> System.out.println(e)); Set<Object> set=new HashSet<>(Arrays.asList(1,2,4,1,76));
set.forEach(e-> System.out.println(e));Map<Object,Object> map=new HashMap<>();
map.put("2",2);
map.put("4",5);
//a,b两个元素分别是key和value
map.forEach((a,b)->{
System.out.println(a+"---"+b);
});()->{}:无参数的实现item->{}:单个参数的实现,jdk能够默认推断出参数的类型(String item1,String item2):指定参数的类型FunctionalInterface:标记这个接口只能定义一个方法(除了默认的方法)@FunctionalInterface
public interface UserService {
String display(String name);
}
@Test
public void test1(){
UserService userService=name -> {
return name;
};
userService.display("che");
}public interface Demo1 {
/**
* 接口的静态方法
*/
static void display() {
System.out.println("cdc");
}
/**
* 默认方法可以不实现,但是可以被覆盖
*/
default void play() {
System.out.println("cdddd");
}
}@Test
public void test2(){
User user1 = new User();
user1.setName("chen");
user1.setAge(22);
User user3 = new User();
user3.setName("zheng");
user3.setAge(22);
User user2 = new User();
user2.setName("zhou");
user2.setAge(30);
List<User> users=new ArrayList<>();
users.add(user1);
users.add(user2);
users.add(user3);
/* 下面的语句类似如下
* users.stream().filter(item->item.getAge()>25).collect(Collectors.toList());
*/
List<User> collect = users.stream().filter(user -> {
if (user.getAge() > 25) {
return true;
} else {
return false;
}
}).collect(Collectors.toList());
}Stream<T> sorted(Comparator<? super T> comparator);
Stream<T> sorted();例子如下:
Stream.of(1, 8, 5, 2, 1, 0, 9, 2, 0, 4, 8)
.filter(n -> n > 2) // 对元素过滤,保留大于2的元素
.distinct() // 去重,类似于SQL语句中的DISTINCT
.skip(1) // 跳过前面1个元素
.limit(2) // 返回开头2个元素,类似于SQL语句中的SELECT TOP
.sorted() // 对结果排序
.forEach(System.out::println);
//按照age排序,实际上就是实现Comparator的接口方法compareTo
users.sort((item1,item2)->{
return item1.getAge()-item2.getAge();
});// 检查流中的任意元素是否包含字符串"Java"
boolean hasMatch = Stream.of("Java", "C#", "PHP", "C++", "Python")
.anyMatch(s -> s.equals("Java"));
// 检查流中的所有元素是否都包含字符串"#"
boolean hasAllMatch = Stream.of("Java", "C#", "PHP", "C++", "Python")
.allMatch(s -> s.contains("#"));
// 检查流中的任意元素是否没有以"C"开头的字符串
boolean hasNoneMatch = Stream.of("Java", "C#", "PHP", "C++", "Python")
.noneMatch(s -> s.startsWith("C"));
// 查找元素
Optional<String> element = Stream.of("Java", "C#", "PHP", "C++", "Python")
.filter(s -> s.contains("C"))
// .findFirst() // 查找第一个元素
.findAny(); // 查找任意元素List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
// 获取对应的平方数
List<Integer> squaresList = numbers.stream().map( i -> i*i).distinct().collect(Collectors.toList());@Test
public void test2(){
User user1 = new User();
user1.setName("chen");
user1.setAge(22);
User user3 = new User();
user3.setName("zheng");
user3.setAge(22);
User user2 = new User();
user2.setName("zhou");
user2.setAge(40);
List<User> users=new ArrayList<>();
users.add(user1);
users.add(user2);
users.add(user3);
List<User> list = users.stream().map(item -> {
item.setAge(item.getAge() * 2);
return item;
}).collect(Collectors.toList());
}//归约操作,返回的是一个Optional类型的
Optional<Integer> optional = users.stream().map(item -> item.getAge()).reduce((item1, item2) -> item1 + item2);
if (optional.isPresent()){
System.out.println(optional.get());
}/根据age进行分组,返回的是Map集合,key就是分组后的age,value是user对象
Map<Integer, List<User>> listMap = users.parallelStream().collect(Collectors.groupingBy(item -> item.getAge()));
listMap.forEach((key,value)->{
System.out.println(key+"--->"+value);
});//根据age进行分组,返回的是Map集合,key就是分组后的age,value是user对象
Map<String, List<User>> listMap = users.parallelStream().collect(Collectors.groupingBy(item -> {
if (item.getAge() > 20) {
return "A";
} else {
return "B";
}
}));
listMap.forEach((key,value)->{
System.out.println(key+"--->"+value);
});public static <T> Optional<T> of(T value):value的值不能为nullpublic static <T> Optional<T> ofNullable(T value):value允许为空public Optional<T> filter(Predicate<? super T> predicate):过滤其中的元素,如果返回true,那么保留,返回false去除该元素public T orElse(T other):如果该元素的值为null,那么指定该值为other User user = new User(22, "chenjiabing");
User user2 = Optional.ofNullable(user).filter(item->{
return item.getAge()>30;
}).orElse(null);Optional.ofNullable(user).filter(item->{
return item.getAge()>30;
}).orElseGet(()->{
System.out.println("该值为空");
return new User(44, "zhengjiahe");
});Optional<Integer> map = Optional.ofNullable(null).map(item->{
System.out.println(item);
//返回值也决定着你的类型
return Integer.MAX_VALUE;
});Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,Function<? super T, ? extends U> valueMapper):简单的将集合转换成Map,出现key重复的将直接抛出异常
keyMapper:指定的valueMapper:指定的value Map<Integer, String> map = users.stream().collect(Collectors.toMap(User::getAge, User::getName));
`public static <T, K, U> Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,Function<? super T, ? extends U> valueMapper,BinaryOperator<U> mergeFunction):用于解决key冲突的情况
mergeFunction:用于当出现key冲突的情况下解决方法,其中只需要实现apply方法即可,两个参数分别是重复的map的value值,返回值是指定的值 Map<Integer, String> map = users.stream().collect(Collectors.toMap(User::getAge, User::getName,(v1,v2)->v1+","+v2));
toListList<Integer> list = Stream.of(1, 2, 3, 4).collect(Collectors.toList()); <Integer> set = Stream.of(1, 2, 3, 4).collect(Collectors.toSet()); public static <T, K> Collector<T, ?, Map<K, List<T>>> groupingBy(Function<? super T, ? extends K> classifier)
Map<Integer, List<Integer>> map = Stream.of(1, 1, 3, 10, 2, 3, 4).collect(
Collectors.groupingBy(Integer::intValue));`public static
Collector> groupingBy(Function<? super T, ? extends K> classifier,
Collector<? super T, A, D> downstream)`
Map<Integer, Set<Integer>> map = Stream.of(1, 1, 3, 10, 2, 3, 4).collect(
Collectors.groupingBy(Integer::intValue, Collectors.toSet()));