0

I'm writing an application that resends messages received according to the sender, recipient and the keyword used in the sms message. I have a class that fetches from sqlite and returns three objects in an hashmap as follows.

receivedMessages=>map(
0=>map("_ID"=>1,
        "sender"=>"*",
        "recipient"=>"*",
       "keyword"=>"*"
    ),    
1=>map("_ID"=>2,
         "sender"=>"8080",
        "recipient"=>"*",
        "keyword"=>"*"
        ),    
2=>map("_ID"=>3,
        "sender"=>"*",
        "recipient"=>"22255",
        "keyword"=>"*"
        ) 
    )

I would love to group them in a map using each of their properties and their corresponding values in an array rather than read them everytime because of efficiency. It's more efficient to fetch from a hashmap 1000 times than reading them from sqlite 1000 times. I want to put them on a hashmap or any other efficient container as shown below.

 messages=>map(
 "hashmapSenders"=>map(
    "*"=>map(1,3),
    "8080"=>1,
    )
    "hashmapRecipients"=>map(
    "*"=>map(1,2),
    "22255"=>3,
    )
    "hashmapKeywords"=>map(
    "*"=>map(1,2,3)
    )
)

so that when I want to get all the senders and the values contained I will call messages.get("hashmapSenders");

or to get recipients and the values contained I will call messages.get("hashmapRecipients");

or to get keywords and the values contained I will call messages.get("hashmapKeywords");

Here is what I have so far

ArrayList<HashMap<String, String>> receivedMessages = getAllMessages();
Iterator<HashMap<String, String>> iterator = receivedMessages.iterator();
HashMap<String, HashMap<String, String>> messages = new HashMap<>();    
HashMap<String, String> hashmapSenders = new HashMap<>();
HashMap<String, String> hashmapRecipients = new HashMap<>();
HashMap<String, String> hashmapKeywords = new HashMap<>();

while (iterator.hasNext()) {
     HashMap<String, String> map = iterator.next();
     hashmapSenders.put(map.get("sender"),map.get("_ID"));
     hashmapRecipients.put(map.get("recipient"),map.get("_ID"));
     hashmapRecipients.put(map.get("keyword"),map.get("_ID"));
     }
 messages.put("hashmapSenders", hashmapSenders);
 messages.put("hashmapRecipients", hashmapRecipients);
 messages.put("hashmapKeywords", hashmapKeywords);

The problem in my code is that the new values will overwrite the older values so I wont get my desired results. Please advice. thank you.

sammyukavi
  • 1,501
  • 2
  • 23
  • 51
  • Declare the HashMap as `HashMap>`, and then insert the values into the ArrayList for the specified key. This way you can store more then one value. – SomeJavaGuy Sep 17 '15 at 13:34
  • How do i insert the value in the arraylist while still in the loop? I'm supposed to group them – sammyukavi Sep 17 '15 at 13:35
  • I guess you overwrite the values whenever you call `hashmapSenders.put(map.get("sender"),map.get("_ID"))` in the loop, or any of the two following calls of `put`. – SomeJavaGuy Sep 17 '15 at 13:40

1 Answers1

0

After a search on SO, I found a similar question here

So I modified the answer to fit me. Here is the metnod I used

public void addToMap(HashMap<String, List<String>> map, String key, String value) {
        if (!map.containsKey(key)) {
            map.put(key, new ArrayList<String>());
        }
        map.get(key).add(value);
    }

So my answer is below

ArrayList<HashMap<String, String>> receivedMessages = getAllMessages();
Iterator<HashMap<String, String>> iterator = receivedMessages.iterator();
HashMap<String, List<String>> messages = new HashMap<>();
while (iterator.hasNext()) {
HashMap<String, String> map = iterator.next();
addToMap(messages, map.get("sender"),map.get("_ID"));
addToMap(messages, map.get("recipient"),map.get("_ID"));
addToMap(messages, map.get("keyword"),map.get("_ID"));

}

Community
  • 1
  • 1
sammyukavi
  • 1,501
  • 2
  • 23
  • 51