【问题标题】:How to loop over an ArrayList which contains a map?如何遍历包含地图的 ArrayList?
【发布时间】:2019-01-10 13:17:45
【问题描述】:

目前我正在使用 rest-assured 命中一个端点,然后通过 ArrayList 和 HashMap 存储任何 JSON 数据匹配。

我可以看到我收到了回复,但是当 ArrayList 包含内部 HashMap 时如何循环它?

从下面的 JSON 数据中可以看出,我希望输出存储在 ArrayList 中的所有值/匹配项。

我的代码:

public void apiTest() {
    String position = "Attacker";
    String role = "PLAYER";

    Response response = given()
            .spec(footballCompetitions_requestSpecification)
            .when().get(EndPoint.TEAMS + EndPoint.SQUAD);

    ArrayList<Map<String, ?>> allPlayers = response.path
            ("squad.findAll { it.role == '%s' }.findAll  { it.position == '%s' }", position, role);

示例 JSON 数据:

 {
      "id": 66,
      "area": {
        "id": 2072,
        "name": "England"
      },
      "activeCompetitions": [
        {
          "id": 2021,
          "area": {
            "id": 2072,
            "name": "England"
          },
          "name": "Premier League",
          "code": "PL",
          "plan": "TIER_ONE",
          "lastUpdated": "2019-01-03T23:39:45Z"
        },
        {
          "id": 2001,
          "area": {
            "id": 2077,
            "name": "Europe"
          },
          "name": "UEFA Champions League",
          "code": "CL",
          "plan": "TIER_ONE",
          "lastUpdated": "2018-12-13T18:55:02Z"
        }
      ],
      "name": "Manchester United FC",
      "shortName": "Man United",
      "tla": "MNU",
      "crestUrl": "http://upload.wikimedia.org/wikipedia/de/d/da/Manchester_United_FC.svg",
      "address": "Sir Matt Busby Way Manchester M16 0RA",
      "phone": "+44 (0161) 8688000",
      "website": "http://www.manutd.com",
      "email": "enquiries@manutd.co.uk",
      "founded": 1878,
      "clubColors": "Red / White",
      "venue": "Old Trafford",
      "squad": [
        {
          "id": 3188,
          "name": "David De Gea",
          "position": "Goalkeeper",
          "dateOfBirth": "1990-11-07T00:00:00Z",
          "countryOfBirth": "Spain",
          "nationality": "Spain",
          "shirtNumber": 1,
          "role": "PLAYER"
        },
        {
          "id": 3331,
          "name": "Marcus Rashford",
          "position": "Attacker",
          "dateOfBirth": "1997-10-31T00:00:00Z",
          "countryOfBirth": "England",
          "nationality": "England",
          "shirtNumber": 10,
          "role": "PLAYER"
        },
        {
          "id": 3372,
          "name": "Anthony Martial",
          "position": "Attacker",
          "dateOfBirth": "1995-12-05T00:00:00Z",
          "countryOfBirth": "France",
          "nationality": "France",
          "shirtNumber": 11,
          "role": "PLAYER"
        },
        {
          "id": 3662,
          "name": "Romelu Lukaku",
          "position": "Attacker",
          "dateOfBirth": "1993-05-13T00:00:00Z",
          "countryOfBirth": "Belgium",
          "nationality": "Belgium",
          "shirtNumber": 9,
          "role": "PLAYER"
        },

【问题讨论】:

  • 您需要遍历 List 并在 Map 上进行嵌套迭代

标签: java rest-assured web-api-testing


【解决方案1】:

如果您使用的是 Java 8 或更高版本,则只需先迭代 List,然后使用 lambda 表达式在嵌套迭代中迭代 Map

public static void main(String[] args) {
    List<Map<String, ?>> jsonList = new ArrayList<>();

    Map<String, String> mapOne = new HashMap<String, String>();

    mapOne.put("id", String.valueOf(66));
    mapOne.put("area", "Some Area");

    jsonList.add(mapOne);

    jsonList.forEach(map -> {
        map.forEach((key, value) -> {
            System.out.println(key + ": " + value);
        });
    });
}

您需要注意的是valueString 表示,因为它在您的代码中是Map&lt;String, ?&gt;,在我的示例中是Map&lt;String, String&gt;,如果您直接使用可能会导致问题上面例子的System.out.println(key + ": " + value);

【讨论】:

  • @SamP 没问题,我很高兴提供了一些有用的东西 ;-)
【解决方案2】:

首先遍历列表,然后遍历 Map:

for (Map<String, ?> map : allPlayers) {
  for (String key : map.keySet()) {
    Object player = map.get(key);
    // ...
  }
}

【讨论】:

  • 对于地图,使用 entrySet() 迭代更有效:findbugs
  • 是的,但我认为对于 Java 初学者来说使用 keySet() 更直观,因为在迭代中进行迭代本身可能会造成混淆。
猜你喜欢
  • 2012-04-21
  • 2020-01-25
  • 2015-12-13
  • 1970-01-01
  • 1970-01-01
  • 2015-06-15
  • 1970-01-01
  • 1970-01-01
  • 2015-11-28
相关资源
最近更新 更多