create dynamic objects using spring boot hashmap - java

i wrote the below code to form custom objects. but its returning only single object. like below
{"fathername":null,"name":"ks"}
but i want like below(array object)
[{"name":"Kalaiselvan","fathername":"Karuppanan"},{"name":"ks","fathername":null}]
but it is returning single object. what i am missing here, please pour your suggestions
public HashMap<String, String> applicantlistv2() {
List<Applicant> users = repository.findAll();
HashMap<String, String> myhash = new HashMap<String, String>();
for (Applicant applicant : users) {
myhash.put("fathername", applicant.getFather_name());
myhash.put("name", applicant.getFirst_name());
System.out.println(applicant.getFirst_name());
}
//return repository.findAll();
return myhash;
}

For each iteration you are replacing the same key. So only the data in the last iteration would be available.
Replace the code as follows:
List<Map<String, String>> myhash = new ArrayList<>();
for (Applicant applicant : users) {
Map<String, String> map = new HashMap<>();
map.put("fathername", applicant.getFather_name());
map.put("name", applicant.getFirst_name());
myhash.add(map);
System.out.println(applicant.getFirst_name());
}
return myhash;

Related

how can i get values from HashMap<String, Object> inside Obect []?

Here's my User Entity field
#Schema
private Object[] alreadyShownUser;
how can i get values from HashMap<String, Object> inside Obect [] ?
"alreadyShownUser": [
{
"shownUserId": "63d357149dac903ab670d577",
"shownAt": "2023-01-30T07:19:52.295+00:00"
},
{
"shownUserId": "63a4196c62533371159762fd",
"shownAt": "2023-01-30T07:21:00.243+00:00"
}
i need only "shownUserId"'s values
If I understand you correctly, each element in your Object[] alreadyShownUser is a HashMap<String, Object>. If that's the case then you can just cast it:
for(Object o:alreadyShownUser) {
HashMap<String, Object> theMap = (HashMap<String, Object>) o;
System.out.println(theMap.get("shownUserId"));
}

How do i acess a nested hash map value that i have return from another class ? Java

I am new to hash mapping and I was trying to created a nested hash map on one side of the class and create another class to call it out, so here's how my code looks like
public class Hash {
private HashMap<String, HashMap<String, String>> wow = new HashMap<String, HashMap<String, String>>();
public void SetHash(){
wow.put("key", new HashMap<String, Object>());
wow.get("key").put("key2", "val2");
}
public HashMap GetMap(){
return wow;
}
}
And on the other class which is the main class it will be like this:
public static void main(String[] args) {
Hash h = new Hash();
h.SetHash();
System.out.println(h.GetMap.get("key").get("key2"));
}
But when I place the second get, there's an error, so I am not sure if this is possible or if I should actually place the hash directly at the main class.
GetMap is a method, not an attribute, so you have to refer it with parenthesis ():
h.GetMap().get("key")
Now, second error. Your Map<String, Map<String, String> named wow contains a values that are objects of the type Map<String, String> so, before the get, you need get the map:
Map<String, String> m = (HashMap<String, String>) h.GetMap().get("key");
And then you can print it:
System.out.println(m.get("key2"));
if you want an ONELINER (is not really clear, but check explanation in comments):
System.out.println(((HashMap<String, String>) h.GetMap().get("key")).get("key2"));
// ↑ casting parenthesis ↑ (
// ↑ this say group IS a map and allow get() ↑
// ↑ system.out.println parenthesis ↑
NOTE: change also this declaration
wow.put("key", new HashMap<String, Object>());
By
wow.put("key", new HashMap<String, String>());
FINAL CODE:
public class Q37066776 {
public static void main(String[] args) {
Hash h = new Hash();
h.SetHash();
Map<String, String> m = (HashMap<String, String>) h.GetMap().get("key");
System.out.println(m.get("key2"));
}
}
class Hash {
private HashMap<String, HashMap<String, String>> wow = new HashMap<String, HashMap<String, String>>();
public void SetHash() {
wow.put("key", new HashMap<String, String>());
wow.get("key").put("key2", "val2");
}
public HashMap GetMap() {
return wow;
}
}
WORKING ONLINE DEMO
but you can always
Do it better! :=)
As pointed by Andrew
you can change return of the method,
But also many other things like:
using less concrete objects (Map instead of HashMap)
follow conventions (GetMap() would be getMap())
Make Hash a static class with static block
If I had to rewrite your code, my result would be like this:
public class Q37066776 {
public static void main(String[] args) {
System.out.println(Hash.getMap().get("key").get("key2"));
}
}
class Hash {
private static Map<String, Map<String, String>> wow = new HashMap<String, Map<String, String>>();
static {
wow.put("key", new HashMap<String, String>());
wow.get("key").put("key2", "val2");
}
public static Map<String, Map<String, String>> getMap() {
return wow;
}
}
You have 3 errors:
GetMap is a method - you need to write GetMap().
you declared the inner Map as HashMap<String, String> - you cannot initialize the inner map to: wow.put("key", new HashMap<String, Object>());
Change it to wow.put("key", new HashMap<String, String>());
In order to access the inner map from the main - you must declare the returned value of GetMap to be Map<String, HashMap<String, String>> instead of just raw type. Otherwise, the outer class won't know that the outer map value is also a hash map.
Instead of using nested maps, you should use google's Guava Table:
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Table.html

Merging Treemaps in a list

I have a List<Map<String, Object>> which contains the following maps.
Map:{clusterList=[71051], senseId=65786, totalCluster=1}
Map:{clusterList=[71051], senseId=65787, totalCluster=1}
Map:{clusterList=[4985, 71052], senseId=65788, totalCluster=2}
Map:{clusterList=[125840,153610,167812, 65787, 204091, 32586, 65786], senseId=71051, totalCluster=7}
Map:{clusterList=[11470, 65788], senseId=71052, totalCluster=2}
I have traversed the map and checked if senseId is present in the clusterList. But traversing each clusterList with senseId is taking long time with traditional for loop and also I am unable to get the merged list as the following
Map:{clusterList=[125840,153610,167812, 65787, 204091, 32586, 65786], senseId=71051, totalCluster=7}
Map:{clusterList=[4985,11470, 65788], senseId=71052, totalCluster=2}
I cant even remove the map with sensId present in the clusterList as it throws as concurrent operation exception.
Any ideas how to get to the result other than for loop as this list is very small so for loop still works. But I have list with 180 map entries and its hard to traverse the whole list and merge the maps.
I am stuck because the senseId of one map is present in clusterList of other map. So not able to merge them with simple search.
I'm rather sure that the problem is still underspecified. For example, it is not clear how to decide about the final senseId. When you have two maps
Map:{clusterList=[123,456,789], senseId=123, totalCluster=1}
Map:{clusterList=[123,456,666], senseId=456, totalCluster=1}
then (if I understood you correctly) they should be merged. But it is not clear whether the result should be a map
Map:{clusterList=[123,456,789,666], senseId=123, totalCluster=1}
or a map
Map:{clusterList=[123,456,789,666], senseId=456, totalCluster=1}
Apart from that, it seems like the "totalCluster" is the size of the cluster list. This means that it is most likely unnecessary, and if it is not unnecessary, you'd have to specify how it should be treated when two maps are merged.
However, here is a basic approach: One can create a map from senseId to maps with this senseId, and afterwards collect the maps that contain a certain senseId in their cluster list in order to find out which maps have to be merged.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class MapMergeTest
{
public static void main(String[] args)
{
List<Map<String, Object>> maps = createInput();
System.out.println("Input:");
for (Map<String, Object> map : maps)
{
System.out.println(map);
}
List<Map<String, Object>> result = createMergedMapsList(maps);
System.out.println("Result:");
for (Map<String, Object> map : result)
{
System.out.println(map);
}
}
private static List<Map<String, Object>> createInput()
{
List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
// senseId clusterList...
maps.add(createMap(65786, 71051));
maps.add(createMap(65787, 71051));
maps.add(createMap(65788, 4985, 71052));
maps.add(createMap(71051, 125840, 153610, 167812,
65787, 204091, 32586, 65786));
maps.add(createMap(71052, 11470, 65788));
return maps;
}
private static Map<String, Object> createMap(
Integer senseId, Integer ... clusters)
{
Map<String, Object> result = new LinkedHashMap<String, Object>();
result.put("senseId", senseId);
result.put("clusterList", new ArrayList<Integer>(Arrays.asList(clusters)));
return result;
}
private static List<Map<String, Object>> createMergedMapsList(
List<Map<String, Object>> maps)
{
Map<Integer, Map<String, Object>> senseIdToMap =
createSenseIdToMap(maps);
Map<Integer, Map<String, Object>> copy =
new LinkedHashMap<Integer, Map<String,Object>>(senseIdToMap);
for (Entry<Integer, Map<String, Object>> e : copy.entrySet())
{
Integer senseId = e.getKey();
Map<String, Object> map = e.getValue();
List<Integer> clusterList = getClusterList(map);
List<Map<String, Object>> mapsToMerge =
new ArrayList<Map<String,Object>>();
mapsToMerge.add(map);
for (Integer cluster : clusterList)
{
Map<String, Object> mapToMerge =
senseIdToMap.get(cluster);
if (mapToMerge != null)
{
mapsToMerge.add(mapToMerge);
senseIdToMap.remove(cluster);
}
}
if (mapsToMerge.size() > 1)
{
Map<String, Object> mergedMap = mergeMaps(mapsToMerge);
List<Integer> mergedClusterList = getClusterList(mergedMap);
mergedClusterList.remove(senseId);
senseIdToMap.put(senseId, mergedMap);
}
}
return new ArrayList<Map<String,Object>>(senseIdToMap.values());
}
private static Map<Integer, Map<String, Object>> createSenseIdToMap(
List<Map<String, Object>> maps)
{
Map<Integer, Map<String, Object>> senseIdToMap =
new LinkedHashMap<Integer, Map<String,Object>>();
for (Map<String, Object> map : maps)
{
Integer senseId = (Integer)map.get("senseId");
senseIdToMap.put(senseId, map);
}
return senseIdToMap;
}
private static Map<String, Object> mergeMaps(List<Map<String, Object>> list)
{
Map<String, Object> mergedMap = new LinkedHashMap<String, Object>();
Map<String, Object> firstMap = list.get(0);
mergedMap.put("senseId", firstMap.get("senseId"));
Set<Integer> mergedClusterList = new LinkedHashSet<Integer>();
for (Map<String, Object> map : list)
{
List<Integer> clusterList = getClusterList(map);
mergedClusterList.addAll(clusterList);
}
mergedMap.put("clusterList", new ArrayList<Integer>(mergedClusterList));
return mergedMap;
}
private static List<Integer> getClusterList(Map<String, Object> map)
{
Object object = map.get("clusterList");
return (List<Integer>)object;
}
}

How to Convert Stream Stream<HashMap<String, Object>> to HashMap Array HashMap<String, Object>[]?

I am newbie in Java 8 Streams. Please advice, how to Convert Stream Stream<HashMap<String, Object>> to HashMap Array HashMap<String, Object>[] ?
For example, I has some stream in code:
Stream<String> previewImagesURLsList = fileNames.stream();
Stream<HashMap<String, Object>> imagesStream = previewImagesURLsList
.map(new Function<String, HashMap<String, Object>>() {
#Override
public HashMap<String, Object> apply(String person) {
HashMap<String, Object> m = new HashMap<>();
m.put("dfsd", person);
return m;
}
});
How I can do something like
HashMap<String, Object>[] arr = imagesStream.toArray();
?
Sorry my bad English.
The following should work. Unfortunately, you have to suppress the unchecked warning.
#SuppressWarnings("unchecked")
HashMap<String, Object>[] arr = imagesStream.toArray(HashMap[]::new);
The expression HashMap[]::new is an array constructor reference, which is a kind of method reference. Method references provide an alternative way to implement functional interfaces. You can also use a lambda expression:
#SuppressWarnings({"unchecked", "rawtypes"})
HashMap<String, Object>[] array = stream.toArray(n -> new HashMap[n]);
Before Java 8, you would have used an anonymous inner class for that purpose.
#SuppressWarnings({"unchecked", "rawtypes"})
HashMap<String, Object>[] array = stream.toArray(new IntFunction<HashMap[]>() {
public HashMap[] apply(int n) {
return new HashMap[n];
}
});

Why does this Hashmap Iteration not work? I get a NullPointer Exception

Map<String, String> listOfIndexes = INDEXED_TABLES.get(tableName);
Iterator it = listOfIndexes.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
System.out.println(pairs.getKey());
}
My Hashmap is like this :
public static Map<String, Map<String, String>> INDEXED_TABLES = new HashMap<String, Map<String, String>>()
{{
put("employee", EMPLOYEE);
}};
public static Map<String, String> EMPLOYEE = new HashMap<String, String>()
{{
put("name", "Test");
put("age", "Test");
put("sex", "test");
}};
This is because you outsmarted yourself: your initializers depend on the order of execution. At the time this line runs
put("employee", EMPLOYEE);
EMPLOYEE is still null, so that's what gets put into your Map<String,Map<String,String>>.
You can switch the order of initializers to fix this problem. However, you would be better if you put initialization code into a separate initializer, rather than using anonymous classes with custom initializers:
public static Map<String, Map<String, String>> INDEXED_TABLES = new HashMap<String, Map<String, String>>();
public static Map<String, String> EMPLOYEE = new HashMap<String, String>();
static {
EMPLOYEE.put("name", "Test");
EMPLOYEE.put("age", "Test");
EMPLOYEE.put("sex", "test");
INDEXED_TABLES.put("employee", EMPLOYEE);
}
It looks like you are putting EMPLOYEE into the map before it has been initialized, so it will be null (and remain so, even if you assign something to EMPLOYEE later).
Reverse the order of the two statements.
Or, while in general I disapprove of the double-brace-initializer (hopefully, we'll get proper Collection literals in Java 8):
public static Map<String, Map<String, String>> INDEXED_TABLES =
new HashMap<String, Map<String, String>>(){{
put("employee", new HashMap<String, String>(){{
put("name", "Test");
put("age", "Test");
put("sex", "test");
}}
}}

Categories