java8学习整理二
生活随笔
收集整理的這篇文章主要介紹了
java8学习整理二
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
java8不但可以提高代碼的運行的性能,而且實現起來很優雅,因此學習它是不錯的選擇。
今天寫這篇文章,是因為看到我們部門大佬寫的代碼,因此將其還原成匿名內部類的時候發現這個retrun是不能省掉的,省去會報錯。同時還可以學習到map如果在篩選條件中只有一行的時候,是可以不需要return的,這個是與中間有處理過程是不同的。因此就有了下面的學習:
public List<CmPatientBasePO> listAll(Integer fkHospId, Integer fkTenantId) {CmPatient cmPatient = new CmPatient();cmPatient.setFkHospId(Objects.isNull(fkHospId) ? UserUtils.getHospId() : fkHospId);cmPatient.setFkTenantId(Objects.isNull(fkTenantId) ? UserUtils.getTenantId() : fkTenantId);List<CmPatient> cmPatientList = cmPatientPOMapper.listByCondition(cmPatient);if (CollectionUtils.isEmpty(cmPatientList)) {return Collections.emptyList();}return cmPatientList.stream().filter(Objects::nonNull).map(patient -> {CmPatientBasePO cmPatientBasePO = new CmPatientBasePO();BeanUtils.copyProperties(patient, cmPatientBasePO);cmPatientBasePO.setAliasInitials(patient.getSpellInitials());cmPatientBasePO.setPatientName(patient.getAliasName());return cmPatientBasePO; //需要返回值,此時不可省略}).collect(Collectors.toList()); }首先map和peek之間的區別和使用
map是有返回值的,而peek作為中間處理過程,其返回的值是void,這是兩者最大的區別,其次官方推薦使用map。而使用peek可以進行中間處理,方便對數據的處理。
/*** @author lyz* @date 2020/5/12 17:03**/ public class Demo {public static void main(String[] args) {Stream.of("小王:18","小楊:20").map(new Function<String, People>() {@Overridepublic People apply(String s) {String[] str = s.split(":");People people = new People(str[0],Integer.valueOf(str[1]));return people;}}).forEach(people-> System.out.println("people = " + people));} }運行結果:
people = People{name='小王', age=18} people = People{name='小楊', age=20}修改成map實現:
/**** @description: lambda學習* @author: lyz* @date: 2020/05/12 14:48**/ public class demo2 {public static void main(String[] args) {Stream.of("小王:18","小楊:20").map((String s)->{String[] str = s.split(":");People people = new People(str[0],Integer.valueOf(str[1]));return people; //此處不可省略}).forEach(people-> System.out.println("people = " + people));} }運行結果:
people = People{name='小王', age=18} people = People{name='小楊', age=20}修改成peek實現:
/**** @description: lambda學習* @author: lyz* @date: 2020/05/12 15:16**/ public class Demo5 {public static void main(String[] args) {Stream.of("小王:18","小楊:20").peek((String s)->{String[] str = s.split(":");People people = new People(str[0],Integer.valueOf(str[1])); //這里沒有return,因此返回的void}).forEach(people-> System.out.println("people = " + people));} }運行結果:
people = 小王:18 people = 小楊:20進行數據過濾
/**** @description: stream流學習* @author: lyz* @date: 2020/05/12 15:22**/ public class Demo6 {public static void main(String args[]){List<Map<String,Object>> list=new ArrayList<>();for(int i=0;i<5;i++){Map<String,Object> map=new HashMap<>();map.put("type",i);list.add(map);}System.out.println("list過濾前的數據:"+list);System.out.println("list過濾前的數量:"+list.size());//過濾獲取 type=2的數據List<Map<String,Object>> list2 = list.stream().filter((Map a) -> ("4".equals(a.get("type").toString()))).collect(Collectors.toList());//只獲取數量也可以這樣寫Long list2Count = list.stream().filter((Map a) -> ("4".equals(a.get("type").toString()))).count();System.out.println("list過濾后的數據:"+list2);System.out.println("list過濾后的數量:"+list2Count);System.out.println("list過濾后的數量:"+list2.size());} }運行結果:
list過濾前的數據:[{type=0}, {type=1}, {type=2}, {type=3}, {type=4}] list過濾前的數量:5 list過濾后的數據:[{type=4}] list過濾后的數量:1 list過濾后的數量:1進行數據過濾:
/**** @description: lambda學習 使用過濾+循環* @author: lyz* @date: 2020/05/12 15:24**/ public class Demo7 {public static void main(String[] args) {/* List<String> strArr = Arrays.asList("1", "2", "3", "4");strArr.stream().filter(str ->{return "2".equals(str)?true:false;}).forEach(str ->{System.out.println(str);System.out.println(str+1);});*/List<String> strArr = Arrays.asList("1", "2", "3", "4");strArr.stream().filter(str -> "2".equals(str)?true:false).forEach(str ->{System.out.println(str);System.out.println(str+1);});}}運行結果:
2 21基于扁平流實現:
/**** @description: lambda學習 扁平流使用* @author: lyz* @date: 2020/05/12 16:00**/ public class Demo12 {public static void main(String[] args) {List<Student> students = new ArrayList<>(4);students.add(new Student("百花", 22, 175));students.add(new Student("白條", 40, 178));students.add(new Student("略一", 40, 180));students.add(new Student("暢享", 50, 185));List<Student> list = students.stream().filter(stu -> stu.getStature() < 180).collect(Collectors.toList());System.out.println(list);List<Student> students1 = new ArrayList<>();students1.add(new Student("拉普斯", 22, 175));students1.add(new Student("洛夫斯基", 40, 178));List<Student> studentList = Stream.of(students, students1).flatMap(stu -> stu.stream()).collect(Collectors.toList());System.out.println(studentList);System.out.println("==============================");Optional<Student> max = students.stream().max(Comparator.comparing(stu -> stu.getAge()));Optional<Student> min = students.stream().min(Comparator.comparing(stu -> stu.getAge()));//判斷是否有值if (max.isPresent()) {System.out.println(max.get());}if (min.isPresent()) {System.out.println(min.get());}long count = students.stream().filter(s1 -> s1.getAge() < 45).count();System.out.println("年齡小于42歲的人數是:" + count);}運行結果:
[Student{name='百花', age=22, stature=175}, Student{name='白條', age=40, stature=178}] [Student{name='百花', age=22, stature=175}, Student{name='白條', age=40, stature=178}, Student{name='略一', age=40, stature=180}, Student{name='暢享', age=50, stature=185}, Student{name='拉普斯', age=22, stature=175}, Student{name='洛夫斯基', age=40, stature=178}] ============================== Student{name='暢享', age=50, stature=185} Student{name='百花', age=22, stature=175} 年齡小于42歲的人數是:3進行字符串拼接
/**** @description: 學習java8,字符串拼接* @author: lyz* @date: 2020/05/12 16:23**/ public class Demo13 {public static void main(String[] args) {List<Student> students = new ArrayList<>(3);students.add(new Student("綠衣", 22, 175));students.add(new Student("紅妝", 40, 180));students.add(new Student("水月", 50, 185));String names = students.stream().map(Student::getName).collect(Collectors.joining(",","[","]"));System.out.println(names);} }運行結果:
[綠衣,紅妝,水月]進行分組操作
/**** @description: 進行lambda學習,進行分組操作* @author: lyz* @date: 2020/05/12 16:43**/ public class Demo15 {public static void main(String[] args) {List<User> users = Lists.newArrayList(new User("高三1班", "stu01", "男"),new User("高三1班", "stu02", "女"),new User("高三2班", "stu11", "男"),new User("高三2班", "stu12", "女"),new User("高三3班", "stu21", "女"),new User("高三3班", "stu22", "男"),new User("高三3班", "stu23", "女"));Map<String, List<User>> collect = users.stream().collect(groupingBy(User::getClassName));System.out.println(JSON.toJSONString(collect));System.out.println();Map<String, Long> collect1 = users.stream().collect(groupingBy(User::getClassName, Collectors.counting()));System.out.println(JSON.toJSONString(collect1));System.out.println();Map<String, Map<String, User>> collect2 = users.stream().collect(groupingBy(User::getClassName, Collectors.toMap(User::getStudentName, o -> o)));System.out.println(JSON.toJSONString(collect2));System.out.println();}private static class User {private String className;private String studentName;private String sex;//省略get/set與構造函數} }結果:
{"高三3班":[{"className":"高三3班","sex":"女","studentName":"stu21"},{"className":"高三3班","sex":"男","studentName":"stu22"},{"className":"高三3班","sex":"女","studentName":"stu23"}],"高三2班":[{"className":"高三2班","sex":"男","studentName":"stu11"},{"className":"高三2班","sex":"女","studentName":"stu12"}],"高三1班":[{"className":"高三1班","sex":"男","studentName":"stu01"},{"className":"高三1班","sex":"女","studentName":"stu02"}]}{"高三3班":3,"高三2班":2,"高三1班":2}{"高三3班":{"stu21":{"className":"高三3班","sex":"女","studentName":"stu21"},"stu23":{"className":"高三3班","sex":"女","studentName":"stu23"},"stu22":{"className":"高三3班","sex":"男","studentName":"stu22"}},"高三2班":{"stu12":{"className":"高三2班","sex":"女","studentName":"stu12"},"stu11":{"className":"高三2班","sex":"男","studentName":"stu11"}},"高三1班":{"stu02":{"className":"高三1班","sex":"女","studentName":"stu02"},"stu01":{"className":"高三1班","sex":"男","studentName":"stu01"}}}總結
以上是生活随笔為你收集整理的java8学习整理二的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信小程序多次跳转后不能点_微信突然更新
- 下一篇: Unity鼠标拖拽旋转拉远拉近场景