简体   繁体   English

将字符串与arraylist元素进行比较并获取另一个arraylist元素

[英]Compare string with arraylist element and get another arraylist element

public ArrayList<String> endDuration(String searchedKey, ArrayList<ArrayList<String>> keys, ArrayList<ArrayList<String>> values) {
    for (int i = 0; i < keys.size() && i < values.size(); i++) {
        if (searchedKey.equals(keys.get(i))) {
            return values.get(i);
        }
    }
    return null;
}

I am having two lists both are string lists. 我有两个列表都是字符串列表。 Also I am having one string. 我也有一个字符串。

I want to compare the string with first list of all strings if any of the list values matches with string. 如果列表值中的任何一个与字符串匹配,我想将字符串与所有字符串的第一个列表进行比较。 I want to get string value from second list in that position. 我想从该位置的第二个列表中获取字符串值。

Eample: 范例:

ArrayList<String> strvalue = (a,b,c,d,e)
ArrayList<String> intValues = (1,2,3,4.5)
String str=c;

I want to get integer value 3. 我想获取整数值3。

Pretty Simple stuff. 很简单的东西。 You can use the following code: 您可以使用以下代码:

public static List<String> endDuration(String searchedKey, List<List<String>> keys, List<List<String>> values) {
    Iterator<List<String>> itKey = keys.iterator();
    Iterator<List<String>> itValue = values.iterator();
    Set<String> uniqueKeys = new HashSet<>();

    while (itKey.hasNext() && itValue.hasNext()) {
        List<String> v = itValue.next();
        uniqueKeys.clear();
        uniqueKeys.addAll(itKey.next());

        if (uniqueKeys.contains(searchedKey))
            return v;
    }

    return Collections.emptyList();
}

Pretty Simple stuff. 很简单的东西。 You can use the following code: 您可以使用以下代码:

public int getSecondListValue(ArrayList<String> list1, ArrayList<Integer> list2, String compare) {

    for (int i = 0; i < list1.size() && i < list2.size(); i++) {
        if (list1.get(i).equals(compare)) 
            return list2.get(i);
    }
    return null;
}

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

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