我有字符串数组列表,我需要找到同一数组列表中的项目之间的子字符串?
例如:
ArrayList<String> ar = new ArrayList();
ar.add("UserId"); //adding item to arraylist
ar.add("Directory");
ar.add("Username");
ar.add("PhoneNumber");
在这里我想找到项目的子字符串,基本上我需要从列表项中输出 UserId 和 Username 。我该怎么办有人可以帮我吗
你有两种方法:
ArrayList<String> ar = new ArrayList();
ar.add("UserId"); //adding item to arraylist
ar.add("Directory");
ar.add("Username");
ar.add("PhoneNumber");
// approach one using iteration
for (Iterator<String> it = ar.iterator(); it.hasNext(); ) {
String f = it.next();
if (f.equals("UserId"))
System.out.println("UserId found");
}
// approach two using a colletion which supports fetching element by key
Map<String,String> myMap=new HashMap<>();
for(String strg:ar){
myMap.put(str,str);
}
String result=myMap.get("UserId");
如果您有重复元素(例如多个“UserId”元素),则可以使用支持包的集合(具有重复元素的集合),例如 guava mutiset