iterate over a list and arrange data - java

I am getting a list of data in the form of List object from database which contains data as
field_id value dt_updated
1 3 jun-6
2 5 jun-5
1 2 jun-3
3 5 jun-3
i want iterate over this list and group data for for fields as per there old and new values.For example:i want create a json like:
"field_1":
[
oldvalue"2"
new value:"3"
],
field_2 :
[ oldvalue:"5"],
i want to iterate over the list and get the old and new values for that field and put in some object.How to perform this? In some cases field will have only old values.

I don't know if entries from database are ordered by dt_update but I assume it's not guaranteed.
Just put your entries from db to Map(id->DBUpdate) where DBUpdate is some simple value object with value and updateDate fields into some MultiMap (Available e.g. in Google Guava library). After iterating over all objects from DB sort elementes for each id by updateDate (simply by implementing Comparable interface in DBUpdate) and construct appropriate String (if there is only one value for given id in MultiMap there will be only oldValue, otherwise use first element for new value and second element in MultiMap as oldValue)

Related

Merge multiple Map and differentiate their value with different keys in java

EDIT: I have three maps with following format:
map1.put("aaa",1);
map1.put("bbb",1);
map1.put("ccc",1);
map2.put("aaa",2);
map2.put("bbb",3);
map3.put("ccc",6);
map3.put("ddd",6);
Now I want the result as list with following format:
[{"id":"aaa","map1count":"1","map2count":"2","map3count":"0"},
{"id":"bbb","map1count":"1","map2count":"0","map3count":"0"},
{"id":"ccc","map1count":"1","map2count":"0","map3count":"6"},
{"id":"ddd","map1count":"0","map2count":"0","map3count":"6"}]
How can I do this in java, help me to figure out.
you can create a class Count, with three members ex. mapCount1, mapCount2, mapCount3. Now create result map
HashMap<String, Count>() result = new HashMap<>();
Now you need to traverse these 3 maps, check map.containsKey(key), here key might be "aaa", "bbb", "ccc" etc. Now traverse first map, since map doesnt not contain any key, add key with value to result map. Now do this for 2nd, 3rd map, If key is there and then get value of count object and update count for mapCount2/3. At last for each key you can print count specific to each map.

Increment Key in a collection

I have a collection say
Map<Integer,Integer> myMap=new Map<Integer,Integer>();
myMap.put(1,"a");
myMap.put(2,"b");
myMap.put(3,"c");
My map currently has {1="a",2="b",3="c"}.
Suppose I want to put a value say myMap.put(1,"d")
So is there a way that I am able to add the existing key and incrementing the remaining key so my output map be like
{1="d",2="a",3="b",4="c"} ?
You will have to iterate over all the elements of the Map in order to modify the values for all the existing keys.
For example, woth Java 8 Streams you can produce a new Map where the keys are incremented, and then add the new Entry :
map = map.entrySet().stream().collect(Collectors.toMap(e->e.getKey()+1,e->e.getValue()));
map.put(1,"d");
However, if your keys are consecutive integers (as your example suggests), why not use an ArrayList<String> instead of a Map<Integer,String>? This will give you the functionality you want by simply calling list.add(0,"d"); (with the small difference of the indices starting at 0 instead of 1).

How can I store a list of parse objects from one column into a HashSet?

If I have a String column "A" in a parse class. And I have 5 objects in column "A" with values:
1- A'
2- B'
3- C'
4- D'
5- E'
How can I retrieve those objects, and store them in HashSet to be used?
So, if I system.out.println the HashSet values, it would show me A',B',C',D',E'.
Note: I am trying to add a list of parse objects into a hashSet. So I retrieve a whole column, like I said above, and store it into a hashSet.
Need more information? Ask instead of -1ing please.

2 Dimentional array with Hash-map

Is there a way to put a 2d array into a Hash map setup?
Example of the array would be two strings {"John", "red"},
{"George", "blue}
And I would want red to correspond to john etc.
I know I can use a nested loop to go through every item in the 2d array but how would then set it up to add them to the hash-map
hashMap.put("John", "red");
Assuming every array has 2 items in the form of {Name, Color}, you can just iterate over it
for(String[] combo : some2DArray){
someHashMap.Put(combo[0], combo[1]); // Or swap them, depending on what you
// want to be the key and the value
}
If you want to avoid the possibility of removing data because you happen to have two people with the same name there are a few approaches you can take:
Keep the old data
Keep the new data
Assign the new data to a new key
Combine the data in the same key
Keep the old data
Perform a check before using HashMap#put and see if the key already exists.
Only add the data if it doesn't exist yet.
Keep the new data
Use the current code, it will overwrite the old value.
Assign the new data to a new key
Create a new key based on your own rules and insert that.
Combine the data in the same key
Define your HashMap as HashMap<String, List<String>> and add the values to the list.
How about implementing a Pair class, so you can use HashMap<Pair<String,String>> ?
EDIT: Might be that I misunderstood your question, is that what yoe were asking?

How to find intersection of two keys in Redis using Java?

I have two keys in the Redis. First key contains set of strings as a value.Second key contains the sorted set of object(String as a value and score ). I want to fetch elements where string in first key and string field of the object in the second key are similar.
If I replace object with a string in the second key,I am able to fetch but I want to fetch list of strings along with their score.
I am using Spring-data-redis and jedis for Redis handling.
Is it possible to fetch list of common strings and their corresponding score? If yes, how.
How you are storing your data will affect how you want to retrieve it. By storing your keys as listed in the comment, you are basically limited to string manipulation to determine anything useful, and that really isn't the value of using Redis. (It's not meant for "searching", its meant for fast lookups.)
Consider something like this:
The keys used in Redis will be your first set of strings, each containing a list of values. The values in those lists will be your second set of strings, and may be duplicated in different lists (as you see fit).
LPUSH "x1" "POJO[field1=x1, field2=y1]" "POJO[field1=x1, field2=y2]"
LPUSH "x2" "POJO[field1=x2, field2=y2]"
etc...
When you want the values of your first number
LRANGE x1 0 1000 (or LLEN x1 --> "result", then LRANGE x1 0 "result")

Categories