Storing the map contents into another map in java - java

I have the below java class
public class DataStruc {
private List<String> TradeRef;
private List<String> TMS;
public DataStruc(List<String> TradeRef, List<String> TMS) {
setTradeRef(TradeRef);
setTMS(TMS);
}
//setters and getters for them
}
I have the below map which is as shown below and into which i am explicitly creating the list
Map<String, DataStruc> newdatamap = new HashMap<String, DataStruc>();
List<String> B1TradeRef = Arrays.asList("TradRefr", "tr1");
List<String> B1TMS = Arrays.asList("TS", "TMSW");
List<String> B2TradeRef = Arrays.asList("TradRefrtsy", "tr1ty");
List<String> B2TMS = Arrays.asList("TWES", "TUYTMSW");
newdatamap.put("B1", new DataStruc (B1TradeRef,B1TMS));
newdatamap.put("B2", new DataStruc (B2TradeRef,B2TMS));
below is the output of the above program as shown below
output :-
*******
B1 = com.asd.ert.DataStruc#1394894
B2 = com.asd.ert.DataStruc#1394894
Now I want to retrieve the value of above HashMap named newdatamap as I want to store it like this format in another map named finalmap.Please advise how to achieve this?
lets say my finalmap declartion is like
Map<String , String> finalmap = new HashMap<String , String>();
so if newdatamap.keyset is equal to B1 then following should be stored in finalmap.Please advise how to achieve this
TradRefr TradeRef
tr1 TradeRef //class member name declartions
TS TMS
TMSW TMS //class member name declartions

Try this:
Map<String , String> finalmap = newdatamap.values().stream()
.flatMap(d -> Stream.concat(
d.getTradeRef().stream().map(s -> new SimpleEntry<>(s, "TradeRef")),
d.getTMS().stream().map(s -> new SimpleEntry<>(s, "TMS"))))
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
System.out.println(finalmap);
// -> {TUYTMSW=TMS, tr1=TradeRef, TradRefrtsy=TradeRef, TWES=TMS, TradRefr=TradeRef, TMSW=TMS, TS=TMS, tr1ty=TradeRef}
class SimpleEntry is an public inner class of java.util.AbstractMap.

TMSW TMS //class member name declartions
So you want to extract field names here of class DataStruc. There is only one way to do that in java, which is using reflection API.
Your DataStruc class
import java.util.List;
public class DataStruc {
private List<String> TradeRef;
private List<String> TMS;
public DataStruc(List<String> TradeRef, List<String> TMS) {
this.TradeRef = TradeRef;
this.TMS = TMS;
} // setters and getters for them
}
Calling class.
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainClass {
public static void main(String[] args) throws IllegalArgumentException,
IllegalAccessException {
Map<String, DataStruc> newdatamap = new HashMap<String, DataStruc>();
List<String> B1TradeRef = Arrays.asList("TradRefr", "tr1");
List<String> B1TMS = Arrays.asList("TS", "TMSW");
List<String> B2TradeRef = Arrays.asList("TradRefrtsy", "tr1ty");
List<String> B2TMS = Arrays.asList("TWES", "TUYTMSW");
newdatamap.put("B1", new DataStruc(B1TradeRef, B1TMS));
newdatamap.put("B2", new DataStruc(B2TradeRef, B2TMS));
Map<String, String> finalmap = new HashMap<String, String>();
// loop through current map
for (Map.Entry<String, DataStruc> entry : newdatamap.entrySet()) {
String key = entry.getKey();
DataStruc dataStruc = entry.getValue();
// get all the fields of object dataStruc
for (Field field : dataStruc.getClass().getDeclaredFields()) {
field.setAccessible(true);
String fieldName = field.getName();
// check if field is List<String>
if (field.get(dataStruc) instanceof List) {
List<String> fieldValue = (List<String>) field.get(dataStruc);
// if yes then add the List entries to your final map with
// current field name
for (String str : fieldValue) {
finalmap.put(str, fieldName);
}
}
}
}
for (Map.Entry<String, String> entry : finalmap.entrySet())
System.out.println(entry.getKey() + "-" + entry.getValue());
}
}
Output after running Main class
TUYTMSW-TMS
tr1-TradeRef
TradRefrtsy-TradeRef
TWES-TMS
TradRefr-TradeRef
TMSW-TMS
tr1ty-TradeRef
TS-TMS

