Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.time.LocalDate;
- import java.time.format.DateTimeFormatter;
- import java.util.LinkedHashMap;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- class DateRecord {
- public int id;
- public LocalDate date;
- // Declaring a class constructor to create DateRecord object for our HashMap(Dictionary).
- // "This" keyword is used to reference to the class variables "id" and "date",
- // otherwise they will be hidden(shadowed) by the constructors local variables with the same name.
- public DateRecord(int id, LocalDate date) {
- this.id = id;
- this.date = date;
- }
- public int getId() {
- return this.id;
- }
- public LocalDate getDate() {
- return this.date;
- }
- }
- public class p01_Registered_Users {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String inputLine = scanner.nextLine();
- LinkedHashMap<String, DateRecord> registeredUsers = new LinkedHashMap<>();
- int id = 0;
- while (!"end".equals(inputLine)) {
- String[] tokens = inputLine.split(" -> ");
- String name = tokens[0];
- LocalDate parsedDate = parseDate(tokens[1]);
- registeredUsers.put(name, new DateRecord(id++, parsedDate));
- inputLine = scanner.nextLine();
- }
- LinkedHashMap<String, DateRecord> orderedData = registeredUsers.entrySet().stream()
- .sorted((d1, d2) -> d1.getValue().getDate().compareTo(d2.getValue().getDate()))
- .sorted((id1, id2) -> {return id2.getValue().getId() - id1.getValue().getId();})
- .limit(5).collect(Collectors.toMap(e -> e.)
- }
- private static LocalDate parseDate(String dateInput) {
- // Creating DateTimeFormatter to parse a date from a string using our custom pattern.
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyy");
- LocalDate date = LocalDate.parse(dateInput, formatter);
- return date;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment