fdsf fgfhgfgh
fdsf fgfhgfgh

Reputation: 5

Storing the map contents into another map in 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

Upvotes: 0

Views: 90

Answers (2)

Amit.rk3
Amit.rk3

Reputation: 2417

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

Upvotes: 0

user4910279
user4910279

Reputation:

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.

Upvotes: 1

Related Questions