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.
importjava.util.Map;importjava.util.Scanner;importjava.util.stream.Collectors;importjava.util.ArrayList;importjava.util.HashMap;publicclassSolution{publicstaticvoidmain(String[]args){/* * Enter your code here. Read input from STDIN. Print output to STDOUT. Your * class should be named Solution. */Scannersc=newScanner(System.in);intsize=sc.nextInt();sc.nextLine();varlist=newArrayList<Map<String,Integer>>();for(inti=0;i<size;i++){String[]str=sc.nextLine().split(" ");varmap=newHashMap<String,Integer>();map.put(str[0],Integer.parseInt(str[1]));list.add(map);}varc=list.stream().sorted((a,b)->{Stringak=a.keySet().iterator().next();Stringbk=b.keySet().iterator().next();Integerav=a.get(ak);Integerbv=b.get(bk);intre=bv.compareTo(av);intre2=ak.compareTo(bk);if(re==0){returnre2;}else{returnre;}}).collect(Collectors.toList());c.forEach(i->{System.out.println(i.keySet().iterator().next()+" "+i.values().iterator().next());});}}
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 →