Related

Hashmap inside a hashmap with arraylist

Declaration:-
private static HashMap<String, HashMap<String, ArrayList>> parentMap = new HashMap<>();
private static HashMap<String, ArrayList> childMap = new HashMap<>();
How do I want to store data in hashmap?
"India":
"EmployeeName":[A,B,C]
"China":
"EmployeeName":[D,E,F]
Methods used:-
public static ArrayList<String> getMap(String parentkey, String childKey) {
return parentMap.get(parentkey).get(childKey);
}
public static ArrayList<String> setMap(String parentkey, String childKey, String value) {
childMap.computeIfAbsent(childKey, k -> new ArrayList<>()).add(value);
parentMap.put(parentkey, childMap);
return getMap(parentkey, childKey);
}
setMap("India", "EmployeeName", "A")
setMap("India", "EmployeeName", "B")
setMap("India", "EmployeeName", "C")
setMap("China", "EmployeeName", "D")
setMap("China", "EmployeeName", "E")
setMap("China", "EmployeeName", "F")
How data get stored and printed in hashmap while fetchng from getMap method:
System.out.println("India" + getMap("India").get("EmployeeName"));
System.out.println("China" + getMap("China").get("EmployeeName"));
"India" [A,B,C,D,E,F]
"China" [A,B,C,D,E,F]
Whilst i know keeping the childKey name unique would do thejob for me but I wish to keep the same childKey name under each parentkey name and store the respecive value in arraylist.
Any solution to my problem is welcome.
The problem is that you keep reusing the same childMap, regardless of which parentKey is being used. You need to look up the respective child map when adding values.
That means that childMap should be a local variable, nothing more. Delete your private static HashMap<String, ArrayList> childMap.
Try this:
public static ArrayList<String> setMap(String parentkey, String childKey, String value) {
HashMap<String, ArrayList> childMap = parentMap.computeIfAbsent(parentkey, k->new HashMap<>());
childMap.computeIfAbsent(childKey, k -> new ArrayList<>()).add(value);
return getMap(parentkey, childKey);
}
Proof that this works
Suggestion, don't have generic types and dont have static params
private HashMap<String, HashMap<String, ArrayList<String>>> parentMap = new HashMap<>();
private HashMap<String, ArrayList<String>> childMap = new HashMap<>();
Try to replace this method
public ArrayList<String> setMap(String parentkey, String childKey, String value) {
childMap.putIfAbsent(childKey, new ArrayList<>()); // inserts a key only if the key is not already present
childMap.get(childKey).add(value); // puts the value in the existing key and
if (!parentMap.containsKey(parentkey)) { // puts in the parent map only if not present.
parentMap.put(parentkey, childMap);
}
}
Since the childmap is referenced already, No need to put again.
If I was you I will do it in more "OOP way" so that you can benefit from static typing. Something like:
import java.util.List;
class Employee{
String name;
String getName(){
return name;
}
}
public class CompanyBranch{
String national;
List<Employee> employees;
List<String> getEmployeeAllName(){
return employees.stream().map(Employee::getName).toList();
}
}

How to check String equality while iterating over two lists?

