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.