一、前言
在上一篇文章中已经完成了Lambda表达式语法和使用。
lambda表达式高级应用
二、lambda表达式高级使用
- 静态方法的使用
- 实例方法的使用
- 抽象方法的使用,绑定函数式接口
public class Test {
/**
* 1.静态方法引用
* 类型名称::方法名称
*/
public static void main(String[] args) {
List<Person> peopleList = new ArrayList<>();
peopleList.add(new Person("tom","男",15));
peopleList.add(new Person("jarry","女",16));
peopleList.add(new Person("mina","男",32));
peopleList.add(new Person("lilei","女",26));
peopleList.add(new Person("hanmeimei","男",32));
//1.匿名内部类
Collections.sort(peopleList, new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
return o1.getAge() - o2.getAge();
}
});
//System.out.println(peopleList);
//2.lamda表达式
Collections.sort(peopleList,(p1,p2)->p1.getAge()-p2.getAge());
//System.out.println(peopleList);
//3.静态方发的引用
Collections.sort(peopleList,Person::compareByAge);
//System.out.println(peopleList);
//4.实例方法的使用
PersonUtil personUtil = new PersonUtil();
Collections.sort(peopleList,personUtil::compareByName);
System.out.println(peopleList);
//5.抽象方法的使用,绑定函数式接口
Iperson iperson = Person::new;
Person person = iperson.initPerson("hm", "nan", 13);
System.out.println(person);
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
class Person{
private String name;
private String gender;
private int age;
public static int compareByAge(Person p1,Person p2){
return p1.getAge() - p2.getAge();
}
}
class PersonUtil{
//实例方法
public int compareByName(Person p1,Person p2){
return p1.getName().hashCode() - p2.getName().hashCode();
}
}
interface Iperson{
Person initPerson(String name,String gender,int age);
}
三 Stream操作List对象demo
我们以前操作list对象多使用for或者迭代器模式,现在加上java8 stream处理
public class Test {
public static void main(String[] args) {
//1. 数据
List<String> accounts = new ArrayList<>();
accounts.add("tom");
accounts.add("jerry");
accounts.add("beita");
accounts.add("shuke");
accounts.add("damu");
//1.1 业务要求:长度大于等于5的为有效账号
for(String account : accounts){
if(account.length()>=5){
System.out.println("有效账号为: " + account);
}
}
//1.2 迭代器方式
Iterator<String> iterator = accounts.iterator();
while (iterator.hasNext()){
if(iterator.next().length()>=5){
System.out.println("it 有效账号为: " + iterator.next());
}
}
//1.3 stream结合lambda表达式
List<String> list = accounts.stream().filter(s -> s.length() >= 5).collect(Collectors.toList());
System.out.println(list.toString());
}
}
四 stream API使用
4.1 各种对象转成stream对象
stream使用第一步就是把list,array,set,map等对象转成stream对象
//1. 批量数据 -> stream对象
//多个数据
Stream<String> stream = Stream.of("1", "2", "3");
//数组
String [] array = new String[]{"xueqi","biyao"};
Stream stream2 = Arrays.stream(array);
//列表
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
Stream<String> stream3 = list.stream();
//集合
Set<String> set = new HashSet<>();
set.add("1");
set.add("2");
set.add("3");
Stream<String> stream1 = set.stream();
//map
Map<String,Integer> map = new HashMap<>();
map.put("tom",1000);
map.put("jerry",1200);
map.put("lilie",1000);
Stream<Map.Entry<String, Integer>> stream4 = map.entrySet().stream();
//stream对象对基本类型数据功能封装
IntStream.of(new int[]{10,20,30}).forEach(System.out::println);
IntStream.range(1,5).forEach(System.out::println);
IntStream.rangeClosed(1,5).forEach(System.out::println);
网友评论