Collection Content Never Updated in Intellij IDEA

I created a simple Counter class:

public class Counter<T> extends HashMap<T, Long> { public Counter() { } public void increase(T key) { put(key, getOrDefault(key, 0l) + 1); } } 

In my code, I call the zoom () method and then use the Map method to access the data, for example.

  Counter<Integer> counter = new Counter<>(); for (Integer i: ... some collection ...) counter.increase(i); 

Intellij highlights the counter declaration (the first line in the last fragment) with a warning color, and in the tooltip message

The contents of the collection are requested but never updated.

Obviously, I can simply ignore this warning, but is there a way to convince Intellij that something is wrong with my code?

I use the 14.0.2 community.

+8
source share
2 answers

IntelliJ IDEA does not understand that the zoom () method updates the map because it is not part of the standard map API. To remove the warning, you can disable the check.

However, the best design would be for your Counter class to encapsulate the HashMap rather than extend it. This will ensure that users in your class will only call the appropriate APIs and will not corrupt your data by calling the put () method or other change methods directly.

+7
source

You can also try adding @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") to ignore it.

+2
source

Source: https://habr.com/ru/post/987937/


All Articles