将Map的以下数据结构转换为java

时间:2015-06-19 15:18:00

标签: java

我基本上是在尝试创建以下数据结构:

  Map<String, Map<String, List<String>>>

我想要如下所示:

Key         Value

B1          payerName  ----> "wpn", "wpfnb", "dgeft", "xbthy"
            fixedRate ----->"dd", "ww", "trrty", "httyure"

B2          payerName  ----> "SSSwpn", "wpfSSSnb", "GGGdgeft", "xbtYYYYhy"
            fixedRate ----->"WWdd", "wTTYw", "YYYYtrrty", "IIIhttyure"

所以基本上我尝试的是但请告诉我是否有任何其他方式或数据结构我可以通过链表或树实现相同的以下功能请告知

Map<String, Map<String, List<String>>> ultimateMap = new HashMap<String,Map<String, List<String>>> ();

    Map<String, List<String>> map1 = new HashMap<String, List<String>>();
    map1.put ("payerName", Arrays.asList("wpn", "wpfnb", "dgeft", "xbthy"));
    map1.put ("fixedRate", Arrays.asList("dd", "ww", "trrty", "httyure"));

    Map<String, List<String>> map2 = new HashMap<String, List<String>>();
    map2.put ("payerName", Arrays.asList("SSSwpn", "wpfSSSnb", "GGGdgeft", "xbtYYYYhy"));
    map2.put ("fixedRate", Arrays.asList("WWdd", "wTTYw", "YYYYtrrty", "IIIhttyure"));

    ultimateMap.put("B1", map1);
    ultimateMap.put("B2", map2);

告诉我是否有任何其他方式或数据结构,我可以通过链表或树实现相同的以下功能请告知

3 个答案:

答案 0 :(得分:0)

Map<String, Map<String, List<String>>> ultimateMap = new HashMap<String,Map<String, List<String>>> ();

是实现最终地图的好方法。我不认为有更好的方法。

答案 1 :(得分:0)

我根据你的问题创建了两个数据结构。希望这有助于你的想法

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class TestClass {

public static void main(String[] args) {
    new TestClass().LoadData();
}

private void LoadData() {

    //1 Assuming payerName List and fixedRate list are of different sizes
    Map<String, PayerDataModel1> detailMap = new HashMap<>();
    PayerDataModel1 p1 = new PayerDataModel1();
    p1.payerName.add("p1");
    p1.fixedRate.add("f1");
    p1.payerName.add("p2");
    p1.fixedRate.add("f2");
    p1.payerName.add("p3");
    p1.fixedRate.add("f3");
    p1.payerName.add("p4");
    p1.fixedRate.add("f4");

    detailMap.put("B1", p1);

    p1 = new PayerDataModel1();
    p1.payerName.add("pp1");
    p1.fixedRate.add("ff1");
    p1.payerName.add("pp2");
    p1.fixedRate.add("ff2");
    p1.payerName.add("pp3");
    p1.fixedRate.add("ff3");
    p1.payerName.add("pp4");
    p1.fixedRate.add("ff4");

    detailMap.put("B2", p1);

    System.out.println(detailMap.get("B1").fixedRate.get(0));
    System.out.println(detailMap.get("B1").payerName.get(0));

    System.out.println(detailMap);
    //2 Assuming each payerName has a fixedRate [so list are of same sizes]
    Map<String, PayerDataModel2> detailMap2 = new HashMap<>();
    PayerDataModel2 payerdata = new PayerDataModel2();
    payerdata.addNew("p1", "f1");
    payerdata.addNew("p2", "f2");
    payerdata.addNew("p3", "f3");

    detailMap2.put("B1", payerdata);

    payerdata = new PayerDataModel2();
    payerdata.addNew("pp1", "ff1");
    payerdata.addNew("pp2", "ff2");
    payerdata.addNew("pp3", "ff3");
    detailMap2.put("B2", payerdata);

    System.out.println(detailMap2.get("B1").payerDataList.get(0).payerName);

    System.out.println(detailMap2);
}
}

class PayerDataModel1 {

ArrayList<String> payerName = new ArrayList<>();
ArrayList<String> fixedRate = new ArrayList<>();

@Override
public String toString() {
    return "payerName - " + payerName + " fixedRate - " + fixedRate;
}
 }

 class PayerDataModel2 {

ArrayList<PayerDataModelDetail> payerDataList = new ArrayList<>();

public void addNew(String payerName, String fixedRate) {
    PayerDataModelDetail p = new PayerDataModelDetail();
    p.fixedRate = fixedRate;
    p.payerName = payerName;
    payerDataList.add(p);
}

@Override
public String toString() {
    return payerDataList.toString();
}
 }

class PayerDataModelDetail {

String payerName;
String fixedRate;

@Override
public String toString() {
    return "payerName - " + payerName + " fixedRate - " + fixedRate;
}

}

第一个模型的输出

{B2=payerName - [pp1, pp2, pp3, pp4] fixedRate - [ff1, ff2, ff3, ff4], B1=payerName - [p1, p2, p3, p4] fixedRate - [f1, f2, f3, f4]}

第二个模型的输出

{B2=[payerName - pp1 fixedRate - ff1, payerName - pp2 fixedRate - ff2, payerName - pp3 fixedRate - ff3], B1=[payerName - p1 fixedRate - f1, payerName - p2 fixedRate - f2, payerName - p3 fixedRate - f3]}

答案 2 :(得分:0)

您似乎只需要一张从String映射到List<String>的地图。如果您不想将名称与每个地图相关联,只需将它们封装在包装类中即可。

我会创建一个CustomMap类,其中包含String mapNameMap<String, List<String>> payerAndRateMap

class CustomMap
{
    private String mapName;
    private Map<String, List<String>> payerAndRateMap;

    CustomMap(String mapName, Map<String, List<String>> payerAndRateMap)
    {
        this.mapName = mapName;
        this.payerAndRateMap = payerAndRateMap;  
    }

    public String getMapName()
    {
        return mapName;
    }

    public Map<String, List<String>> getPayerAndRateMap()
    {
        return payerAndRateMap;
    }
}

然后我会按以下方式创建此CustomMap的实例:

Map<String, List<String>> payerAndRateMap = new HashMap<>();
payerAndRateMap.put("payerName", Arrays.asList("wpn", "wpfnb", "dgeft", "xbthy"));
payerAndRateMap.put("fixedRate", Arrays.asList("dd", "ww", "trrty", "httyure"));

CustomMap customMap1 = new CustomMap("B1", payerAndRateMap);

这样您的客户端代码会更好地读取,例如:

customMap1.getPayerAndRateMap().get("payerName");

对读者来说更清醒,而不是说:

ultimateMap.get("B1").get("xyz");