javaandroiddictionary

Alternative to getOrDefault for devices below API 24 Android


I am writing an Android application and I'm using a Java class that uses has a loop as follows:

for (Set<I> itemset : candidateList2) {
    supportCountMap.put(itemset, supportCountMap.getOrDefault(itemset,0)+ 1);
}

I get the warning Call requires API level 24(current min is 16) on the method:

supportCountMap.getOrDefault(itemset,0)+1);

Is there any workaround to this method such that it can work on phones with an SDK version lower than 24 e.g Marshmallow(23) and Lollipop(21)?


Solution

  • You can always implement the same logic yourself:

    for (Set<I> itemset : candidateList2) {
        Integer value = supportCountMap.get(itemset);
        if (value == null) {
            value = 0;
        }
        supportCountMap.put(itemset, value + 1);
    }