【问题标题】:Compare a String to the elements of the ArrayList and return a String将 String 与 ArrayList 的元素进行比较并返回 String
【发布时间】:2017-11-15 10:42:05
【问题描述】:

我有一个使用此代码创建的数组列表:

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

此列表包含以下格式的字符串:

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

假设上面的字符串有 3 个部分。第一部分是数据部分。第二部分是+符号,第三部分是描述部分

我在代码的其他地方有另一个函数,它返回一个字符串。现在这个字符串的形式是:

Retrieved_Name

这个 Retrieved_Name 将始终与上面的 Data1...Data n 之一匹配。即它将与上述字符串的第一部分匹配,直到 + 符号之前。

注意:这个 Retrieved_Name 字符串始终与上述字符串匹配,直到出现 + 号。而不是在那之后。

我的问题是,每当 Retrieved_Name 与上述字符串的第一部分匹配时,我需要返回第三部分。即字符串的描述部分。

我该如何实现?

【问题讨论】:

  • 你遍历列表,与第一部分比较,如果相等,返回第三部分?
  • @TimCastelijns 是的,这正是他要问的。

标签: java list arraylist


【解决方案1】:
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 arguements 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 arguement 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);
        }


    }
}

我已经解释了cmets中的代码。希望对您有所帮助。

【讨论】:

    【解决方案2】:

    我认为你应该使用 Map 来检索值,而不是通过 ArrayList 循环。

    这将是 HashMap 的完美候选。

    应用程序

    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);
        }
    }
    

    输出

    键:数据1,值:描述1

    键:数据3,值:描述3

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-28
      • 2014-03-01
      • 2017-08-30
      • 1970-01-01
      • 2019-10-19
      • 2013-10-18
      • 2020-07-16
      相关资源
      最近更新 更多