java - How to check a String for multiple items in a String ArrayList -
i have arraylist , add strings i'm checking this.
arraylist<string> al = new arraylist<string>(); al.add("is"); al.add("the");
then have method returns match found in string in arraylist.
public static string getword(string str1, arraylist<string> list) { for(int = 0; < list.size(); i++) { if(str1.tolowercase().contains(list.get(i))) { return list.get(i); } } return false; }
although when want check string has more 1 match returns fist 1 added arraylist, so
al.add("is"); al.add("the");
it return "is"
if add them this
al.add("the"); al.add("is");
it return "the"
i need way of determining how many matches there , returning them individually.
try use map hold, key string in list , value count matched
public static void main(string[] args) throws exception { list<string> list = new arraylist<string>(); list.add("aa"); list.add("bb"); list.add("cc"); list.add("dd"); list.add("ee"); list.add("ff"); list.add("kk"); list.add("ff"); system.out.println(getword("aabbccddeeff", list)); } public static map<string, integer> getword(string str1, list<string> list) { map<string, integer> map = new hashmap<string, integer>(); integer count = 1; (string match : list) { if(str1.tolowercase().contains(match)) { if(map.containskey(match)) { count = map.get(match); count++; } map.put(match, count); } } return map; }
Comments
Post a Comment