java8新特性学习笔记
程序员文章站
2022-05-15 19:22:08
...
package com.abing; import org.apache.commons.lang.StringUtils; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; /** * Created on 18/7/31 by bingyin.gby. */ public class Java8Test { public static void main(String[] args) { //将1个判断输入值字符串否为空,非空则输出,否则赋予默认值 String s=null; String brandId= Optional.ofNullable(s).orElse("123456"); List<String> names= Arrays.asList("bingyingao","tieyi"); //遍历集合得到一个新的集合 List<String> newNames= names.stream().filter(b-> StringUtils.contains(b,"tieyi")).map(a->"prefix"+a).collect( Collectors.toList()); System.out.println("newNames:"); newNames.forEach(System.out::println); //遍历原集合并且对原集合中对象进行操作??? names.stream().map(a->a+"sufix"); System.out.println("names:"); names.forEach(System.out::println); new Java8Test().resetUserName(); //定义function函数并且调用:list转set Function<List<String>,Set<String>> listToMap= list->list.stream().collect(Collectors.toSet()); Set<String> names1=listToMap.apply(Arrays.asList("tieyi","tieyi","bingyingao")); System.out.println("listToSet:"); names1.forEach(System.out::println); //所有用户的年龄进行求和,reduce new Java8Test().sumUserAge(); //所有用户的年龄最大、最小、平均,求sum new Java8Test().intSummaryStatistics(); //根据年龄排序 new Java8Test().sortAgeAsc(); //遍历map集合 Map<String,String> classUnit=new HashMap<String,String>(){{ put("class1","stu1,stu2"); put("class2","stu2,stu3"); }}; classUnit.forEach((k,v)->System.out.println(k+":"+v)); //遍历int数组 int[] luckNums={0,8,5,6,7}; Arrays.stream(luckNums).forEach(System.out::println); //遍历字符串数组 String[] charsArrays="a,c,d,b".split(","); Arrays.sort(charsArrays); Arrays.asList(charsArrays).stream().forEach(a->System.out.println(a)); //flatmap的用法 // java8遍历map map.forEach((key, val) -> System.out.println(key + "-->" + val)); } /** * 给用户名重设值 */ private void resetUserName(){ List<Java8Test.UserInfo> userInfoList= Arrays.asList(new Java8Test.UserInfo("tieyi"),new Java8Test.UserInfo("bingyingao")); userInfoList.forEach(u->u.setName(u.getName()+"suFix")); userInfoList.forEach(a->System.out.println(a.getName())); } /** * 累加用户年龄 */ private void sumUserAge(){ List<Java8Test.UserInfo> userInfoList= Arrays.asList(new Java8Test.UserInfo("tieyi",30),new Java8Test.UserInfo("bingyingao",31)); Integer sumAge= userInfoList.stream().map(a->a.getAge()).reduce((a,b)->a=a+b).get(); System.out.println(sumAge); } /** * 最佳统计方法 */ private void intSummaryStatistics(){ List<Java8Test.UserInfo> userInfoList= Arrays.asList(new Java8Test.UserInfo("tieyi",30),new Java8Test.UserInfo("bingyingao",31)); IntSummaryStatistics intSummaryStatistics= userInfoList.stream().mapToInt(a->a.getAge()).summaryStatistics(); System.out.println(intSummaryStatistics.getAverage()); System.out.println(intSummaryStatistics.getCount()); System.out.println(intSummaryStatistics.getMax()); System.out.println(intSummaryStatistics.getMin()); System.out.println(intSummaryStatistics.getSum()); } /** * 根据用户年龄由低到高排序 */ private void sortAgeAsc(){ List<Java8Test.UserInfo> userInfoList= Arrays.asList(new Java8Test.UserInfo("tieyi",30),new Java8Test.UserInfo("bingyingao",31),new Java8Test.UserInfo("abing",29)); //由低到高正序 userInfoList.sort(Comparator.comparingInt(Java8Test.UserInfo::getAge)); //由高到低正序 userInfoList.sort(Comparator.comparingInt(Java8Test.UserInfo::getAge).reversed()); userInfoList.forEach(a->System.out.println(a.getAge())); } #list转为map private void list2Map(){ Map<Long,TppItem> tppItemMap=items.stream().collect(Collectors.toMap(TppItem::getItemId,v->v,(k1,k2)->k1)); } public class UserInfo{ String name =""; int age; UserInfo(String name ){ this.name=name; } UserInfo(int age){ this.age=age; } UserInfo(String name,int age){ this.name=name; this.age=age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } }
上一篇: Web安全前端常见问题以及修复方法