Untitled
user_6122548
plain_text
a year ago
3.7 kB
4
Indexable
Never
import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; public class Exercise_02_ja { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // ArrayList<String> listaImena = (ArrayList) Arrays.stream(sc.nextLine().split(", ")).collect(Collectors.toList()); ArrayList<String> listaImena = new ArrayList<>(); for (String ime : sc.nextLine().split(", ")){ listaImena.add(ime); } HashMap<String, Integer> takmicariIvrednosti = new HashMap<>(); for (String ime : listaImena) { takmicariIvrednosti.put(ime, 0); } Pattern paternSlova = Pattern.compile("[a-zA-Z]"); Pattern paternBrojevi = Pattern.compile("[0-9]"); while (true) { String unos = sc.nextLine(); if (unos.equals("end of race")) break; Matcher mtcSlova = paternSlova.matcher(unos); StringBuilder sbSlova = new StringBuilder(); while (mtcSlova.find()) { String trenutnoSlovo = mtcSlova.group(); sbSlova.append(trenutnoSlovo); } Matcher mtcBrojevi = paternBrojevi.matcher(unos); StringBuilder sbBrojevi = new StringBuilder(); int suma = 0; while (mtcBrojevi.find()) { String trenutnaCifra = mtcBrojevi.group(); suma = suma + Integer.parseInt(trenutnaCifra); sbBrojevi.append(trenutnaCifra); } String kljucIme = sbSlova.toString(); if (takmicariIvrednosti.containsKey(kljucIme)) { int vrednostTrkaca = takmicariIvrednosti.get(kljucIme); takmicariIvrednosti.put(kljucIme, vrednostTrkaca + suma); } } //List<Map.Entry<String, Integer>> sort = takmicariIvrednosti.entrySet() // .stream() // .sorted(Map.Entry.<String, Integer>comparingByValue().reversed()) // .collect(Collectors.toList()); List<Map.Entry<String, Integer>> sort = takmicariIvrednosti.entrySet() .stream().sorted(Map.Entry.<String, Integer>comparingByValue().reversed()) .collect(Collectors.toList()); System.out.printf("1st place: %s\n", sort.get(0).getKey()); System.out.printf("2nd place: %s\n", sort.get(1).getKey()); System.out.printf("3rd place: %s\n", sort.get(2).getKey()); //List <String> sort = takmicariIvrednosti.entrySet() //.stream().sorted((a, b) -> Integer.compare(b.getValue(), a.getValue())) // .map(Map.Entry::getKey).toList(); //List<String> sortirano = takmicariIvrednosti.entrySet() // .stream() // .sorted((a, b) -> Integer.compare(b.getValue(), a.getValue())) /// a - jedan element entry seta, b - drugi element entry seta // .map(Map.Entry::getKey).toList(); //for (String ime : sortirano) { // System.out.println(ime); // } //for (Map.Entry<String, Integer> e : takmicariIvrednosti.entrySet()) { // String ime = e.getKey(); //int br = e.getValue(); // System.out.printf("%s => %d\n", ime, br); // } //System.out.printf("1st place: %s\n",sort.get(0)); /// pozicija 0 //System.out.printf("2nd place: %s\n",sort.get(1)); /// pozicija 1 //System.out.printf("3rd place: %s\n",sort.get(2)); /// pozicija 2 } }