Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- import java.util.*;
- class Candidate implements Comparable<Candidate>
- {
- private String city,code,name;
- private int age;
- public Candidate(String city, String code, String name, int age) {
- this.city = city;
- this.code = code;
- this.name = name;
- this.age = age;
- }
- public String getCity() {
- return city;
- }
- public String getCode() {
- return code;
- }
- public String getName() {
- return name;
- }
- public int getAge() {
- return age;
- }
- @Override
- public int compareTo(Candidate o) {
- return Comparator.comparing(Candidate::getName)
- .thenComparing(Candidate::getAge)
- .compare(this,o);
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Candidate candidate = (Candidate) o;
- return code.equals(candidate.code);
- }
- @Override
- public int hashCode() {
- return Objects.hash(code);
- }
- @Override
- public String toString()
- {
- return String.format("%s %s %s",code,name,age);
- }
- }
- class Audition {
- private HashMap<String, TreeSet<Candidate>> map;
- public Audition() {
- map = new HashMap<>();
- }
- void addParticpant(String city, String code, String name, int age) {
- Candidate c = new Candidate(city, code, name, age);
- map.computeIfPresent(city, (k, v) ->
- {
- v.add(c);
- return v;
- });
- map.computeIfAbsent(city, (v) ->
- {
- TreeSet<Candidate> set = new TreeSet<>();
- set.add(c);
- return set;
- });
- }
- void listByCity(String city)
- {
- TreeSet<Candidate> set = map.get(city);
- set.stream()
- .forEach(System.out::println);
- }
- }
- public class AuditionTest {
- public static void main(String[] args) {
- Audition audition = new Audition();
- List<String> cities = new ArrayList<String>();
- Scanner scanner = new Scanner(System.in);
- while (scanner.hasNextLine()) {
- String line = scanner.nextLine();
- String[] parts = line.split(";");
- if (parts.length > 1) {
- audition.addParticpant(parts[0], parts[1], parts[2],
- Integer.parseInt(parts[3]));
- } else {
- cities.add(line);
- }
- }
- for (String city : cities) {
- System.out.printf("+++++ %s +++++\n", city);
- audition.listByCity(city);
- }
- scanner.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement