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"));
}
Related
If I am representing JSON string say as a Map<String, Object> object, how can I achieve the following?
public class Main {
public static void main(String [] args) {
Map<String, Object> properties = Map.of("k1", "v1", "k2", "v2");
Map<String, Object> map = Map.of("id", 2819, "name", "John", "properties", properties);
//map.get("properties").put("k3", "v3"); --> Doesn't work, since Object.put() doesn't exist
Map<String, Object> propCopy = (Map<String, Object>) map.get("properties"); //Works but getting an unchecked cast error
}
}
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;
I have a hashmap used to store the platform details
I need to iterate Map<String, Map<String, String>> finalmapWin8
and get the values and keys of mapWin8
values 33,8 and key BrowserType.CHROME, BrowserType.FIREFOX and BrowserType.IE
Also i want to get the key of 'finalmapWin8' "WIN8_1"
How can i iterate Map<String, Map<String, String>> finalmapWin8
static Map<String, Map<String, String>> finalmapWin8 = new HashMap<Platform, Map<String, String>>();
public static final Map<String, String> mapWin8 = new HashMap<String, String>();
static {
mapWin8.put(BrowserType.CHROME, "33");
mapWin8.put(BrowserType.FIREFOX, "33");
mapWin8.put(BrowserType.IE, "8");
}
static {
finalmapWin8.put("WIN8_1", mapWin8);
}
iteration for Map is the same. since your value for your map is also a Map then you just need another iteration to iterate through it.
for(String s: finalmapWin8.keySet()){
System.out.println(s + " : ");
for(Entry<String, String> entry : finalmapWin8.get(s).entrySet()){
System.out.println(entry);
}
}
You can use a for loop for loop for Map of type Map.Entry<String, Map<String, String>> ... well, that's it.. See the code snippet below -
import java.util.HashMap;
import java.util.Map;
public class Test {
static Map<String, Map<String, String>> finalmapWin8 = new HashMap<String, Map<String,String>>();
public static final Map<String, String> mapWin8 = new HashMap<String, String>();
static {
mapWin8.put("CHROME", "33");
mapWin8.put("FIREFOX", "33");
mapWin8.put("IE", "8");
}
static {
finalmapWin8.put("WIN8_1", mapWin8);
}
public static void main(String[] args) {
for(Map.Entry<String, Map<String, String>> entry : finalmapWin8.entrySet()) {
System.out.println(entry.getValue());
}
}
}
// get the keysets or outer map and then again run a secound loop for inner map.
for (Map.Entry<String, Map<String, String>> entry : firstMap) {
system.out.println("entry="+entry );
}
Refer to How to efficiently iterate over each Entry in a Map?
You can use below code for iterating through HashMap
for (Map.Entry<String, Map<String, String>> entry : finalmapWin8.entrySet()) {
System.out.println("Key:"+entry.getKey());
for (Map.Entry<String, String> entry1 : mapWin8.entrySet()) {
System.out.println("Keys in Second:"+ entry1.getKey()+" Values:"+ entry1.getValue());
}
}
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;
}
}
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");
}}
}}