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.
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"));
}
Related
Need to filter duplicates values by id keeping the record with latest timestamp.
Below is the code:
HashMap<String, Object> sampleMap = new HashMap<String, Object>();
sampleMap.put("Name", "Ronaldo");
sampleMap.put("Playerid", "11");
sampleMap.put("Date", "2020-05-01T08:35:42-04:00");
HashMap<String, Object> sampleMap1 = new HashMap<String, Object>();
sampleMap1.put("Name", "Messi");
sampleMap1.put("Playerid", "12");
sampleMap1.put("Date", "2020-06-01T08:35:42-04:00");
HashMap<String, Object> sampleMap2 = new HashMap<String, Object>();
sampleMap2.put("Name", "Messi");
sampleMap2.put("Playerid", "12");
sampleMap2.put("Date", "2020-05-01T08:35:42-04:00");
ArrayList<HashMap<String, Object>> fMap = new ArrayList<HashMap<String, Object>>();
fMap.add(sampleMap);
fMap.add(sampleMap1);
fMap.add(sampleMap2);
Expected Result :
fmap[sampleMap, sampleMap1]
Final Output should have only sampleMap, sampleMap1 because sampleMap1 and sampleMap2 have same playerId but date for sampleMap1 is latest hence it should be there not sampleMap2.
Couldn't find logic to solve this case. Please help me out
If you want to sort a list of maps. You could use the functions of Java 8, namely Stream API. The comparator often uses for sort. You could choose which fields you want to sort by Comparator.comparing(map ->map.get("field")). To remove duplicate value the Stream API represents the distinct() function.
Your list of players could be sorted like this:
fMap.stream()
.sorted(Comparator.comparing(map -> Integer.parseInt((String)map.get("Playerid")))
.thenComparing(map -> LocalDate.parse((String)map.get("Date"),
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",Locale.ENGLISH))))
.distinct()
.collect(Collectors.toList());
I have method that should return Map<Strings, List<String>> but in the mean time my method gives me a Map<Strings, Object>, I want to transfer the values of object into a List of Strings.
Here is the current code:
#SuppressWarnings("unchecked")
static Map<String, List<String>> getQueryParameters(JsonObject inputJsonObject) {
JsonArray parameters = inputJsonObject.getJsonArray("parameters");
Optional<JsonObject> queryParameters = parameters.stream().
filter(JsonObject.class::isInstance).
map(JsonObject.class::cast).
filter(jsonObject -> jsonObject.getJsonObject("queryParameters") != null).
map(item -> item.getJsonObject("queryParameters")).findFirst();
Map<String, Object> paramMap = queryParameters.get().getMap();
paramMap contains key and value , values could be an arrays of integers
so I want to put them into the map below:
Map<String, List<String>> mystore = new LinkedHashMap<String, List<String>>();
My solution is this which did not work correctly
Map<String, List<String>> mystore = new LinkedHashMap<String, List<String>>();
Map<String, Object> paramMap = queryParameters.get().getMap();
Iterator it = paramMap.entrySet().iterator();
while (it.hasNext()) {
String key = it.next().toString();
if (!mystore.containsKey(key)) ;
mystore.put(key, new LinkedList<String>());
mystore.get(key).add(it.next().toString());
}
I was a key holding another key as value and is just a mix up , any suggestions?
After debuging what happens i see that mystore holds both "key and value" together as a key and value it hold the next "key and value as value
Should be something like this:
while (it.hasNext()) {
Map.Entry<String, Object> next = iterator.next();
String key = next.getKey();
Object value = next.getValue();
if (!mystore.containsKey(key)) mystore.put(key, new LinkedList<String>());
mystore.get(key).add(value.toString());
}
I'm not writing a program for you, but instead help you in finding a problem. You are confused with Entry. If you are using IDE, you should solve it easier. Look for this line :
String key = it.next().toString();
Entry has a K,V pair. The iterator returns an EntrySet and thus usage to get key is it.next().getKey() and it.next().getValue()
Now that you have a correct key, please go on debugging. Instead of putting and getting and manipulating in below lines of your code. Put with correct value instead?
Yours:
mystore.put(key, new LinkedList<String>());
mystore.get(key).add(it.next().toString());
What about?:
Entry entry = it.next();
//Get key and value here. DO coding using Entry's methods
List<String> ll = new LinkedList<String>();
ll.add(value)
mystore.put(key, ll);
Tip: Always have the Javadoc or reference documentation handy for knowing more. That's how you learn the language. Refer:https://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html
I have an ArrayList HashMap like the one below.
ArrayList<HashMap<String, String>> mArrType = new ArrayList<>();
with the following values added into it
HashMap<String, String> map;
map = new HashMap<String, String>();
map.put("type", "TRIMMER");
map.put("request", "5");
map.put("actual", "0");
mArrType.add(map);
map = new HashMap<String, String>();
map.put("type", "HAND ROUTER");
map.put("request", "6");
map.put("actual", "0");
mArrType.add(map);
map = new HashMap<String, String>();
map.put("type", "AIR COMPRESSOR");
map.put("request", "6");
map.put("actual", "0");
mArrType.add(map);
Question is how can i get the position of a hashmap from arraylist. eg : hashmap with 'type' trimmer has a position 0 in arraylist, I want to retrieve the position value "0"
I'll write a small util method
private static int getTrimmerTypeMapPosition(ArrayList<HashMap<String, String>> mArrType) {
for (int i = 0; i < mArrType.size(); i++) {
HashMap<String, String> mp = mArrType.get(i);
if (mp.get("type").equals("TRIMMER")) {
return i;
}
}
return -1;
}
To make this method very generic, have "type" and "TRIMMER" as method params, so that you can just pass any key and value pairs to check with.
That's not efficiently possible with your data structure. You can either store the own position in each HashMap or loop through all entries and search for the one with the type you are looking for.
You can, of course, define another HashMap<String, Integer> which maps all your type strings to the corresponding ArrayList index.
Others answer is also correct, but you can do this thing using Java8 also.
E.g.:
int index = IntStream.range(0, mArrType.size()).
filter(i -> mArrType.get(i).get("type").equals("TRIMMER"))
.findFirst().getAsInt();
I would like to know the best way to retrieve the elements of a list inside a list.
List<Object> totalsList = new ArrayList<Object>();
Map<String, Object> grandTotalsMap = new HashMap<String, Object>();
List<Map<String, String>> items = new ArrayList<Map<String, String>>();
Map<String, String> lineItemsMap1 = new HashMap<String, String>();
lineItemsMap1.put("amount", "$70.00");
lineItemsMap1.put("period", "Monthly");
Map<String, String> lineItemsMap2 = new HashMap<String, String>();
lineItemsMap2.put("amount", "$55.00");
lineItemsMap2.put("period", "Bi-Monthly");
items.add(lineItemsMap1);
items.add(lineItemsMap2);
grandTotalsMap.put("section" , "total per pay period amounts");
grandTotalsMap.put("title", "You'r amount");
grandTotalsMap.put("lineItems", items);
**
// I'm expecting output as: I want to create a new Map and put key-values like below:
{
amount: $70.00,
period: Monthly,
},
{
amount: $55.00,
period: Bi-Monthly,
}
**
In your case, using items.get(int index) will return a HashMap corresponding to the location in the array that map is stored. For instance, items.get(0) would return your the first map you added (lineItemsMap1) while items.get(1) would return the second map you added (lineItemsMap2).
Once you have the correct map you are looking for, you can then call the HashMap.get(String columnName) to retrieve the value you have stored.
Two different ways to access the information stored in your ArrayList are as follows:
HashMap<String, String> map = items.get(0);
String amount = map.get("amount"); // This will return '$70.00'
String period = map.get("period"); // This will return 'Monthly'
OR
String amount = items.get(0).get("amount"); // Returning '$70.00'
String period = items.get(0).get("period"); // Returning 'Monthly'
If you are looking to create a new map with these values, you can either store them in local variables (like done above) and then add the variables into the map when creating it like:
HashMap<String, String> newMap = new HashMap<String, String>();
newMap.put("newAmount", amount);
Or you can add the values by directly accessing the ArrayList when creating the map:
newMap.put("newAmount", items.get(0).get("amount"));
Hope this helps!
I have this ArrayList
public ArrayList<HashMap<String, String>> xmlFileNames = new ArrayList<>();
and I want to convert this to:
HashMap<String, String> comparemap2 = new HashMap<>();
What I want is: I want all the Items inside the ArrayList and want to put them into the HashMap
My HashMap looks like:
KEY VALUE
job_id 032014091029309130921.xml
job_id 201302149014021492929.xml
job_id 203921904901920952099.xml
EDIT:
Later I want to compare this map with an existing map:
Properties properties = new Properties();
try {
properties.load(openFileInput("comparexml.kx_todo"));
} catch (IOException e) {
e.printStackTrace();
}
for (String key : properties.stringPropertyNames()) {
compareMap.put(key, properties.get(key).toString());
}
HashMap<String, String> oldCompareMap = new HashMap<>();
for (HashMap key : xmlFileNames) {
oldCompareMap.putAll(key);
}
isEqualMaps(oldCompareMap, compareMap);
I only want to compare, if the filename exists in the compareMap. If not, than add it to the xmlFileName Map
I've looked up in StackOverFlow, how I can convert ArrayList to HashMap. But the other Threads treat data types like Item or Product.
I hope you can help me!
Kind Regards
Given...
public ArrayList<HashMap<String, String>> xmlFileNames = new ArrayList<>();
then something like this should do it.
HashMap<String, String> nhm = new HashMap<>();
for (HashMap xmlFileHm : xmlFileNames ) {
nhm.putAll(xmlFileHm);
}
but be aware if you have duplicate keys in your hashmaps they will get overwritten.
You should also think about coding to interfaces. Take a look at Map and List rather than typing your collections to implementations (ArrayList and HashMap). Take a look at this thread which is quite interesting What does it mean to "program to an interface"?
Depending on what you are trying to do as well you might consider a MultiMap as this might server your purposes better
Edit After update to the question...
A multimap would be better here with one key and multiple values. Although arguably if the key never changes then you could just store the values in a list. For multiamps you can use Google's guava library or do one yourself. For example (not checked for compilation errors as Im doing this from my head)
Map<String, List<String>> m = new HashMap<>();
if (m.containsKey("key")) {
m.get("key").add("new value");
}
else {
List<String> l = new ArrayList<>();
l.add("new value");
m.put("key", l);
}
You can create a new HashMap, then iterate through the list and put all elements from the map from the list to the main map.
List<Map<String, String>> list = new ArrayList<>();
Map<String, String> map = new HashMap<>();
for (Map<String, String> mapFromList : list) {
map.putAll(mapFromList);
}
You can try something like this..
ArrayList<HashMap<String, String>> xmlFileNames = new ArrayList<>();
HashMap<String, String> comparemap2 = new HashMap<>();
for(HashMap<String, String> i:xmlFileNames){
comparemap2.putAll(i);
}
You may need to consider the case of duplicate keys. else they will get override.
Create a new map and put All each element of arrayList to the map.
But in that case if you have same keys in two element of arrayList (hashmap) then it will override the previous one.