[英]Storing the map contents into another map in java
I have the below java class 我有下面的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
}
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? 现在我想检索上面名为
newdatamap
HashMap
的值,因为我想以这种格式将其存储在另一个名为finalmap
地图中。请告知如何实现此目标?
lets say my finalmap
declartion is like 可以说我的
finalmap
声明就像
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 因此,如果
newdatamap.keyset
等于B1,则以下内容应存储在finalmap
。请告知如何实现此目的
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. 类SimpleEntry是java.util.AbstractMap的公共内部类。
TMSW TMS // class member name declartions
TMSW TMS // 类成员名称声明
So you want to extract field names here of class DataStruc
. 因此,您要在此处提取类
DataStruc
字段名称。 There is only one way to do that in java, which is using reflection API. 在Java中,只有一种方法可以使用反射API。
Your DataStruc
class 您的
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
}
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 运行Main类后的输出
TUYTMSW-TMS
tr1-TradeRef
TradRefrtsy-TradeRef
TWES-TMS
TradRefr-TradeRef
TMSW-TMS
tr1ty-TradeRef
TS-TMS
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.