In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how to use JDK1.8lambda in Java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The specific code is as follows:
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 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 newList=list.stream () .filter (item-> item.getAge () .contains ("G")) .contains (Collectors.toList ()) / *-complex application of filter-* / list.stream () .filter (new Predicate () {/ @ Override// public boolean test (Student student) {/ / if (student.getName ()! = null) {/ / return student.getName () .contains ("30") / /} else {/ / return true;//} / /} /}) .pages (Collectors.toList ()); / *-pagination-- * / List newList=list.stream (). Skip (4) .limit (3) .limits (Collectors.toList ()) / *-map returns a new list---*/// List newList=list.stream () .map (item-> {/ / return new Student (/ / item.getAddress (), / / item.getAge (), / / item.getName ()) / / item.getId () / / / /}) .map (Collectors.toList ()); / *-map returns an original list and assigns a value-* / List newList=list.stream () .map (item-> {/ / item.setId ("1"); / / return item;//}) .requests (Collectors.toList ()) / *-flatMap---*/// List 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 sort-- * / List newList=list.stream (). Sorted ((o1Powero2)-> {/ / if (o1.getAddress (). Equals (o2.getAddress () {/ / return Integer.parseInt (o1.getId ())-Integer.parseInt (o2.getId ()) / /} else {/ / return o1.getAddress () .compareTo (o2.getAddress ()); / /} / /}) .subscription (Collectors.toList ()) / *-sorted Comparator sort-- * / List newList=list.stream () .sorted (/ / Comparator.comparing (Student::getAddress) .reversed () /) .ranking (Collectors.toList ()) / *-sorted3 multi-conditional sorting-- * / List newList=list.stream () .sorted (/ / Comparator.comparing (Student::getAddress) .thencomparing (Comparator.comparing (Student::getId) .reversed ()) / / .ranking (Collectors.toList ()) / / boolean b=list.stream () .anyMatch (item-> item.getAge () .contains ("G")); / / System.out.println (b) / *-Collector tomap--*/// Map newList=list.stream () .sorted (/ / Comparator.comparing (Student::getAddress) .thencomparing (Comparator.comparing (Student::getId) .reversed ()) / / .compare (Collectors.toMap (Student::getId,Student::getAddress)) / *-Collector groupingBy--*/ Map ageMap = list.stream (). Collect (Collectors.groupingBy (Student::getAddress)); System.out.println (ageMap);}} Thank you for reading! This is the end of the article on "how to use JDK1.8lambda in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.