简体   繁体   English

将MAPS加载到LIST中-覆盖值

[英]Loading MAPS into a LIST - overwriting values

The code below is taking a string (that happens to be JSON) - converting it to a JSONObject and then iterating through the data. 下面的代码采用一个字符串(恰好是JSON)-将其转换为JSONObject,然后遍历数据。 The JSON has a schema(column) and data. JSON具有架构(列)和数据。 My end goal is to have each of the items linked and then put into the map. 我的最终目标是将每个项目链接起来,然后放入地图中。

This works perfectly when each item is distinct, like the following : 当每个项目都不同时,这非常适用,如下所示:

String jsonSTR = "{\"schema\":[\"col1\",\"col2\",\"col3\"],\"data\":[[\"banana\",\"1\",face],[\"orange\",\"2\",\"foot\"],[\"apple\",\"3\",\"hand\"]]}"; 

Output 输出量

{banana=col1, orange=col1, 1=col2, apple=col1, face=col3, 2=col2, 3=col2, foot=col3, hand=col3}

But the second duplicates are introduced (that is included in the code below) I end up overwriting values. 但是引入了第二个重复项(包含在下面的代码中),我最终覆盖了值。 I tried switching the values for the input into the list, but the same issue exists. 我尝试将输入的值切换到列表中,但是存在相同的问题。

I'm either using the wrong containers to hold this data, or I am using them incorrectly. 我使用了错误的容器来保存此数据,或者使用不正确。 I thought LISTS could hole duplicates. 我认为LISTS可能会造成重复。

Here's my current code : 这是我当前的代码:

import org.json.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class Main {

    public static void main(String[] args) {

        ArrayList<String> schemaList = new ArrayList<>();
        List<Map<String, Object>> allData = new ArrayList<Map<String, Object>>();
        Map<String,Object> partData = new HashMap<String, Object>();

        String jsonSTR = "{\"schema\":[\"col1\",\"col2\",\"col3\"],\"data\":[[\"banana\",\"orange\",2],[\"face\",\"arm\",\"eye\"],[\"yes\",\"no\",\"no\"]]}";
        JSONObject jsonObj = new JSONObject(jsonSTR);


        //Getting schema
        JSONArray schema = jsonObj.getJSONArray("schema");
        for (int i = 0; i < schema.length(); i++)
        {
            schemaList.add(schema.get(i).toString());
        }

        //Getting data
        JSONArray data = jsonObj.getJSONArray("data");

        for (int j = 0; j < data.length(); j++)
        {
            JSONArray row = data.getJSONArray(j);
            for (int k = 0; k < row.length(); k++)
            {
                partData.put(schema.get(k).toString(),row.get(k));
                allData.add(partData);
            }
        }

        System.out.println(allData.get(0));
        System.out.println(allData.get(1));

    }
}

If the data has to be in the map with the key, then no data can be repeated in any column, so it is not the best solution. 如果必须使用键将数据存储在映射中,则任何列都不能重复数据,因此这不是最佳解决方案。

public static Map<String, List<String>> toMap(String jsonSTR) {

    Map<String, List<String>> result = new HashMap<>();
    JSONObject jsonObj = new JSONObject(jsonSTR);

    JSONArray schema = jsonObj.getJSONArray("schema");
    JSONArray data = jsonObj.getJSONArray("data");

    for (int i = 0; i < schema.length(); i++) {

        String key = schema.get(i).toString();
        result.put(key, new ArrayList<>());

        for (int j = 0; j < data.length(); j++)
            result.get(key).add(((JSONArray)data.get(j)).get(i).toString());

    }
    return result;
}

In: 在:
"{\\"schema\\":[\\"col1\\",\\"col2\\",\\"col3\\",\\"col4\\"],\\"data\\":[[\\"banana\\",\\"1\\",\\"face\\",\\"black\\"],[\\"orange\\",\\"2\\",\\"foot\\",\\"red\\"],[\\"apple\\",\\"3\\",\\"hand\\",\\"yellow\\"]]}" “ {\\” schema \\“:[\\” col1 \\“,\\” col2 \\“,\\” col3 \\“,\\” col4 \\“],\\”数据\\“:[[\\”香蕉\\“,\\” 1 \\“,\\” face \\“,\\” black \\“],[\\” orange \\“,\\” 2 \\“,\\” foot \\“,\\” red \\“],[\\” apple \\“, \\“ 3 \\”,\\“手\\”,\\“黄色\\”]]}“

Out: 出:
{col4=[black, red, yellow], col2=[1, 2, 3], col3=[face, foot, hand], col1=[banana, orange, apple]} {col4 = [黑色,红色,黄色],col2 = [1、2、3],col3 = [脸,脚,手],col1 = [香蕉,橙,苹果]}

What do you think about this solution? 您如何看待该解决方案?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM