在List中模糊搜索keyword

public class Test {

    public static boolean contains(String name, Collection 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 list = new ArrayList();
        list.add("12345678");
        list.add("a12345678");
        list.add("a78462979");
        System.out.println("#" + contains("777", list));
        System.out.println("#" + contains("2979", list));
    }
}

你可能感兴趣的:(在List中模糊搜索keyword)