Predicate operations set using Collection

Java 8 from the Collection is a new collection of removeIf (Predicate filter) method, which will delete all the elements in line with the bulk filter criteria. This method requires a Predicate object as a parameter, is also a function Predicate interface, thus Lambda expressions may be used as a parameter.

Predicate used to filter the sample collection.

public class ForeachTest {
    public static void main(String[] args) {
        // 创建一个集合
        Collection objs = new HashSet();
        objs.add(new String("中文百度搜索Java教程"));
        objs.add(new String("中文百度搜索C++教程"));
        objs.add(new String("中文百度搜索C语言教程"));
        objs.add(new String("中文百度搜索Python教程"));
        objs.add(new String("中文百度搜索Go教程"));
        // 使用Lambda表达式(目标类型是Predicate)过滤集合
        objs.removeIf(ele -> ((String) ele).length() < 12);
        System.out.println(objs);
    }
}

The above program line 11 code calls removeIf Collection collection () method bulk delete elements in the collection meet the conditions, procedures pass a Lambda expression as a filter. All the elements are less than the string length 12 is deleted. Compile and run this code, you can see the following output:

[中文百度搜索Java教程, 中文百度搜索Python教程]

Use Predicate set sufficiently simplify the calculation , it is assumed there are still the above procedure shown objs set, if the program needs the following three statistics:

	统计集合中出现“中文百度搜索”字符串的数量。
	
	统计集合中出现“Java”字符串的数量。
	
	统计集合中出现字符串长度大于 12 的数量。

Here is just a hypothesis, it may actually have more statistical needs. If using the traditional way of programming to complete these requirements, you need to perform three cycles, but with only one method to Predicate. The following code illustrates this usage.

public class ForeachTest {
    public static void main(String[] args) {
        // 创建一个集合
        Collection objs = new HashSet();
        objs.add(new String("中文百度搜索Java教程"));
        objs.add(new String("中文百度搜索C++教程"));
        objs.add(new String("中文百度搜索C语言教程"));
        objs.add(new String("中文百度搜索Python教程"));
        objs.add(new String("中文百度搜索Go教程"));
        // 统计集合中出现“中文百度搜索”字符串的数量
        System.out.println(calAll(objs, ele -> ((String) ele).contains("中文百度搜索")));
        // 统计集合中出现“Java”字符串的数量
        System.out.println(calAll(objs, ele -> ((String) ele).contains("Java")));
        // 统计集合中出现字符串长度大于 12 的数量
        System.out.println(calAll(objs, ele -> ((String) ele).length() > 12));
    }

    public static int calAll(Collection books, Predicate p) {
        int total = 0;
        for (Object obj : books) {
            // 使用Predicate的test()方法判断该对象是否满足Predicate指定的条件
            if (p.test(obj)) {
                total++;
            }
        }
        return total;
    }
}

The output is:

5 1 1

The above procedure first defines a calAll () method, which uses a set of each element is determined Predicate meets certain conditions, the conditions will be passed through the dynamic parameter Predicate. The first 11, 13 lines of code can be seen from the above program, the program was introduced to three Lambda expressions, which are the target type Predicate, so calAll () method will only meet Predicate Statistics condition book.

Published 457 original articles · won praise 94 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_45743799/article/details/104716154