I have two java classes:
public class MyClass1 {
private String userId;
private String userName;
private List<CustomList1> customList1;
// getters and setters
// inner CustomList1 class
}
public class MyClass2 {
private String userId;
private List<CustomList2> customList2;
// getters and setters
// inner CustomList2 class
}
Now, I have have lists of these classes:
List<MyClass1> classOneList;
List<MyClass2> classTwoList;
In both classOneList and classTwoList lists, object should be sorted with userId ascending. userId in both lists should have same values. What I want to check is that:
Has both lists same size? If not, thow error exception about.
Has every next element from both list the same userId? If not, throw another exception.
Step 1. I have done with simply if statement.
By prototype, step 2. should look like this:
for (el1, el2 : classOneList, classTwoList) {
el1.getUserId().isEqualTo(el2.getUserId());
}
Try the below code for your problem.
public class Testing {
public static void main(String[] args) {
Map<String, List<String>> map1 = new LinkedHashMap<String, List<String>>();
List<String> m1l1 = new LinkedList<String>();
m1l1.add("One");
m1l1.add("Two");
m1l1.add("Three");
m1l1.add("Four");
map1.put("1", m1l1);
List<String> m1l2 = new LinkedList<String>();
m1l2.add("One");
m1l2.add("Two");
m1l2.add("Three");
m1l2.add("Four");
map1.put("2", m1l2);
// Add more element into the map1 by creating more list.
Map<String, List<String>> map2 = new LinkedHashMap<String, List<String>>();
List<String> m2l1 = new LinkedList<String>();
m2l1.add("One");
m2l1.add("Two");
m2l1.add("Three");
m2l1.add("Four");
map2.put("1", m2l1);
// Add more element into the map2 by creating more list.
for (Entry<String, List<String>> entry : map1.entrySet()) {
if (map2.containsKey(entry.getKey())) {
if (entry.getValue().size() == map2.get(entry.getKey()).size()) {
} else {
System.out.println("UserId are same but list are different for userid: " + entry.getKey());
}
}
else {
System.out.println("Userid '"+entry.getKey()+"' exists in map1 but is not found in map2");
}
}
}
}
Hope this may help you.
if(classOneList.size() != classTwoList.size()){
throw new ErrorException();
}else{
classOneList = classOneList.stream().sorted(Comparator.comparing(MyClass1::getUserId)).collect(Collectors.toList());
classTwoList = classTwoList.stream().sorted(Comparator.comparing(MyClass2::getUserId)).collect(Collectors.toList());
for (int i = 0; i < classOneList.size(); i++){
if(!classOneList.get(i).getUserId().equals(classTwoList.get(i).getUserId())){
throw new AnotherErrorException();
}
}
}

Compare contents of HashSet against a map

I have a Map and a HashSet.
The goal is to check the contents of the Set against the Map and add it to the Map if the elements are there in the HashSet but not in the Map.
// Map is defined in a class
private final Map<String, A> sb = new ConcurrentHashMap<>();
public void someMethod() {
Set<A> hSet = new HashSet<>();
for (A a : ab){
hSet.add(a..a...);
// Check if all elements added to hash Set are there in a Map
// if not present, add it to Map
}
}
if you want to search in map values:
if(!map.values().contains(a))
// put a in the map
if you want to look for keys
if(!map.containsKey(a))
// put a in the map
keep in mind that contains calls equals so in your A class you have to implement hashCode and equals.
public static void main(String[] args) {
Set<String> set = Stream.of("a","b","c","d").collect(Collectors.toSet());
Map<String, String> map = new HashMap<String, String>();
map.put("a", "foo");
map.put("h", "bar");
map.put("c", "ipsum");
for (String string : set) {
if(!map.containsKey(string)) {
map.put(string,string);
}
}
System.out.println(map);
}
output
{a=foo, b=b, c=ipsum, d=d, h=bar}
for (String element : hSet) {
if (!sb.containsKey(element)) {
sb.put(element, A);
}
}
Following could also be solution:
private final Map<String, A> sb = new ConcurrentHashMap<>();
public void someMethod() {
Set<String> set = new HashSet<>();
set.stream().filter(word -> !sb.containsKey(word))
.forEach(word -> sb.put(word, correspondingValueOfTypeA));
}

How can i retrieve the key based on the value present in the multimap?

