简体   繁体   English

根据 Java 中的键属性从 hashmap 中删除 object

[英]Remove object from hashmap based on key attribute in Java

I have an Identifier class that looks like this:我有一个Identifier class,如下所示:

private final Integer year;
private final Integer month;

...

@Override
public boolean equals(Object o) {
    ...
}

@Override
public int hashCode() {
    ...
}

I want to use this class as the key in a ConcurrentHashMap .我想使用这个 class 作为ConcurrentHashMap中的键。

Something like ConcurrentHashMap<Identifier, Object>类似ConcurrentHashMap<Identifier, Object>

Objects are always identified by year and month, which means that this class is useful to find the object and still use the concurrent property of the hashmap.对象始终按年和月标识,这意味着此 class 可用于查找 object 并仍使用 hashmap 的并发属性。

Now, in some situations I want to remove all objects related to some specific year.现在,在某些情况下,我想删除与某个特定年份相关的所有对象。 Is there a way to do this easily?有没有办法轻松做到这一点?

Or should I just create a double ConcurrentHashMap ?还是我应该只创建一个双ConcurrentHashMap ConcurrentHashMap<Integer, ConcurrentHashMap<Integer, Object>>

I'm not sure it would be the same thing.我不确定它是否会是同样的事情。 And if for some reason I wanted to delete by month it wouldn't work.如果出于某种原因我想按月删除它是行不通的。

If you use ConcurrentHashMap<Identifier, Object> , you'll have to iterate all entries of the map and delete required objects.如果您使用ConcurrentHashMap<Identifier, Object> ,则必须迭代 map 的所有条目并删除所需的对象。

It is not possible to override equals & hashCode to achieve what you want.无法覆盖equals & hashCode来实现您想要的。

If you override the above-mentioned methods to treat 2 objects with the same year as equal, you won't be able to store multiple objects with the same year & different months in the map because doing that will replace the old object since they both are equal.如果您覆盖上述方法以将相同年份的 2 个对象视为相等,您将无法在 map 中存储多个具有相同年份和不同月份的对象,因为这样做将替换旧的 object,因为它们都是平等的。


However, this setup ConcurrentHashMap<Integer, ConcurrentHashMap<Integer, Object>> can work for you.但是,此设置ConcurrentHashMap<Integer, ConcurrentHashMap<Integer, Object>>可以为您工作。

I think it is not possible to do that using equals, hashcode cause you already defined them in your Identifier class using both year and month, so you can't change them just to use year field cause it will override elements in your map even if month is different.我认为使用等于,哈希码是不可能做到这一点的,因为您已经使用年和月在标识符 class 中定义了它们,因此您不能仅使用年字段来更改它们,因为它会覆盖 map 中的元素,即使月份不同。

public class Application {
public static void main(String[] args) {
    ConcurrentHashMap<Identifier, String> map = new ConcurrentHashMap<>();
    map.put(new Identifier(2020,1), "1");
    map.put(new Identifier(2020,2), "2");
    map.put(new Identifier(2021,3), "3");
    map.put(new Identifier(2021,4), "4");
    map.put(new Identifier(2021,1), "5");

    System.out.println(map.size()); // 5
    removeElementsByYear(map, 2020);
    System.out.println(map.size()); // 3
}

private static void removeElementsByYear(ConcurrentHashMap<Identifier, String> map, int year) {
    for(Identifier key : map.keySet()) {
        if (key.getYear() == year) {
            map.remove(key);
        }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM