()—参数列表
-> 分隔
{} 方法体
(a,b)->{
}
无参方法调用
带参数方法调用
(函数接口的参数列表 不需要写类型 需要定义参数名称)->{方法体}
():函数方法参数列表
->分隔 {}方法体
(a,b)->{
Sout(a,b)
}
Lambda语法:
():参数列表
->分隔
{}:方法体
()->{}
ArrayList strings = new ArrayList<>();
strings.add("mayikt");
strings.add("xiaowei");
strings.add("xiaomin");
// strings.forEach(new Consumer() {
// @Override
// public void accept(Object o) {
// System.out.println("o:" + o);
// }
// });
strings.forEach((o) -> {
System.out.println(o);
});
public class UserEntity {
private String name;
private Integer age;
public UserEntity(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
@Override
public String toString() {
return "UserEntity{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
ArrayList userlists = new ArrayList<>();
userlists.add(new UserEntity("mayikt", 22));
userlists.add(new UserEntity("xiaomin", 18));
userlists.add(new UserEntity("xiaoha", 36));
// userlists.sort(new Comparator() {
// @Override
// public int compare(UserEntity o1, UserEntity o2) {
// return o1.getAge() - o2.getAge();
// }
// });
userlists.sort((o1, o2) ->
o1.getAge() - o2.getAge()
);
userlists.forEach((Consumer) o -> System.out.println("o:" + o.toString()));
new Thread(()-> System.out.println("我是子线程")).start();
ArrayList userEntities = new ArrayList<>();
userEntities.add(new UserEntity("mayikt", 20));
userEntities.add(new UserEntity("meite", 28));
userEntities.add(new UserEntity("zhangsan", 35));
userEntities.add(new UserEntity("xiaowei", 16));
userEntities.add(new UserEntity("xiaowei", 16));
userEntities.stream();
userEntities.parallelStream();
Stream stream = userEntities.stream();
//将我们的集合转为Set
Set collectSet = stream.collect(Collectors.toSet());
System.out.println(collectSet);
import com.mayikt.entity.UserEntity;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @ClassName Test001
* @Author 蚂蚁课堂余胜军 QQ644064779 www.mayikt.com
* @Version V1.0
**/
public class Test001 {
public static void main(String[] args) {
ArrayList userEntities = new ArrayList();
userEntities.add(new UserEntity("mayikt", 20));
userEntities.add(new UserEntity("meite", 28));
userEntities.add(new UserEntity("zhangsan", 35));
userEntities.add(new UserEntity("xiaowei", 16));
// userEntities.add(new UserEntity("xiaowei", 16));
// strings.add("xiaowei");
// strings.add("xiaomin");
// strings.add("xiaowei");
/**
* 创建一个串行的stream流
*/
Stream stream = userEntities.stream();
// key 为string类型 value UserEntity 集合中的数据:UserEntity , string类型
Map collect = stream.collect(Collectors.toMap(new Function() {
@Override
public String apply(UserEntity userEntity) {
return userEntity.getUserName();
}
}, new Function() {
@Override
public UserEntity apply(UserEntity userEntity) {
return userEntity;
}
}));
collect.forEach(new BiConsumer() {
@Override
public void accept(String s, UserEntity userEntity) {
System.out.println("s:" + s + ",:" + userEntity.toString());
}
});
}
}
Stream integerStream = Stream.of(10, 30, 80, 60, 10, 70);
// Optional reduce = integerStream.reduce(new BinaryOperator() {
// @Override
// public Integer apply(Integer a1, Integer a2) {
// return a1 + a2;
// }
// });
Optional reduce = integerStream.reduce((a1, a2) -> a1 + a2);
System.out.println(reduce.get());
// Optional reduce = stream.reduce(new BinaryOperator() {
// @Override
// public UserEntity apply(UserEntity userEntity, UserEntity userEntity2) {
// userEntity.setAge(userEntity.getAge() + userEntity2.getAge());
// return userEntity;
// }
// });
Optional reduce = stream.reduce((user1, user2) -> {
user1.setAge(user1.getAge() + user2.getAge());
return user1;
});
// Optional max = stream.max(new Comparator() {
// @Override
// public int compare(UserEntity o1, UserEntity o2) {
// return o1.getAge() - o2.getAge();
// }
// });
Optional max = stream.max((o1, o2) -> o1.getAge() - o2.getAge());
System.out.println(max.get());
Optional min = stream.min(((o1, o2) -> o1.getAge() - o2.getAge()));
System.out.println(min.get());
// boolean result = stream.noneMatch(new Predicate() {
// @Override
// public boolean test(UserEntity userEntity) {
// return userEntity.getAge() >35;
// }
// });
boolean result = stream.noneMatch((user) -> user.getAge() > 35);
System.out.println(result);
// stream.forEach(new Consumer() {
// @Override
// public void accept(UserEntity userEntity) {
// System.out.println(userEntity.toString());
// }
// });
stream.forEach((userEntity -> System.out.println(userEntity.toString())));
// stream.filter(new Predicate() {
// @Override
// public boolean test(UserEntity userEntity) {
// return userEntity.getAge() >= 35;
// }
// }).filter(new Predicate() {
// @Override
// public boolean test(UserEntity userEntity) {
// return userEntity.getUserName().equals("zhangsan");
// }
// }).forEach(new Consumer() {
// @Override
// public void accept(UserEntity userEntity) {
// System.out.println(userEntity.toString());
// }
// });
stream.filter((userEntity -> userEntity.getAge() >= 35)).filter(userEntity -> userEntity.equals("zhangsan"))
.forEach((userEntity -> System.out.println(userEntity.toString())));
// stream.sorted(new Comparator() {
// @Override
// public int compare(UserEntity o1, UserEntity o2) {
// return o1.getAge() - o2.getAge();
// }
// }).forEach(new Consumer() {
// @Override
// public void accept(UserEntity userEntity) {
// System.out.println(userEntity.toString());
// }
// });
stream.sorted(((o1, o2) -> o1.getAge() - o2.getAge())).forEach(userEntity -> System.out.println(userEntity.toString()));
stream.skip(2).limit(1).forEach(userEntity -> System.out.println(userEntity));
串行流:单线程的方式操作; 数据量比较少的时候。
并行流:多线程方式操作;数据量比较大的时候,原理:
Fork join 将一个大的任务拆分n多个小的子任务并行执行,
最后在统计结果,有可能会非常消耗cpu的资源,确实可以
提高效率。
注意:数据量比较少的情况下,不要使用并行流。
Integer a1 = 1;
Optional a = Optional.ofNullable(a1);
System.out.println(a.isPresent());
Integer a1 = 5;
// Optional a = Optional.ofNullable(a1);
// System.out.println(a.get());
// System.out.println(a.isPresent());
Integer a = Optional.ofNullable(a1).orElse(10);
System.out.println(a);
Integer a1 = 16;
Optional a = Optional.ofNullable(a1);
boolean isPresent = a.filter(a2 -> a2 > 17).isPresent();
System.out.println(isPresent);