目前我了解的lambda表达式一般用于遍历集合或匿名函数中
记住能用foreach的一定是集合,需要遍历哟!
lambda表达式的语法:
(parameters) -> expression或(parameters) ->{ statements; }
一:数组使用lambda表达式
注:数组里面没有foreach,所以数组要转成集合或者是流才能使用foreach
@Test
public void lambdaTest1(){
String [] str=new String[]{"k","f","a","b"};
Arrays.stream(str).forEach(System.out::println);
Arrays.asList(str).forEach(System.out::println);
// Arrays.stream(str).forEach(s->System.out.println(s));
// Arrays.asList(str).forEach(t->System.out.println(t));
//Arrays.asList(str).forEach(t->{System.out.println(t);});
}
二:集合遍历
@Test
public void findAllUserinfo(){
List userList=userinfoDao.selectList(null);
userList.forEach(System.out::println);
//userList.forEach(us->System.out.println(us));
}
三:关于匿名函数
(1)无参
//匿名内部内的方式
// new Thread(new Runnable() {
// @Override
// public void run() {
// System.out.println("我也不知道這是啥");
// }
// }).start();
//lambda的形式
//这里一个空括号,表示的是方法没有参数(传的参数个数不同表示调用的方法不同)
new Thread(()->System.out.println("这个可以代替上面那个")).start();
(2)有参(字符串的比较)
@Test
public void lambdaTest1(){
String [] str=new String[]{"k","f","a","b"};
// Arrays.sort(str, new Comparator() {
// @Override
// public int compare(String o1, String o2) {
// return o1.compareTo(o2);
// }
// });
//new Comparator() {
// @Override
// public int compare(String o1, String o2) {
// return o1.compareTo(o2);
// }相当于(String t1,String t2)->t1.compareTo(t2)
Arrays.sort(str,(String t1,String t2)->t1.compareTo(t2));
Arrays.asList(str).forEach(t->{System.out.println(t);});
}
(2)数字的比较
@Test
public void lambdaTest2(){
Integer [] integers=new Integer[]{5,9,3,2,4,7,8,};
Arrays.sort(integers,(Integer t1,Integer t2)->t1.compareTo(t2));
Arrays.asList(integers).forEach(t->{System.out.println(t);});
}
四:当stream和lambda一起使用时(个人认为stream可以为结果加很多的约束)
(1)filter()
eg:List javaProgrammers =newArrayList() {
{
add(newPerson("Elsdon","Jaycob","Java programmer","male",43,2000));
add(newPerson("Tamsen","Brittany","Java programmer","female",23,1500));
add(newPerson("Floyd","Donny","Java programmer","male",33,1800));
add(newPerson("Sindy","Jonie","Java programmer","female",32,1600));
add(newPerson("Vere","Hervey","Java programmer","male",22,1200));
add(newPerson("Maude","Jaimie","Java programmer","female",27,1900));
add(newPerson("Shawn","Randall","Java programmer","male",30,2300));
add(newPerson("Jayden","Corrina","Java programmer","female",35,1700));
add(newPerson("Palmer","Dene","Java programmer","male",33,2000));
add(newPerson("Addison","Pam","Java programmer","female",34,1300));
}
};
javaProgrammers.stream().filter(p->p.getSalary()>=30).forEach(System.out::println);
(2)limit()
javaProgrammers.stream().filter(p->p.getSalary()>=30).limit(3).forEach(System.out::println);
3sorted()
这里的排序跟上面不一样了,你注意哟!这里是对象,所以要获取属性进行排序
javaProgrammers.stream().sorted((p1,p2)->p1.getFirstName().compareTo(p2.getFirstName())).limit(3).forEach(System.out::println);
4、min() max()最大值最小值,找的是集合当中的对象。出来的时候一定要有get()方法,这样才能返回对象
Person p=javaProgrammers.stream().min((p1,p2)->p1.getSalary()-p2.getSalary()).get();
System.out.println(p.getFirstName()+p.getLastName()+p.getSalary());
Person pm=javaProgrammers.stream().max((p1,p2)->p1.getSalary()-p2.getSalary()).get();
System.out.println(pm.getFirstName()+pm.getLastName()+pm.getSalary());
5、map()和collect()一起使用
(1)collect()的joining()方法
//将对象的属性拼接成字符串(拼接后成立字符串)
System.out.println(javaProgrammers.stream().map(Person::getFirstName).collect(Collectors.joining(",")));
(2)collect()的toSet()方法
//将对象的属性放到set集合中
Set<String> strSet=javaProgrammers.stream().map(Person::getFirstName).collect(Collectors.toSet());
strSet.forEach(System.out::println);
(2)collect()的toCollection()方法
//将对象的属性放到TreeSet 集合中
TreeSet stringTreeSet=javaProgrammers.stream().map(Person::getFirstName).collect(Collectors.toCollection(TreeSet::new));
stringTreeSet.forEach(System.out::println);
注:
map(Person->Person.getLastName())=map(Person::getFirstName)
6、parallelStream()方法
System.out.println(javaProgrammers.parallelStream().mapToDouble(p->p.getSalary()).sum());//个人觉得跟下面的没区别
System.out.println(javaProgrammers.stream().mapToDouble(p->p.getSalary()).sum());
7、summaryStatistics()方法,只要是数字就可以求count, min, max, sum, and average
Integer [] integers=new Integer[]{5,58,4,1,2,6,9};
System.out.println(Arrays.stream(integers).mapToInt(p->p).summaryStatistics().getMax());//最大值
System.out.println(Arrays.stream(integers).mapToInt(p->p).summaryStatistics().getMin());//最小值
System.out.println(Arrays.stream(integers).mapToInt(p->p).summaryStatistics().getCount());//总个数
System.out.println(Arrays.stream(integers).mapToInt(p->p).summaryStatistics().getAverage());//平均数
System.out.println(Arrays.stream(integers).mapToInt(p->p).summaryStatistics().getSum());//总和
//补充:
//返回的是IntSummaryStatistics 对象
IntSummaryStatistics intSummaryStatistics=Arrays.stream(integers).mapToInt(p->p).summaryStatistics();
System.out.println(intSummaryStatistics.getMax());
System.out.println(intSummaryStatistics.getMin());
System.out.println(intSummaryStatistics.getCount());
System.out.println(intSummaryStatistics.getAverage());
System.out.println(intSummaryStatistics.getSum());
总结:(1)foreach和map用可以用lambda表达式也可以用“双冒号操作符”,用lambda表达式格式:别名->语句//这里的语句是循环内的语句
(2)匿名函数使用lambda表达式格式:(参数)->函数体//函数体就是参数所对应的方法语句
无参:()->语句
有参:(参数1,参数2,......)->语句
网友评论