We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
publicclassSolution{publicstaticvoidmain(String[]args){/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */try(BufferedReaderbIn=newBufferedReader(newInputStreamReader(System.in))){Stringline=bIn.readLine();List<Player>scoreList=newArrayList<>();while((line=bIn.readLine())!=null){scoreList.add(newPlayer(line));}ScoreComparecp=newScoreCompare();scoreList.stream().sorted(cp::compare).forEach(elem->System.out.println(elem.getName()+" "+elem.getScore()));}catch(Exceptione){//}}publicstaticclassPlayer{privateStringname;privateintscore;publicPlayer(Stringtext){String[]strArray=text.split(" ");this.name=strArray[0];this.score=Integer.valueOf(strArray[1]).intValue();}publicStringgetName(){returnthis.name;}publicvoidsetName(Stringname){this.name=name;}publicintgetScore(){returnthis.score;}publicvoidsetName(intscore){this.score=score;}}publicstaticclassScoreCompareimplementsComparator<Player>{@Overridepublicintcompare(Playerp1,Playerp2){if(p1.getScore()<p2.getScore()){return1;}elseif(p1.getScore()>p2.getScore()){return-1;}else{returnp1.getName().compareTo(p2.getName());}}}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Comparator
You are viewing a single comment's thread. Return to all comments →