简体   繁体   中英

Compare a String to the elements of the ArrayList and return a String

I have an arrayList which I created using this code:

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. 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.

NOTE : This Retrieved_Name string always matches with the above strings till the + sign. 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.

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.

This would be perfect candidate for 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

Key : Data3, Value : Description3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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