在List中模糊搜索keyword

public class Test {

    public static boolean contains(String name, Collection<String> list) {
        Pattern pattern = Pattern.compile(name);

        for (String str : list) {
            Matcher matcher = pattern.matcher(str);
            if (matcher.find()) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("12345678");
        list.add("a12345678");
        list.add("a78462979");
        System.out.println("#" + contains("777", list));
        System.out.println("#" + contains("2979", list));
    }
}

推荐阅读更多精彩内容