0

我有一个大学的项目要完成,它基本上是从 Java 中的 CSV 文件中返回一堆东西。

我已经设法从以前的尝试中完成了大部分工作,但我坚持的一个部分是如何读取 CSV 文件,但只有文件的特定部分:预订时间最多的房间并显示它在终端中运行 jmh 并创建一个新的 jar 文件。我的第一次尝试是硬编码,虽然它有效,但这不是讲师想要的。任何帮助或方向指针都会是很棒的代码,下面是讲师不想要的方式。

此外,CSV 文件的格式为 A) id B) 房间名称 C) 日期 D) 时间 E) 预订时间 F) 人预订

公共字符串 [] getTopRoomsBooked(int n) {

    HashMap<String, Integer> rooms = new HashMap<String, Integer>();

    rooms.put("Gower", 281);
    rooms.put("Usk", 291);
    rooms.put("Wye", 283);
    rooms.put("Bala", 282);
    rooms.put("Pen y Fan", 292);
    rooms.put("Llangorse", 290);
    rooms.put("Snowdon", 288);
    rooms.put("Taff", 296);
    rooms.put("Cadair Idris", 292);

    for (String i : rooms.keySet()) {
        System.out.println("Room name: " + i + " Amount of time: " + rooms.get(i));
    }

    System.out.println();

    List<Integer> timeBooked = new ArrayList<>(rooms.values());

    Collections.sort(timeBooked, Collections.reverseOrder());

    if (n == 1) {
        System.out.println("RoomName : Taff " + timeBooked.get(n - 1));
    } else if (n == 3) {
        System.out.println("1: Taff " + timeBooked.get(n - 3));
        System.out.println("2: Cadiar Idris " + timeBooked.get(n - 2));
        System.out.println("3: Pen y Fan " + timeBooked.get(n - 1));

    } else if (n == 5) {
        System.out.println("1: Taff " + timeBooked.get(n - 5));
        System.out.println("2: Cadair Idris " + timeBooked.get(n - 4));
        System.out.println("3: Pen y Fan " + timeBooked.get(n - 3));
        System.out.println("4: Usk " + timeBooked.get(n - 2));
        System.out.println("5: Llangorse " + timeBooked.get(n - 1));
    } else if (n == 9) {
        System.out.println("1: Taff " + timeBooked.get(n - 9));
        System.out.println("2: Cadair Idris " + timeBooked.get(n - 8));
        System.out.println("3: Pen y Fan " + timeBooked.get(n - 7));
        System.out.println("4: Usk " + timeBooked.get(n - 6));
        System.out.println("5: Llangorse " + timeBooked.get(n - 5));
        System.out.println("6: Snowden " + timeBooked.get(n - 4));
        System.out.println("7: Wye " + timeBooked.get(n - 3));
        System.out.println("8: Bala " + timeBooked.get(n - 2));
        System.out.println("9: Gower " + timeBooked.get(n - 1));
    }

    return null;

}
4

1 回答 1

0

这可以通过使用以下特性的 Java 8 流来实现:

  • Comparator.comparing(...).thenComparing()Map.Entry<String, Integer>按预订时间值倒序排序流,然后按房间名称排序
  • limit输出量
  • map重新映射到房间名称(roomshashmap 中的键)
  • collect(Collectors.toList()).toArray(String[]::new)将房间名称收集到字符串数组

假设roomshashmap 在方法外部填充getTopRoomsBooked并作为参数提供,让我们将其重写如下:

public static String[] getTopRoomsBooked(Map<String, Integer> rooms, int n) {

    // just for the row id in inner print
    AtomicInteger id = new AtomicInteger(1);
  
    Comparator<Map.Entry<String, Integer>> byTime = Comparator.comparing(Map.Entry::getValue, Comparator.reverseOrder());
    Comparator<Map.Entry<String, Integer>> byName = Comparator.comparing(Map.Entry::getKey);
    
    return rooms.entrySet().stream()
                .sorted(byTime.thenComparing(byName))
                .limit(n)
    // inner print       
                .peek(e -> System.out.printf("%d: %s %d%n", id.getAndIncrement(), e.getKey(), e.getValue())) 
                .map(Map.Entry::getKey)
                .collect(Collectors.toList())
                .toArray(String[]::new);
}

测试与输出

    int[] arr = {1, 3, 5};
    Arrays.stream(arr)
          .forEach(a -> System.out.println("Top " + a + ":\n" 
              + Arrays.toString(Main.getTopRoomsBooked(rooms, a)) 
              + "\n======\n"
    ));
//////////////////////////////////////////////////////////////////

1: Taff 296
Top 1:
[Taff]
======

1: Taff 296
2: Cadair Idris 292
3: Pen y Fan 292
Top 3:
[Taff, Cadair Idris, Pen y Fan]
======

1: Taff 296
2: Cadair Idris 292
3: Pen y Fan 292
4: Usk 291
5: Llangorse 290
Top 5:
[Taff, Cadair Idris, Pen y Fan, Usk, Llangorse]
======

在线演示

于 2020-08-29T15:02:47.953 回答