判断集合中的元素前缀满足多个标准之一
public static void main(String[] args) {
//这里代表标准: 以boy,girl,car开头的String
String[] strs = new String[]{"boy", "girl", "car"};
//要求如果List中的元素符合标准,则把符合标准的元素组成一个新的List
List<String> stringList = new ArrayList<>();
stringList.add("boy_xueweiping");
stringList.add("guona_ss");
stringList.add("girl_guona");
stringList.add("car_jeep");
stringList.add("boy_wangzhi");
stringList.add("wangyanbo");
//这里用了java8 的stream
List<String> collect = stringList.stream().filter(x -> Stream.of(strs).collect(Collectors.toList()).contains(x.substring(0, x.indexOf("_") == -1 ? 0 : x.indexOf("_")))).collect(Collectors.toList());
System.out.println(collect);
}
输出: