Pulling from data from SQLite DB on Android
Updating collections

Many things can be simplified around this snippet:


for (PriceTable pt1 : allList) {
    ArrayList&lt;PriceTable&gt; set = valueMap.get(pt1.getItemType());
    if (set == null) {
        set = new ArrayList&lt;PriceTable&gt;();
        set.add(pt1);
    } else {
        set.add(pt1);
    }
    valueMap.put(pt1.getItemType(), set);

}
cursor.close();



This is equivalent:

for (PriceTable pt1 : allList) {
    String itemType = pt1.getItemType();
    ArrayList&lt;PriceTable&gt; set = valueMap.get(itemType);
    if (set == null) {
        set = new ArrayList&lt;PriceTable&gt;();
        valueMap.put(itemType, set);
    }
    set.add(pt1);
}


What I did:


Moved set.add(pt1); out of the if-else
Moved valueMap.put(itemType, set); inside the if
No need for an else
Extracted pt1.getItemType() to avoid an extra lookup
cursor.close(); doesn't belong here, it should be done before this snippet. It makes the code easier to read if you don't delay actions like this.


Use interfaces types instead of implementations

When the interface type is enough, you should use that instead of the specific implementation type. For example instead of this:


public HashMap&lt;String, ArrayList&lt;PriceTable&gt;&gt; getPromoForViewGroup(
            String condition) {
    String custGroup = "custType";
    String itemGroup = "itemType";
    HashMap&lt;String, ArrayList&lt;PriceTable&gt;&gt; valueMap = new HashMap&lt;String, ArrayList&lt;PriceTable&gt;&gt;();



This is recommended:

public Map&lt;String, List&lt;PriceTable&gt;&gt; getPromoForViewGroup(String condition) {
    String custGroup = "custType";
    String itemGroup = "itemType";
    Map&lt;String, List&lt;PriceTable&gt;&gt; valueMap = new HashMap&lt;String, List&lt;PriceTable&gt;&gt;();


Notice all the rewritten types.

Mutually exclusive if-else

This conditions will not be both true at the same time,
so instead of two separate if, they should be joined with an else if:


if (condition.equals(CUST)) {
    getPromoForViewGroupHelperCust(custGroup, condition, valueMap);
}

if (condition.equals(ITEM)) {
    getPromoForViewGroupHelperItem(itemGroup, condition, valueMap);

}



Don't Select *

It's not recommended to select columns using * wildcard.
Different versions of the database might have columns in different order than you expect. There might be more columns than you expected, selecting more data than necessary, causing slowness. It's recommended to name the columns you need explicitly, and access their values by name.

Misc


  Apart from merging the 2 get methods, what else can I do to optimise this? 


Oh definitely merge those methods...


  Is there a way to iterate a map while writing to it?


Kind of. You can iterate over a copy, while writing to the original. For example:

Map&lt;String, Integer&gt; map = new HashMap&lt;&gt;();
map.put("hello", 2);
map.put("world", 7);
for (Map.Entry&lt;String, Integer&gt; entry : new HashMap&lt;&gt;(map).entrySet()) {
    if (entry.getValue() &gt; 5) {
        map.remove(entry.getKey());
    }
}
System.out.println(map);


Prints {hello=2}.