您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關Java中JDK1.8lambda怎么用的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
具體代碼如下所示:
public class Student { private String id; private String name; private String age; private String address; public Student(String id, String name, String age, String address) { this.id = id; this.name = name; this.age = age; this.address = address; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Student{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", age='" + age + '\'' + ", address='" + address + '\'' + '}'; }}public class TestStrean { public static void main(String[] args){ Student stuA = new Student("1", "A", "M", "184"); Student stuB = new Student("2", "B", "G", "163"); Student stuC = new Student("3", "C", "M", "175"); Student stuD = new Student("4", "D", "G", "158"); Student stuE = new Student("5", "E", "M", "175"); List<Student> list = new ArrayList<>(); list.add(stuA); list.add(stuB); list.add(stuC); list.add(stuD); list.add(stuE); /*-----------forEach-----------------------*/// list.forEach(item -> item.setId(item.getAddress()+"test") ); /*-----------filter-----------------------*/// List<Student> newList=list.stream().filter(item -> item.getAge().contains("G")).collect(Collectors.toList()); /*-----------filter的復雜應用-----------------------*/// list.stream().filter(new Predicate<Student>(){//// @Override// public boolean test(Student student) {// if(student.getName() !=null){// return student.getName().contains("30");// }else{// return true;// }// }// }).collect(Collectors.toList()); /*-----------分頁-----------------------*/// List<Student> newList=list.stream().skip(4).limit(3).collect(Collectors.toList()); /*-----------map 返回一個新的list<Object>-----------------------*/// List<Student> newList=list.stream().map(item ->{// return new Student(// item.getAddress(),// item.getAge(),// item.getName(),// item.getId()// );// }).collect(Collectors.toList()); /*-----------map 返回一個原來的的list<Student>并賦值-----------------------*/// List<Student> newList=list.stream().map(item ->{// item.setId("1");// return item;// }).collect(Collectors.toList()); /*-----------flatMap-----------------------*/// List<Student> newList= list.stream().flatMap(item ->{// String[] split =item.getAddress().split("");// String[] s2 = (String[]) Arrays.stream(split).filter(s -> s.contains("1")).toArray();// return s2;// }); /*-----------sorted compareTo排序-----------------------*/// List<Student> newList=list.stream().sorted((o1,o2)->{// if(o1.getAddress().equals(o2.getAddress())){// return Integer.parseInt(o1.getId())-Integer.parseInt(o2.getId());// }else{// return o1.getAddress().compareTo(o2.getAddress());// }// }).collect(Collectors.toList()); /*-----------sorted Comparator排序-----------------------*/// List<Student> newList=list.stream().sorted(// Comparator.comparing(Student::getAddress).reversed()// ).collect(Collectors.toList()); /*-----------sorted3 多條件排序-----------------------*/// List<Student> newList=list.stream().sorted(// Comparator.comparing(Student::getAddress).thenComparing(Comparator.comparing(Student::getId).reversed())// ).collect(Collectors.toList());// boolean b=list.stream().anyMatch(item -> item.getAge().contains("G"));// System.out.println(b); /*-----------Collector tomap----------------------*/// Map<String, String> newList=list.stream().sorted(// Comparator.comparing(Student::getAddress).thenComparing(Comparator.comparing(Student::getId).reversed())// ).collect(Collectors.toMap(Student::getId,Student::getAddress)); /*-----------Collector groupingBy----------------------*/ Map<String, List<Student>> ageMap = list.stream().collect(Collectors.groupingBy(Student::getAddress)); System.out.println(ageMap ); }}
感謝各位的閱讀!關于“Java中JDK1.8lambda怎么用”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。