Created
September 21, 2023 10:42
-
-
Save qdongxu/b90a66d6ede4f2775f57e5f9035d6927 to your computer and use it in GitHub Desktop.
Revisions
-
qdongxu created this gist
Sep 21, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,61 @@ package com.example; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class CompetitionMain { private static List<Competition> competList = new ArrayList<>(); public static void main(String[] args) { competList.add(new CompetitionFoot(18, "footA")); competList.add(new CompetitionFoot(30, "footA")); competList.add(new CompetitionFoot(40, "footA")); competList.add(new CompetitionHand(18, "handA")); competList.add(new CompetitionHand(30, "handA")); competList.add(new CompetitionHand(40, "handA")); List<Competition> ageList = findByAge(competList, 18); List<Competition> nameList = findByName(ageList, "footA"); for (Competition competition : nameList) { System.out.println(competition.name); } } public static List<Competition> findByAge(List<Competition> competitions, int age) { return competitions.stream().filter(competition -> competition.age == age).collect(Collectors.toList()); } public static List<Competition> findByName(List<Competition> competitions, String name) { return competitions.stream().filter(competition -> competition.name.equals(name)).collect(Collectors.toList()); } } abstract class Competition { public int age; public String name; public Competition(int age, String name) { this.age = age; this.name = name; } } class CompetitionFoot extends Competition { public CompetitionFoot(int age, String name) { super(age, name); } // additional fields } class CompetitionHand extends Competition { public CompetitionHand(int age, String name) { super(age, name); } // additional fields }