1. Stream
Case:4
public static void main(String[] args) { List<String> citiesl =new ArrayList<>(); Collections.addAll(citiesl,"Nanjing City","Yangzhou City","Nanyang City","Changzhou City"); System.out.println(citiesl);//[Nanjing City, Yangzhou City, Nanyang City, Changzhou City] //Method 1: Original method List<String> citiesls =new ArrayList<>(); for (String s : citiesl) { if (s.startsWith("南")){ citiesls.add(s); } } System.out.println(citiesls);//[Nanjing City, Nanyang City] //Method 2: Stream stream List<String>ciiesl2=citiesl.stream().filter(c->c.startsWith("南")).collect(Collectors.toList()); System.out.println(ciiesl2); }
2. Use of Stream stream
3. Case
public static void main(String[] args) { //1, How to get the Stream stream of the List collection List<String> citiesl =new ArrayList<>(); Collections.addAll(citiesl,"Nanjing City","Yangzhou City","Nanyang City","Changzhou City"); System.out.println(citiesl); Stream<String> stream =citiesl.stream(); //2, How to obtain the Stream stream of the Set collection Set<String> set =new HashSet<>(); Collections.addAll(set,"Nanjing City","Yangzhou City","Nanyang City","Changzhou City"); System.out.println(set); Stream<String> stream1 =set.stream(); //3, How to obtain the Stream stream of the Map collection Map<String,Integer> map =new HashMap<>(); map.put("coin",100); map.put("Like",10); map.put("Collection",20); map.put("三连",1); map.put(null,null); System.out.println(map); Set<String> keySet =map.keySet(); Stream<String> ks =keySet.stream(); Collection<Integer> values =map.values(); Stream<Integer> vs =values.stream(); Set<Map.Entry<String, Integer>> entries = map.entrySet(); Stream<Map.Entry<String, Integer>> stream2 = entries.stream(); stream2.filter(e ->e.getKey().contains("coin")) .forEach(e ->System.out.println(e.getKey() + "-->" + e.getValue())); //3, How to get the Stream stream of the array String[] strings ={"A","B","C","D"}; Stream<String> s1 =Arrays.stream(strings); Stream<String> s2 =Stream.of(strings); }
4. Commonly used methods (key points)
Case:
? ? public static void main(String[] args) { List<Double> scores =new ArrayList<>(); Collections.addAll(scores,88.2,95.3,12.5,65.7,99.5,44.2,35.6); //Method 1, find data greater than or equal to 60, sort it, and then output it scores.stream().filter(s -> s >=60).sorted().forEach(s-> System.out.println(s)); List<Shudent> shudents =new ArrayList<>(); Shudent s1 =new Shudent("Spider Spirit",26,160.0); Shudent s2 =new Shudent("Supreme Treasure",25,176.0); Shudent s3 =new Shudent("White Bone Essence",27,162.0); Shudent s4 =new Shudent("Zixia",18,170.0); Shudent s5 =new Shudent("牛魔王",68,180.0); Collections.addAll(shudents,s1,s2,s3,s4,s5); //Method 2, find the data whose age is greater than or equal to 23 and less than 30, sort it, and then output it shudents.stream().filter(s -> s.getAge() >=23 & & s.getAge()<=30) .sorted((o1,o2) -> o2.getAge() - o1.getAge()) .forEach(s-> System.out.println(s)); System.out.println("-----------------------------------------") ; //Method 3, find the 3 oldest students and then output shudents.stream().sorted((o1,o2) -> Double.compare(o2.getHeight(),o1.getHeight())) .limit(3).forEach(s-> System.out.println(s)); System.out.println("-----------------------------------------") ; //Method 4, find the three youngest students and output them shudents.stream().sorted((o2,o1) -> Double.compare(o1.getHeight(),o2.getHeight())) .skip(shudents.size()-2).forEach(s-> System.out.println(s)); System.out.println("-----------------------------------------") ; //Method 5, find the names of several students whose height is over 170.0, remove duplicates, and then output shudents.stream().filter(s -> s.getHeight() >=170.0).map(s->s.getNane()) .distinct() .forEach(s-> System.out.println(s)); } ? ? ?
package Map1; public class Shudent { private String nane; private int age; private double height; @Override public String toString() { return "Shudent{" + "nane='" + nane + ''' + ", age=" + age + ", height=" + height + '}'; } public Shudent(String nane, int age, double height) { this.nane = nane; this.age = age; this.height = height; } public Shudent() { } public String getNane() { return nane; } public void setNane(String nane) { this.nane = nane; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } }
5. Common termination methods in Stream streams
Case:
age Map1; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class MapTex6 { public static void main(String[] args) { List<Shudent> shudents =new ArrayList<>(); Shudent s1 =new Shudent("Spider Spirit",26,160.0); Shudent s2 =new Shudent("Supreme Treasure",25,176.0); Shudent s3 =new Shudent("White Bone Essence",27,162.0); Shudent s4 =new Shudent("Zixia",18,170.0); Shudent s5 =new Shudent("牛魔王",68,180.0); Collections.addAll(shudents,s1,s2,s3,s4,s5); //Method 1, calculate the number of students whose height exceeds 175.0, and output long size = shudents.stream().filter(s -> s.getHeight() >=175.0).count(); System.out.println(size); //Method 2, calculate the tallest student and output Shudent s =shudents.stream().max((o2,o1) -> Double .compare(o1.getHeight(),o2.getHeight())).get(); System.out.println(s); //Method 3, calculate the student with the lowest height and output Shudent ss =shudents.stream().min((o2,o1) -> Double .compare(o1.getHeight(),o2.getHeight())).get(); System.out.println(ss); //Method 4, calculate students whose height exceeds 175.0 and put them into a new collection List <Shudent>shudents1=shudents.stream().filter(a -> a.getHeight() >=175.0) .collect(Collectors.toList()); System.out.println(shudents1); //Method 4, calculate students whose height exceeds 175.0. And put the name and height of the student object into a new collection Map<String,Double>map=shudents.stream().filter(a -> a.getHeight() >=175.0) .collect(Collectors.toMap(a->a.getNane(), a->a.getHeight())); System.out.println(map);