Collection and Iterator interfaces

The following is a code description of how to use each method

Traversing a set using a Lambda expression

Java8 adds a new forEach default method for the Iterable interface, which requires parameters of a type that is a functional interface.

public class CollectionEach
{
    public static void main(String[] args)
    {
        // Create a collection
        Collection books = new HashSet();
        books.add("Lightweight Java EE Enterprise Application Actual Warfare");
        books.add("Insane Java Handout");
        books.add("Insane Android Handout");
        // Call the forEach() method to traverse the collection
        books.forEach(obj -> System.out.println("Iterate set elements:" + obj));
    }
}

Traversing collection elements using Java8 enhanced Iterator

public class IteratorTest
{
    public static void main(String[] args)
    {
        // The code for creating a collection and adding elements is the same as the previous program
        Collection books = new HashSet();
        books.add("Lightweight Java EE Enterprise Application Actual Warfare");
        books.add("Insane Java Handout");
        books.add("Insane Android Handout");
        // Get the iterator corresponding to the books collection
        Iterator it = books.iterator();
        while(it.hasNext())
        {
            // The data type returned by the it.next() method is Object type, so a cast is required
            String book = (String)it.next();
            System.out.println(book);
            if (book.equals("Insane Java Handout"))
            {
                // Remove elements returned by the last next method from the collection
                it.remove();
            }
            // Assigning a value to a book variable does not change the collection element itself
            book = "Test String";   //①
        }
        System.out.println(books);
    }
}

Use the new redicate action set in Java8

Java8 adds a removeIf(Predicate filter) method to the Collection collection, which will delete all elements that meet the filter criteria in bulk.This method requires a Predicate object as a parameter, and Predicate is also a functional interface, so Lambda expressions can be used as parameters.

public class PredicateTest2
{
    public static void main(String[] args)
    {
        // The code to create the books collection and add elements to the books collection is the same as the previous program.
        Collection books = new HashSet();
        books.add(new String("Lightweight Java EE Enterprise Application Actual Warfare"));
        books.add(new String("Insane Java Handout"));
        books.add(new String("Insane iOS Handout"));
        books.add(new String("Insane Ajax Handout"));
        books.add(new String("Insane Android Handout"));
        // Count the number of books whose titles contain "crazy" substrings
        System.out.println(calAll(books , ele->((String)ele).contains("Insane")));
        // Count the number of books whose titles contain Java substrings
        System.out.println(calAll(books , ele->((String)ele).contains("Java")));
        // Count the number of books whose title string length is greater than 10
        System.out.println(calAll(books , ele->((String)ele).length() > 10));
    }
    public static int calAll(Collection books , Predicate p)
    {
        int total = 0;
        for (Object obj : books)
        {
            // Use Predicate's test() method to determine if the object meets the conditions specified by Predicate
            if (p.test(obj))
            {
                total ++;
            }
        }
        return total;
    }
}

Use Java8's new Stream Action Collection

_java8 adds streaming API s such as Stream, IntStream, LongStream, DoubleStream, which represent multiple elements supporting serial and parallel aggregation operations.
_Steps to use Stream independently are as follows:
1. Use the builder() class method of Stram or XxStream to create the corresponding Builder for that Stream.
2. Repeatedly call Builder's add() method to add multiple elements to the stream.
3. Call Builder's build() method to get the corresponding Stream.
4. Call Stream's clustering method.
Stream provides a number of methods for aggregation, which can be either "intermediate" or "end".
Intermediate methods: Intermediate operations allow streams to remain open and subsequent methods to be called directly.The return value of the intermediate method is another stream.
End method: End method is the final operation of convection.When an end method is executed on a Stream, the stream will be "consumed" and no longer available.

public class CollectionStream
{
    public static void main(String[] args)
    {
        // The code for creating a books collection and adding elements to the books collection is the same as the program in section 8.2.5.
        Collection books = new HashSet();
        books.add(new String("Lightweight Java EE Enterprise Application Actual Warfare"));
        books.add(new String("Insane Java Handout"));
        books.add(new String("Insane iOS Handout"));
        books.add(new String("Insane Ajax Handout"));
        books.add(new String("Insane Android Handout"));
        // Count the number of books whose titles contain "crazy" substrings
        System.out.println(books.stream()
            .filter(ele->((String)ele).contains("Insane"))
            .count()); // Output 4
        // Count the number of books whose titles contain Java substrings
        System.out.println(books.stream()
            .filter(ele->((String)ele).contains("Java") )
            .count()); // Output 2
        // Count the number of books whose title string length is greater than 10
        System.out.println(books.stream()
            .filter(ele->((String)ele).length() > 10)
            .count()); // Output 2
        // First call the stream() method of the Collection object to convert the collection to Stream.
        // Call Stream's mapToInt() method again to get the IntStream corresponding to the original Stream
        books.stream().mapToInt(ele -> ((String)ele).length())
            // Call the forEach() method to iterate through each element in IntStream
            .forEach(System.out::println);// Output 8 11 16 7 8
    }
}

Keywords: Java Android Lambda iOS

Added by deRusett on Sun, 19 May 2019 12:29:55 +0300