简体   繁体   English

将 String 与 ArrayList 的元素进行比较并返回 String

[英]Compare a String to the elements of the ArrayList and return a String

I have an arrayList which I created using this code:我有一个使用此代码创建的 arrayList:

public List<String> Apps = new ArrayList<String>();

This list contains strings in this format:此列表包含以下格式的字符串:

Data1+Description1
Data2+Description2
Data3+Description3
Data4+Description4
Data5+Description5
.... 

Assume that the strings above have 3 parts.假设上面的字符串有 3 个部分。 1st part is the Data part.第一部分是数据部分。 2nd part is the + symbol and the 3rd part is the Description part .第二部分是+ 符号,第三部分是描述部分

and I have another function somewhere else in the code which returns a string.我在代码的其他地方有另一个函数,它返回一个字符串。 Now this string is of the form:现在这个字符串的形式是:

Retrieved_Name

This Retrieved_Name will always match with one of the Data1...Data n above .ie it will match with the 1st part of the above string till before the + symbol.Retrieved_Name将始终与上面的 Data1...Data n 之一匹配。即它将与上述字符串的第一部分匹配,直到 + 符号之前。

NOTE : This Retrieved_Name string always matches with the above strings till the + sign.注意:此 Retrieved_Name 字符串始终与上述字符串匹配,直到出现 + 号。 and not after that.而不是在那之后。

My problem is that whenever the Retrieved_Name matches with the 1st part of the above String, I need to return the 3rd part .ie the description part of the string.我的问题是,每当 Retrieved_Name 与上述字符串的第一部分匹配时,我需要返回第三部分。即字符串的描述部分。

How can I implement this?我该如何实施?

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        String s="anupam+singh"; /* here the string is composed of three parts*/
        String s1="anupam"; /* this is the first part of the string, in your case it is retrieved name */
        
        if(s.substring(0,s.indexOf("+")).equals(s1)) /*we use a substring function of String class, it takes two arguments starting and end index and returns the substring from the given string, the end index is exclusive*/
        {
            String s2=s.substring(s.indexOf("+")+1); /*the substring function can work on one argument even, if you just give the starting index then it will return the substring from that starting index till the end of string */
            System.out.println(s2);
        }
    }
}

I have explained the code in comments.我已经在评论中解释了代码。 Hope it helps.希望能帮助到你。

I think you should use Map to retrieve value rather than looping through ArrayList.我认为您应该使用 Map 来检索值,而不是通过 ArrayList 循环。

This would be perfect candidate for HashMap.这将是 HashMap 的完美候选者。

AppUtils应用程序

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AppUtils {
    public static Map<String, String> hMap = null;
    public static String TOKEN = "\\+";

    public static void main(String[] args) {

        List<String> apps = new ArrayList<String>();
        apps.add("Data1+Description1");
        apps.add("Data2+Description2");
        apps.add("Data3+Description3");
        apps.add("Data4+Description4");

        populateKeyValue(apps);
        String retrieved_Name = "Data1";
        System.out.println("Key : "+ retrieved_Name + ", Value : "+hMap.get(retrieved_Name));
        retrieved_Name = "Data3";
        System.out.println("Key : "+ retrieved_Name + ", Value : "+hMap.get(retrieved_Name));
    }

    public static void populateKeyValue(List<String> list) {
        hMap = list.stream().map(s -> s.split(TOKEN, -1))
                .filter(strings -> strings.length == 2)
                .collect(HashMap::new, (hashMap, str) -> hashMap.put(str[0],str[1]), HashMap::putAll);
    }
}

Output输出

Key : Data1, Value : Description1键:数据1,值:描述1

Key : Data3, Value : Description3键:数据3,值:描述3

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

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