您好朋友,在本文中,我们将讨论Java 8 Stream API示例。
因此,我们要做的是,创建一个Player类,创建Player类的多个对象,然后创建一个测试类,在其中使用Stream API检索播放器的数据。
第一个例子是普通流(我们可以称之为串行流),第二个例子是并行流。
串行流
第一步,让我们创建一个Player类。
public class Player {private String name;private int id;private int fees;private String grade;public Player(String name, int id, int fees, String grade) {super();this.name = name;this.id = id;this.fees = fees;this.grade = grade;}/*** @return the name*/public String getName() {return name;}/*** @param name the name to set*/public void setName(String name) {this.name = name;}/*** @return the id*/public int getId() {return id;}/*** @param id the id to set*/public void setId(int id) {this.id = id;}/*** @return the fees*/public int getFees() {return fees;}/*** @param fees the fees to set*/public void setFees(int fees) {this.fees = fees;}/*** @return the grade*/public String getGrade() {return grade;}/*** @param grade the grade to set*/public void setGrade(String grade) {this.grade = grade;}@Overridepublic String toString() {return "Player [name=" + name + ", id=" + id + ", fees=" + fees + ", grade=" + grade + "]";}
}
现在,让我们创建一个测试类来测试Stream API。
public class TestJava8Streams {public static void main(String[] args) {List players = createPlayerList();/**********STREAM****************/System.out.println("STREAM");/****Get List of Players Who are in Grade AA***/System.out.println("Grade AA Players :");players.stream().filter((Player player1)-> player1.getGrade().equalsIgnoreCase("AA")).collect(Collectors.toList()).forEach(System.out::println);/*********************************************/ /********Get Fee of Grade AA Players**********/System.out.println("Fee Grade AA Players :");players.stream().filter((Player player1)-> player1.getGrade().equalsIgnoreCase("AA")).map(p->p.getFees()).collect(Collectors.toList()).forEach(System.out::println);;/*********************************************//********Are All players getting Match Fee of more than 3 Crore*****/System.out.println("Does All Players getting Match Fee of more than 3 Crores:");boolean isAllPlayerHavingFeeMoreThansThreeCrore1 = players.stream().allMatch(player1 -> player1.getFees()>=30000000);System.out.println(isAllPlayerHavingFeeMoreThansThreeCrore1 ? "Yes": "No");/*******************************************************************//********Are All players getting Match Fee of more than 1 Crore*****/System.out.println("Does All Players getting Match Fee equal to or more than 1 Crore:");boolean isAllPlayerHavingFeeMoreThansOneCrore1 = players.stream().allMatch(player1 -> player1.getFees()>=10000000);System.out.println(isAllPlayerHavingFeeMoreThansOneCrore1 ? "Yes": "No");/*******************************************************************//*************Get Players with their Name and Grade******************/players.stream().collect(Collectors.toMap((Player player1)-> player1.getName(), (Player player1)-> player1.getGrade()));players.forEach(player1 -> System.out.println("Player Name:"+player1.getName() + " | " + "Player Grade:"+player1.getGrade()));/*******************************************************************//*************Get any player detail.As per java docs ,multiple invocations can return different results******/Optional player1 =players.stream().findAny();System.out.println("Any Player:"+player1.get());/*******************************************************************//********Get First Player in the list*****/players.stream().findFirst().get().getName();System.out.println("First Player in the list:"+player1.get());/****************************************//********Get First Two Players from the list******/System.out.println("First two Players in the list:");players.stream().limit(2).collect(Collectors.toList()).forEach(System.out::println);;/************************************************//********Player whose name alphabetically comes Last in the list******/Optional alphabeticallyLast1 = players.stream().max((Player st1,Player st2)-> st1.getName().compareToIgnoreCase(st2.getName()));System.out.println("Alphabetically last Player:"+alphabeticallyLast1.get().getName());/************************************************//********Player whose name alphabetically comes First in the list******/Optional alphabeticallyFirst1 = players.stream().min((Player st1,Player st2)-> st1.getName().compareToIgnoreCase(st2.getName()));System.out.println("Alphabetically first Player::"+alphabeticallyFirst1.get().getName());/**********************************************************************/}private static List createPlayerList(){List palyers = new ArrayList();Player player2 = new Player("Dhawan",101,70000000,"AA");Player player1 = new Player("Rohit",102,70000000,"AA");Player player3 = new Player("Rahane",201,50000000,"A");Player player4 = new Player("Dhoni",202,50000000,"A");Player player5 = new Player("Raina",301,10000000,"C");Player player6 = new Player("Kohli",302,70000000,"AA");Player player7 = new Player("Jadeja",401,50000000,"A");Player player8 = new Player("Bumrah",402,70000000,"AA");Player player9 = new Player("Umesh",403,30000000,"B");Player player10 = new Player("Karthik",404,30000000,"B");Player player11 = new Player("Pandey",405,10000000,"C");palyers.add(player1);palyers.add(player2);palyers.add(player3);palyers.add(player4);palyers.add(player5);palyers.add(player6);palyers.add(player7);palyers.add(player8);palyers.add(player9);palyers.add(player10);palyers.add(player11);return palyers;}
}
输出是:
Grade AA Players :Player [name=Rohit, id=102, fees=70000000, grade=AA, addresses=null]Player [name=Dhawan, id=101, fees=70000000, grade=AA, addresses=null]Player [name=Kohli, id=302, fees=70000000, grade=AA, addresses=null]Player [name=Bumrah, id=402, fees=70000000, grade=AA, addresses=null]Fee Grade AA Players :70000000700000007000000070000000Does All Players getting Match Fee of more than 3 Crores:falseDoes All Players getting Match Fee equal to or more than 1 Crore:truePlayer Name:Rohit | Player Grade:AAPlayer Name:Dhawan | Player Grade:AAPlayer Name:Rahane | Player Grade:APlayer Name:Dhoni | Player Grade:APlayer Name:Raina | Player Grade:CPlayer Name:Kohli | Player Grade:AAPlayer Name:Jadeja | Player Grade:APlayer Name:Bumrah | Player Grade:AAPlayer Name:Umesh | Player Grade:BPlayer Name:Karthik | Player Grade:BPlayer Name:Pandey | Player Grade:CAny Player:Player [name=Rohit, id=102, fees=70000000, grade=AA, addresses=null]First Player in the list:Player [name=Rohit, id=102, fees=70000000, grade=AA, addresses=null]First two Players in the list:Player [name=Rohit, id=102, fees=70000000, grade=AA, addresses=null]Player [name=Dhawan, id=101, fees=70000000, grade=AA, addresses=null]Sorted List of PLayers according to their name in Alphabetical order:Player [name=Bumrah, id=402, fees=70000000, grade=AA, addresses=null]Player [name=Dhawan, id=101, fees=70000000, grade=AA, addresses=null]Player [name=Dhoni, id=202, fees=50000000, grade=A, addresses=null]Player [name=Jadeja, id=401, fees=50000000, grade=A, addresses=null]Player [name=Karthik, id=404, fees=30000000, grade=B, addresses=null]Player [name=Kohli, id=302, fees=70000000, grade=AA, addresses=null]Player [name=Pandey, id=405, fees=10000000, grade=C, addresses=null]Player [name=Rahane, id=201, fees=50000000, grade=A, addresses=null]Player [name=Raina, id=301, fees=10000000, grade=C, addresses=null]Player [name=Rohit, id=102, fees=70000000, grade=AA, addresses=null]Player [name=Umesh, id=403, fees=30000000, grade=B, addresses=null]Alphabetically last Player:UmeshAlphabetically first Player::Bumrah
并行流
以下是使用并行流的相同示例
下面的代码是完全一样的,除了我们将调用parallelStream()方法而不是stream()方法。
public class TestJava8ParallelStream {public static void main(String[] args) {List players = createPlayerList();/****PARALLEL STREAM****************/System.out.println("PARALLEL STREAM");/****Get List of Players Who are in Grade AA***/System.out.println("Grade AA Players :");players.parallelStream().filter((Player player1)-> player1.getGrade().equalsIgnoreCase("AA")).collect(Collectors.toList()).forEach(System.out::println);/*********************************************/ /********Get Fee of Grade AA Players**********/System.out.println("Fee Grade AA Players :");players.parallelStream().filter((Player player1)-> player1.getGrade().equalsIgnoreCase("AA")).map(p->p.getFees()).collect(Collectors.toList()).forEach(System.out::println);;/*********************************************//********Are All players getting Match Fee of more than 3 Crore*****/System.out.println("Does All Players getting Match Fee of more than 3 Crores:");boolean isAllPlayerHavingFeeMoreThansThreeCrore1 = players.parallelStream().allMatch(player1 -> player1.getFees()>=30000000);System.out.println(isAllPlayerHavingFeeMoreThansThreeCrore1 ? "Yes": "No");/*******************************************************************//********Are All players getting Match Fee of more than 1 Crore*****/System.out.println("Does All Players getting Match Fee equal to or more than 1 Crore:");boolean isAllPlayerHavingFeeMoreThansOneCrore1 = players.parallelStream().allMatch(player1 -> player1.getFees()>=10000000);System.out.println(isAllPlayerHavingFeeMoreThansOneCrore1 ? "Yes": "No");/*******************************************************************//*************Get Players with their Name and Grade******************/players.parallelStream().collect(Collectors.toMap((Player player1)-> player1.getName(), (Player player1)-> player1.getGrade()));players.forEach(player1 -> System.out.println("Player Name:"+player1.getName() + " | " + "Player Grade:"+player1.getGrade()));/*******************************************************************//*************Get any player detail.As per java docs ,multiple invocations can return different results******/Optional player1 =players.parallelStream().findAny();System.out.println("Any Player:"+player1.get());/*******************************************************************//********Get First Player in the list*****/players.parallelStream().findFirst().get().getName();System.out.println("First Player in the list:"+player1.get());/****************************************//********Get First Two Players from the list******/System.out.println("First two Players in the list:");players.parallelStream().limit(2).collect(Collectors.toList()).forEach(System.out::println);;/************************************************//********Player whose name alphabetically comes Last in the list******/Optional alphabeticallyLast1 = players.parallelStream().max((Player st1,Player st2)-> st1.getName().compareToIgnoreCase(st2.getName()));System.out.println("Alphabetically last Player:"+alphabeticallyLast1.get().getName());/************************************************//********Player whose name alphabetically comes First in the list******/Optional alphabeticallyFirst1 = players.parallelStream().min((Player st1,Player st2)-> st1.getName().compareToIgnoreCase(st2.getName()));System.out.println("Alphabetically first Player::"+alphabeticallyFirst1.get().getName());/**********************************************************************/}private static List createPlayerList(){List palyers = new ArrayList();Player player2 = new Player("Dhawan",101,70000000,"AA");Player player1 = new Player("Rohit",102,70000000,"AA");Player player3 = new Player("Rahane",201,50000000,"A");Player player4 = new Player("Dhoni",202,50000000,"A");Player player5 = new Player("Raina",301,10000000,"C");Player player6 = new Player("Kohli",302,70000000,"AA");Player player7 = new Player("Jadeja",401,50000000,"A");Player player8 = new Player("Bumrah",402,70000000,"AA");Player player9 = new Player("Umesh",403,30000000,"B");Player player10 = new Player("Karthik",404,30000000,"B");Player player11 = new Player("Pandey",405,10000000,"C");palyers.add(player1);palyers.add(player2);palyers.add(player3);palyers.add(player4);palyers.add(player5);palyers.add(player6);palyers.add(player7);palyers.add(player8);palyers.add(player9);palyers.add(player10);palyers.add(player11);return palyers;}
}
这是带有并行流的输出:
Grade AA Players :Player [name=Rohit, id=102, fees=70000000, grade=AA, addresses=null]Player [name=Dhawan, id=101, fees=70000000, grade=AA, addresses=null]Player [name=Kohli, id=302, fees=70000000, grade=AA, addresses=null]Player [name=Bumrah, id=402, fees=70000000, grade=AA, addresses=null]Fee Grade AA Players :70000000700000007000000070000000Does All Players getting Match Fee of more than 3 Crores:falseDoes All Players getting Match Fee equal to or more than 1 Crore:truePlayer Name:Rohit | Player Grade:AAPlayer Name:Dhawan | Player Grade:AAPlayer Name:Rahane | Player Grade:APlayer Name:Dhoni | Player Grade:APlayer Name:Raina | Player Grade:CPlayer Name:Kohli | Player Grade:AAPlayer Name:Jadeja | Player Grade:APlayer Name:Bumrah | Player Grade:AAPlayer Name:Umesh | Player Grade:BPlayer Name:Karthik | Player Grade:BPlayer Name:Pandey | Player Grade:CAny Player:Player [name=Rohit, id=102, fees=70000000, grade=AA, addresses=null]First Player in the list:Player [name=Rohit, id=102, fees=70000000, grade=AA, addresses=null]First two Players in the list:Player [name=Rohit, id=102, fees=70000000, grade=AA, addresses=null]Player [name=Dhawan, id=101, fees=70000000, grade=AA, addresses=null]Alphabetically last Player:UmeshAlphabetically first Player::Bumrah
以上就是Java 8 Stream API示例。 如果发现有帮助,请与您分享对您有帮助的人。
翻译自: https://www.javacodegeeks.com/2018/07/java-8-stream-api-examples.html