0

我有以下 HashMap 数据仅打印 keySet():-

[P001, P003, P005, P007, P004, P034, P093, P054, P006]

并将以下 ArrayList 数据作为输出: -

[P001]
[P007]
[P034]
[P054]

这就是为他们俩打印的方式。我想将数组列表数据与哈希映射数据一一进行比较。因此,值 [P001] 应该出现在 HashMap 中。

这是我尝试过的代码部分:-

def count = inputJSON.hotelCode.size() // Where "hotelCode" is particular node in inputJSON

Map<String,List> responseMap = new HashMap<String, List>()
for(int i=0; i<count; i++) {
    Map jsonResult = (Map) inputJSON
    List hotelC = jsonResult.get("hotelCode")
    String id = hotelC[i].get("id")
    responseMap.put(id, hotelC[i])
}

String hotelCFromInputSheet = P001#P007#P034#P054
String [] arr  = roomProduct.split("#")
for(String a : arr) {
    ArrayList <String> list = new ArrayList<String>()
    list.addAll(a)

    log.info list
    log.info responseMap.keySet()

    if(responseMap.keySet().contains(list)) {
        log.info "Room Product present in the node"
    }
}

任何帮助,将不胜感激。

4

2 回答 2

2

您可以使用containsAllof 方法Set,它接受一个集合:

if(responseMap.keySet().containsAll(list)) {

不确定您的代码是否可以编译,但至少可以简化:

String hotelCFromInputSheet = 'P001#P007#P034#P054'
ArrayList <String> list  = Arrays.asList(roomProduct.split("#"))
boolean containsAll = responseMap.keySet().containsAll(list)
于 2018-01-02T06:42:33.100 回答
1

在这一行中,您检查 keySet 是否包含整个列表:

if (responseMap.keySet().contains(list)) {
    log.info "Room Product present in the node"
}

我认为您的意图是检查它是否包含已添加到当前正在处理的循环中的字符串:

if (responseMap.keySet().contains(a)) {
        log.info "Room Product present in the node"
}

此外,在这一行中:list.addAll(a)您实际上是在添加一个字符串,因此可以将其替换list.add(a)为使您的代码更清晰。

编辑:如果要打印ArrayList与指定键关联的字符串中存在的值,您可能想尝试使用这样的循环:

if (responseMap.keySet().contains(a)) {
    List<String> strings = responseMap.get(a);
    for (String s : strings) {
        System.out.println(s + ", ");
    }
} 
于 2018-01-02T06:47:25.260 回答