迭代HashMap直到找到第一个值?

问题描述 投票:0回答:2

我有一个HashMap,我用它来存储整数id和String值。

我希望能够根据用户输入String从HashMap中删除内容。我一直这样做的方式如下:

        public void pickingVehicleUp() {

        System.out.println("Please enter number plate: ");
        input = new Scanner(System.in);
        String PLATE = input.nextLine();
        Iterator<Map.Entry<Integer, String>> iter = zone_1.entrySet().iterator();

        Map.Entry<Integer, String> Empty1 = iter.next();
        Map.Entry<Integer, String> Empty2 = iter.next();
        Map.Entry<Integer, String> Empty3 = iter.next();
        Map.Entry<Integer, String> Empty4 = iter.next();
        Map.Entry<Integer, String> Empty5 = iter.next();

        zone_1.entrySet().removeIf(entry -> (PLATE.equals(entry.getValue())));

        if (Empty1.getValue().equals(PLATE)) {
            Integer key = Empty1.getKey();
            zone_1.put(key, "Empty");
        } else if (Empty2.getValue().equals(PLATE)) {
            Integer key = Empty2.getKey();
            zone_1.put(key, "Empty");
        } else if (Empty3.getValue().equals(PLATE)) {
            Integer key = Empty3.getKey();
            zone_1.put(key, "Empty");
        } else if (Empty4.getValue().equals(PLATE)) {
            Integer key = Empty4.getKey();
            zone_1.put(key, "Empty");
        } else if (Empty5.getValue().equals(PLATE)) {
            Integer key = Empty5.getKey();
            zone_1.put(key, "Empty");
        } else {
            System.out.println("That number plate doesn't exist!");
        }
    }

虽然这段代码工作得很好,但我认为这是非常不整洁的,并且可能有更多有效的方法来实现它,我想找到其中一种方法,因为我需要为5种不同的HashMaps做同样的方法。

java loops if-statement hashmap compare
2个回答
0
投票

可能是这样的事情会对你有所帮助

String PLATE = "PLATE";

    HashMap<Integer, String> map = new HashMap<>();
    map.put(1, "DOOR");
    map.put(2, "DOG");
    map.put(3, "PLATE");
    map.put(4, "SNOW");

    System.out.println("Before check - " + map);

    if (map.containsValue(PLATE)) {
        for (Map.Entry<Integer, String> item:map.entrySet()) {
            if (item.getValue().equals(PLATE)) {
                map.remove(item.getKey());
                break;
            }
        }
    }

    System.out.println("After check - " + map);

0
投票

如果您按照JB Nizet在评论中建议的那样颠倒地图可能会更容易。直观地说,如果您正在寻找Plate及其所在的位置,那么您的地图将停车位ID作为板号的函数存储更有意义。然后它使你的任务变得微不足道:

String PLATE = "PLATE1";
Map<String, Integer> plateToParkingSpaceID = new HashMap<>();

plateToParkingSpaceID.put("PLATE1", 1);
plateToParkingSpaceID.put("PLATE2", 2);
plateToParkingSpaceID.put("PLATE3", 3);
plateToParkingSpaceID.put("PLATE4", 4);

if (plateToParkingSpaceID.containsKey(PLATE)) {
    plateToParkingSpaceID.remove(PLATE);
} else {
    System.out.println("That number plate doesn't exist!");
}

但是,如果您保留当前将停车位映射到许可证位置的设计,我们可以执行以下操作:

String PLATE = "PLATE1";
Optional<Integer> parkingSpaceId = zone1.entrySet().stream()  // stream all of the entries in the zone1 Map
    .filter(e -> e.getValue().equals())                       // filter out all that don't have the correct plate
    .findFirst()                                              // there should be 0 or 1 of these, so we can stop as soon as we find the first one

if (parkingSpaceId.isPresent()) {
    zone1.remove(parkingSpaceId.get());  // remove that space from the map
} else {
    System.out.println("That number plate doesn't exist!");
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.