将地图内容存储到java中的另一个地图中

时间:2015-06-21 09:02:02

标签: java hashmap

我有以下java类

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

    }

我有下面的地图,如下所示,我明确地创建了列表

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));

以下是上述程序的输出,如下所示

output :-
*******

B1 = com.asd.ert.DataStruc@1394894
B2 = com.asd.ert.DataStruc@1394894

现在我想检索名为HashMap的上述newdatamap的值,因为我想将此格式存储在另一个名为finalmap的地图中。请告知如何实现此目标?< / p>

让我说finalmap声明就像

Map<String , String> finalmap = new HashMap<String , String>();

因此,如果newdatamap.keyset等于B1,则应将以下内容存储在finalmap中。请告知如何实现此目标

TradRefr                TradeRef
tr1                     TradeRef //class member name declartions
TS                       TMS 
TMSW                     TMS     //class member name declartions

2 个答案:

答案 0 :(得分:1)

试试这个:

    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是java.util.AbstractMap的公共内部类。

答案 1 :(得分:0)

  

TMSW TMS // 班级成员名称声明

所以你想在这里提取类DataStruc的字段名称。在java中只有一种方法可以使用反射API。

您的DataStruc班级

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

    }

致电课程。

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());

    }

}

运行Main class

后的输出
TUYTMSW-TMS
tr1-TradeRef
TradRefrtsy-TradeRef
TWES-TMS
TradRefr-TradeRef
TMSW-TMS
tr1ty-TradeRef
TS-TMS