java 如何比较两个ArrayList并输出值

sqyvllje  于 9个月前  发布在  Java
关注(0)|答案(2)|浏览(76)

不使用GSON。
如果myFilterObj对象中的“chore”数组中有匹配的值,我将尝试从"chores"数组中提取文本值。

{
    "chores": [
        {
            "text": "Wash the car",
            "value": 1
        },
        {
            "text": "Mow the lawn",
            "value": 2
        },
        {
            "text": "Vaccumm the floor",
            "value": 3
        },
        {
            "text": "Walk the dog",
            "value": 4
        }
    ],
    "myFilterObj": {
        "chore": [
            1,
            2
        ]
    }
}

字符串
我做的第一件事就是把它们转换成数组列表。
下面是两个ArrayList。我的目标是如果第一个ArrayList中的值在第二个ArrayList中,则将第一个ArrayList中的文本重新排序。
例如:洗车和Vibrum地板应该是空的,因为1和2在第二个数组列表中,1和2是第一个数组列表中的值。
这是我目前为止所做的。我一直在尝试对allChores.value进行比较。我想我应该使用removeAll或contains??

JSONArray allChores = data.getJSONArray("chores");
JSONArray myChores = myFilterObj.getJSONArray("chore"); 
ArrayList<Object> listdata1 = new ArrayList<Object>();
ArrayList<Object> listdata2 = new ArrayList<Object>();
if (allChores != null) {
    for (int i = 0; i < allChores.length(); i++) {
        //Adding each element of JSON array into ArrayList
        listdata1.add(allChores.get(i));
    }
}
if (myChores != null) {
    for (int i = 0; i < myChores.length(); i++) {
        //Adding each element of JSON array into ArrayList
        listdata2.add(myChores.get(i));
    }
}
//comapare allChores.value to myChores(value)
//printout allChores.text on match
[ 
    {
        "text":"Wash the car",
        "value":1
    }, 
    {
        "text":"Mow the lawn",
        "value":7
    }, 
    {
        "text":"Vaccumm the floor",
        "value":2
    }, 
    {
        "text":"Walk the dog",
        "value":8
    }
]
[
    1,
    2,
    3
]
klr1opcd

klr1opcd1#

下面的答案是在假设可以使用GSON的情况下编写的。
首先,我会将输入转换为POJO。为此,我会为输入中的每个对象引入单独的记录ChoreMcChoresInput本身。

record Chore(int value, String text) {
}

record MyChores(List<Integer> chore) {
}

record Input(List<Chore> chores, MyChores myFilterObj) {
}

字符串
这样你就可以创建查找Map的家务

public static void main(String[] args) {
    final var data = """
            {
                "chores": [
                    {
                        "text": "Wash the car",
                        "value": 1
                    },
                    {
                        "text": "Mow the lawn",
                        "value": 2
                    },
                    {
                        "text": "Vaccumm the floor",
                        "value": 3
                    },
                    {
                        "text": "Walk the dog",
                        "value": 4
                    }
                ],
                "myFilterObj": {
                    "chore": [
                        1,
                        2,
                        7
                    ]
                }
            }
            """;
    final var input = new Gson().fromJson(data, Input.class);
    final Map<Integer, String> choresLookup = input.chores().stream()
            .collect(Collectors.toMap(Chore::value, Chore::text));
    final var myChores = input.myFilterObj().chore().stream()
            .filter(choresLookup::containsKey) // absent values gets filtered out
            .map(choresLookup::get)
            .toList();
}


导致

[Wash the car, Mow the lawn]

8yoxcaq7

8yoxcaq72#

有点矫枉过正,但我的首选解决方案是为这些杂务创建一个查找Map,然后遍历ID以生成标签。

private static Map<Integer, String> makeLookupMap(JSONArray chores) {
    final var result = HashMap.newHashMap(chores.length()); // new since Java 19
    for (Object entry : chores) {
        JSONObject chore = (JSONObject) entry;
        int id = chore.getInt("value");
        String label = chore.getString("text");
        result.put(id, label);
    }
    return result;
}

字符串
然后,输入的处理或多或少与我的GSON解决方案相同,但更多的过程处理,因为JSONArays已经是一个Iterable,否则它需要转换两次(Iterable -> List -> Stream)。

public static void main(String[] args) {
    final var data = ""/* as before */;
    final JSONObject input = new JSONObject(data);
    final JSONArray chores = input.getJSONArray("chores");
    final Map<Integer, String> choresLookup = makeLookupMap(chores);
    final List<String> myChores = new ArrayList<>();
    final var choreIds = input.getJSONObject("myFilterObj").getJSONArray("chore");
    for (Object choreId : choreIds) {
        final var id = (int) choreId;
        if (choresLookup.containsKey(id)) {
            myChores.add(choresLookup.get(id));
        }
    }
    System.out.println(myChores);
}


这也导致

[Wash the car, Mow the lawn]

相关问题