i have some keys which are pointing to many values in a multimap. how can i retrieve the key basing on the value present in the multimap. Here is my code.
package com.manoj;
import java.util.Set;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
public class GuavaMap
{
public static void main(String[] args)
{
Multimap regions = ArrayListMultimap.create();
regions.put("asia", "afganistan");
regions.put("asia", "bangladesh");
regions.put("asia", "inida");
regions.put("asia", "japan");
regions.put("asia", "burma");
regions.put("europe", "andorra");
regions.put("europe", "austria");
regions.put("europe", "belgium");
regions.put("europe", "cyprus");
regions.put("oceania","australia");
regions.put("oceania", "fiji");
regions.put("oceania", "nauru");
Set<String> keys = regions.keySet();
System.out.println("key\t\t\t"+"values\t\t\t");
System.out.println();
String comp = null;
for(String key : keys)
{
System.out.print(key);
System.out.println(regions.get(key));
}
}
}
the above code is providing me the output as follows
i need the region name basing on the country.
Example: if i give "australia" output should be "oceania"
you can invert it
Multimap<String, String> invregions = Multimaps.invertFrom(regions , ArrayListMultimap.<String, String>create());
and call get("yourcountry");
this will give you the keys containing your country
Firstly, I would like to suggest to not use raw types and explicitly mention Key and Value type while creating Map.
Regarding your question to retrieve key depending on value, you can iterate over all entries of the map to do the same like:
class GuavaMap
{
//Explicitly mentioned key and value both are Strings here
public static Multimap<String, String> regions = ArrayListMultimap.<String, String>create();
public static void main(String[] args)
{
regions.put("asia", "afganistan");
regions.put("asia", "bangladesh");
regions.put("asia", "inida");
regions.put("asia", "japan");
regions.put("asia", "burma");
regions.put("europe", "andorra");
regions.put("europe", "austria");
regions.put("europe", "belgium");
regions.put("europe", "cyprus");
regions.put("oceania","australia");
regions.put("oceania", "fiji");
regions.put("oceania", "nauru");
Set<String> keys = regions.keySet();
System.out.println("key\t\t\t"+"values\t\t\t");
System.out.println();
String comp = null;
for(String key : keys)
{
System.out.print(key);
System.out.println(regions.get(key));
}
//usage of below defined method
String region = getRegion("australia");
System.out.println("Region for australia:" + region);
}
// Function to get the region name i.e. key
public static String getRegion(String country){
for(Entry<String, String> entry : regions.entries()){
if(entry.getValue().equals(country))
return entry.getKey();
}
return "Not found";
}
}
Thanks for the reply.
i found a solution based on the list, map and hashmap
package com.manoj;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
public class NestedList
{
public static void main(String[] args)
{
List asia = Arrays.asList("afganistan","japan","india","pakistan","singapore","sri lanka");
List europe = Arrays.asList("albania","belarus","iceland","russia","norway","turkey");
List middleEast = Arrays.asList("australia","new zealand","samoa","tonga","vanuatu");
Map region = new HashMap<>();
region.put("asia",asia);
region.put("europe", europe);
region.put("middleeast" ,middleEast);
String reg = null;
String val = null;
for(Object key : region.keySet())
{
reg = key.toString();
Iterator it = ((List) region.get(key)).iterator();
while(it.hasNext())
{
val = it.next().toString();
if(val.equalsIgnoreCase("india"))//here you have to provide the country name
{
System.out.println(reg);
}
}
}
}
}

Convert object to Map<String, String> in java

Please suggest how to convert an Object to Map<String, String> in java.
Tried org.apache.commons.beanutils.BeanMap(object).
This is returning Map<String, Object only
How about PropertyUtils.describe(object) though it works only for Java Beans (meaning Java Objects that implement getters)
I am assuming that you want to convert "Object[][] obj" to Map. In that case here is my solution:
public static Map<String, String> convert2DArrayObjectToMap(Object[][] object)
{
Map<String, String> map = new TreeMap<>();
if (object != null)
{
for (int i = 0; i < object.length; i++)
{
if (object[i] != null)
{
for (int j = 0; j < object[i].length; j++)
{
map.putAll((TreeMap<String, String>) object[i][j]);
}
}
}
}
return map;
}
In Java, you can use the Jackson library to convert a Java object into a Map easily.
1 - Get Jackson (pom.xml)
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
2 - Convert Object to Map:
import java.util.List;
public class Student {
private String name;
private int age;
private List<String> skills;
// getters setters
}
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Arrays;
import java.util.Map;
public class ObjectToMapExample {
public static void main(String[] args) {
ObjectMapper oMapper = new ObjectMapper();
Student obj = new Student();
obj.setName("Test");
obj.setAge(34);
obj.setSkills(Arrays.asList("java","node"));
// object -> Map
Map<String, Object> map = oMapper.convertValue(obj, Map.class);
System.out.println(map);
}
}
Output:
{name=Test, age=34, skills=[java, node]}
Is there any reason for you not making it manually? It's not that much of a hassle, and you can implement it easily, something like this.
HashMap<String, String> map = new HashMap<>();
//obj represents any object you wanna put inside the map
String key = "your key";
map.put(key, obj.toString());
if the object you want to turn into a map is an iterable object such as a list you can try something like this.
ArrayList<Object> list = new ArrayList<>();
HashMap<String, String> map = new HashMap<>();
Integer i = 0;
for (Object s:list) {
map.put(String.valueOf(i), s.toString());
i++;
}
Iterate over the Map<String, Object> and use newMap.put(key, value.toString()); to fill a new Map<String, String>

Categories