如何阅读复杂的地图和集合

时间:2011-03-09 05:00:34

标签: java

在阅读格式为Map<String, Object>.的地图时遇到问题此对象可以是String / PrimitiveWrapper / List / Map。如果Object是Map,那么这个Map可以包含一个List,这个List可以包含一个Map,.....等等。这是第n级。我需要迭代Object并获取值。我尝试了不同的方法但没有成功。发生下面的帖子但是无法获得所需的输出。

Recursively reading any java Object and pulling out complex types into a hash map

任何人都可以帮助我获得所需的输出。我附上了一个示例程序,并显示了示例输出。

关于第一个输出的简要介绍:
客户是Map-Eight的关键,客户是Map-Seven的关键。 Map Seven包含一个数组Six,因此键应显示为 Customer [index]。 Six [0]是Map fiveA,其键为 Addresses 。 FiveA包含一个Map-Four,其键是 Address 。四包含一个列表 - 三,所以四个键应显示为地址[索引]。三个[0]是一个Map-One,键为 AddressLine1 ,其值为 House 1 。   所以最后的关键和价值是:
Customers.Customer [0] .Addresses.Address [0] .AddressLine1 = House 1

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

public class StructureReader {

public static void main(String[] args) {
    StructureReader reader = new StructureReader();
    Map<String, Object> firstData = reader.getFirstData();
    // After iterating the first data
    // Output should be
    // key = value
    // Customers.Customer[0].Addresses.Address[0].AddressLine1 = House 1
    // Customers.Customer[0].Addresses.Address[1].AddressLine2 = Street 1
    // Customers.Customer[1].Name.FirstName = Joe
    // Customers.Customer[1].Name.Surname = Bloggs


    Map<String, Object> secondData = reader.getSecondData();
    // After iterating the second data
    // Output should be
    // l2Map.l3List[0] = some_string
    // l2Map.l3List[1] = 123
    // l2Map.l3List[2] = 100.5
    // l2Map.l3List[3] =
    // l2Map.l3List[4] =
}

private Map getFirstData() {
    Map one = new HashMap();
    one.put("AddressLine1", "House 1");
    Map two = new HashMap();
    two.put("AddressLine2", "Street 1");

    List three = new ArrayList();
    three.add(one); // index 0
    three.add(two); // index 1

    Map four = new HashMap();
    four.put("Address", three);

    Map fiveA = new HashMap();
    fiveA.put("Addresses", four);

    Map fiveB = new HashMap();
    fiveB.put("FirstName", "Joe");
    fiveB.put("Surname", "Bloggs");

    Map fivec = new HashMap();
    fivec.put("Name", fiveB);

    List six = new ArrayList();
    six.add(fiveA); // index 0
    six.add(fivec); // index 1

    Map seven = new HashMap();
    seven.put("Customer", six);

    // Final HashMap i.e HashMap<String, Object>
    // Now the Object is seven
    Map eight = new HashMap();
    eight.put("Customers", seven);

    return eight;
}


private Map<String, Object> getSecondData() {
    Map<String, Object> inputMap = new HashMap<String, Object>();
    Map<String, List<Object>> level2Map = new HashMap<String, List<Object>>();
    List<Object> level3List = new ArrayList<Object>();
    String level4String = "some_string";
    Integer level4Integer = 123;
    Double level4Double = 100.5d;
    List<Object> level4List = new ArrayList<Object>();
    Map<String, Object> level4Map = new HashMap<String, Object>();

    level3List.add(level4String);
    level3List.add(level4Integer);
    level3List.add(level4Double);
    level3List.add(level4List);
    level3List.add(level4Map);

    level2Map.put("l3List", level3List);

    inputMap.put("l2Map", level2Map);

    return inputMap;
}

}

先谢谢。

2 个答案:

答案 0 :(得分:1)

上面提到的例子被称为原始的痴迷。您是否尝试根据域要求设计模型并使用它们而不是直接使用地图和列表(域模型可以包含列表或地图作为数据结构)。

答案 1 :(得分:0)

As per your design, i implemented logic for itteration and the output came as per the design. Hopefully it will be helpful for you.






    package com.core.java;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class StructureReader
{

    public static void main(String[] args) {
        StructureReader reader = new StructureReader();
        Map<String, Object> firstData = reader.getFirstData();
        // After iterating the first data

        //For Eight map
        Iterator iterator = firstData.entrySet().iterator();
        while(iterator.hasNext())
        {
            String output="";
           Map.Entry entry = (Entry) iterator.next();
           output=output+entry.getKey()+".";

            //for Seven Map
            Map seven=(Map) entry.getValue();
            Iterator iterator7 = seven.entrySet().iterator();
            while(iterator7.hasNext())
            {
               Map.Entry entry7 = (Entry) iterator7.next();
               output=output+entry7.getKey();
               String name=output;
              //for Six i'e List
               List six=(List) entry7.getValue();
              for(int i=0;i<six.size();i++)
              {
                  //for fiveA i'e map
                    Map fiveA=(Map) six.get(i);
                    Iterator iterator5A = fiveA.entrySet().iterator();
                    while(iterator5A.hasNext())
                    {
                       Map.Entry entry5A = (Entry) iterator5A.next();
                       if(i==0){
                           output=output+"["+i+"]."+entry5A.getKey()+".";
                       }
                       else{
                           name=name+"["+i+"]."+entry5A.getKey()+".";
                       }
                       String surname=name;  
                       Map four=(Map) entry5A.getValue();
                       Iterator iterator4 = four.entrySet().iterator();
                       while(iterator4.hasNext())
                       {
                        Map.Entry entry4 = (Entry) iterator4.next();
                        if(i==0){
                            output=output+entry4.getKey()+"";
                        }
                        else{
                            name=surname+entry4.getKey()+"";
                        }
                        String adderess=output;
                           if(i==0){
                            //for three i'e List
                               List three=(List) entry4.getValue();
                               for(int j=0;j<three.size();j++)
                               {
                                   Map onetwo=(Map) three.get(j);
                                   Iterator iterator12 = onetwo.entrySet().iterator();
                                    while(iterator12.hasNext())
                                    {
                                       Map.Entry entry12 = (Entry) iterator12.next();
                                       output=adderess+"["+j+"]."+entry12.getKey()+"";

                                       System.out.printf(output+ " = "+ entry12.getValue()+" %n");
                                    }
                               }
                           }else{
                            System.out.printf(name+ " = "+ entry4.getValue()+" %n");
                           }
                       }
                    }
              }
           }
        }
        Map<String, Object> secondData = reader.getSecondData();

        Iterator iteratMap = secondData.entrySet().iterator();
        while(iteratMap.hasNext())
        {
            String output="";
           Map.Entry entry = (Entry) iteratMap.next();
           output=output+entry.getKey()+".";

           Map Maplist=(Map) entry.getValue();

           Iterator iterator5A = Maplist.entrySet().iterator();
            while(iterator5A.hasNext())
            {
               Map.Entry entry5A = (Entry) iterator5A.next();
               output=output+entry5A.getKey()+"";
               List l=null;
               Map mp=null;
               List list=(List) entry5A.getValue();
               for(int i=0;i<list.size();i++){
                   if(list.get(i) instanceof List || list.get(i) instanceof Map){
                       if(list.get(i) instanceof List){
                           l=(List) list.get(i);
                       }else{
                           mp=(Map) list.get(i);
                       }
                       if(l.isEmpty() ||mp.isEmpty()){
                           System.out.printf(output+ "["+i+"] = %n");
                       }
                   }else{
                       System.out.printf(output+ "["+i+"] = "+ list.get(i)+" %n");
                   }

               }
            }
        }
    }


    private Map<String, Object> getSecondData() {
        Map<String, Object> inputMap = new HashMap<String, Object>();
        Map<String, List<Object>> level2Map = new HashMap<String, List<Object>>();
        List<Object> level3List = new ArrayList<Object>();
        String level4String = "some_string";
        Integer level4Integer = 123;
        Double level4Double = 100.5d;
        List<Object> level4List = new ArrayList<Object>();
        Map<String, Object> level4Map = new HashMap<String, Object>();

        level3List.add(level4String);
        level3List.add(level4Integer);
        level3List.add(level4Double);
        level3List.add(level4List);
        level3List.add(level4Map);

        level2Map.put("l3List", level3List);

        inputMap.put("l2Map", level2Map);

        return inputMap;
    }

    private Map getFirstData() {
        Map one = new HashMap();
        one.put("AddressLine1", "House 1");
        Map two = new HashMap();
        two.put("AddressLine2", "Street 1");

        List three = new ArrayList();
        three.add(one); // index 0
        three.add(two); // index 1

        Map four = new HashMap();
        four.put("Address", three);

        Map fiveA = new HashMap();
        fiveA.put("Addresses", four);

        Map fiveB = new HashMap();
        fiveB.put("FirstName", "Joe");
        fiveB.put("Surname", "Bloggs");

        Map fivec = new HashMap();
        fivec.put("Name", fiveB);

        List six = new ArrayList();
        six.add(fiveA); // index 0
        six.add(fivec); // index 1

        Map seven = new HashMap();
        seven.put("Customer", six);

        // Final HashMap i.e HashMap<String, Object>
        // Now the Object is seven
        Map eight = new HashMap();
        eight.put("Customers", seven);

        return eight;
    }   

}