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.*;
- import java.util.stream.Collectors;
- import java.util.stream.Stream;
- /*class DateRecord {
- private int id;
- private 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_2_Registered_Users_test {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String inputLine = scanner.nextLine();
- LinkedHashMap<String, LocalDate> registeredUsers = new LinkedHashMap<>();
- int id = 1;
- while (!"end".equals(inputLine)) {
- String[] tokens = inputLine.split(" -> ");
- String name = tokens[0];
- LocalDate parsedDate = parseDate(tokens[1]);
- registeredUsers.put(name, parsedDate);
- inputLine = scanner.nextLine();
- }
- Stream<Map.Entry<String, LocalDate>> orderedData = /*new LinkedHashMap<>(*/registeredUsers.entrySet().stream()
- .sorted((a, b) -> {
- if (a.getValue().compareTo(b.getValue()) == 0) {
- return -1;
- }
- return a.getValue().compareTo(b.getValue());
- })
- .limit(5)
- .sorted((a, b) -> b.getValue().compareTo(a.getValue()));
- /*.collect(Collectors.toMap(Map.Entry<String, LocalDate>::getKey,
- Map.Entry<String, LocalDate>::getValue)));*/
- for (Map.Entry<String, LocalDate> orderedDataEntry : orderedData.collect(Collectors.toList())/*orderedData.entrySet()*/) {
- //int idToPrint = orderedDataEntry.getValue().getId();
- String name = orderedDataEntry.getKey();
- String date = formatDate(orderedDataEntry.getValue());
- System.out.printf("%s - > %s\n", name, date);
- }
- }
- 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;
- }
- private static String formatDate(LocalDate dateToFormat) {
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyy");
- String formattedDate = dateToFormat.format(formatter);
- return formattedDate;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment