On the other hand, you can pass the filtered list to another filter, which might make it read cleaner (for example, filter on "Fiction" and then on years greater than 1995).Īs I mentioned earlier, the sorted() method accepts a Comparator functional expression. While the following expression filters only on Fiction, you can make it more complex: b -> b.getGenre().equals( "Fiction") The filter expression implicitly passes each instance in your list and allows you to return a Boolean result indicating whether it should be included. The second example expands on that logic to include a sorting function that sorts by date. The first example shows how to filter only on fiction books and then create a new list that contains the filtered books. sorted(): The sorted() method allows you to provide a Comparator implementation-the same as the Collections.sort() method-with which to sort your list.The into() method allows you to create a new collection into which the results of all expressions will be copied. into(): When you invoke a method like filter(), it returns an instance of Iterable, and you probably want to do something with the results of the filter.Listing 4 retrieves the Book's genre and only includes those books with a genre of Fiction. filter(): The filter() method allows you to filter a collection by the value contained in one (or more) of the object's fields.Listing 4 shows three new methods added to the interface: Listing 3 presents a simple POJO for a book object. If( b1.getYear() > b2.getYear() ) return 1 Įlse if( b1.getYear() = b2.getYear() ) return 0 List sortedFictionBooks = list.filter( b -> b.getGenre().equals( "Fiction") ) Filter on Fiction Books and sort by year List fictionBooks = list.filter( b -> b.getGenre().equals( "Fiction") ) List.add( new Book( "Author F", "Title 7", "Computer Science", 2000 ) ) List.add( new Book( "Author D", "Title 5", "Computer Science", 2010 ) ) List.add( new Book( "Author E", "Title 6", "Computer Science", 2000 ) ) List.add( new Book( "Author B", "Title 2", "Fiction", 1980 ) ) List.add( new Book( "Author C", "Title 3", "Fiction", 1995 ) ) List.add( new Book( "Author A", "Title 1", "Fiction", 1990 ) ) Public static void showBooks( List list ) package test Ĭollections.sort( myList, new Comparator() int compare( String a1, String a2 ) Listing 4: LambdaCollectionTest.java. Listing 1: TraditionalComparatorTest.java. Listing 1 shows how we can do this to sort a list of Strings. This is pretty straightforward, and you've probably done it dozens of times, but it's a bit cumbersome. In this section, I'll demonstrate how to use lambda expressions for functional interfaces and how to pass lambda expressions to new methods in the collection classes to perform operations on the contents of the collection.įor the first example, consider the standard mechanism in Java 7 for sorting a collection of objects: To sort, you need to create a class that implements the Comparable interface and overrides the compareTo() method. JSR 335 is large and still being developed, so I wanted to choose something to demonstrate lambda expressions that shows the value they offer and as well as the syntax, which isn't likely to change much in the next few months. To compare numbers instead of strings, the compare function can subtract bįrom a.Learn More Buy Getting Started with Lambda Expressions The default lexicographic comparator satisfies all constraints above. A comparator that always returns 0 will cause the array to not be changed at all, but is reliable nonetheless. For example, if a comparator only returns 1 and 0, or only returns 0 and -1, it will not be able to sort reliably because anti-symmetry is broken. Transitive: If compareFn(a, b) and compareFn(b, c) are both positive, zero, or negative, then compareFn(a, c) has the same positivity as the previous two.Ī comparator conforming to the constraints above will always be able to return all of 1, 0, and -1, or consistently return 0.Anti-symmetric: compareFn(a, b) and compareFn(b, a) must both be 0 or have opposite signs.Stable: The comparator returns the same result with the same pair of input.(This is important because there's no guarantee when and how the comparator will be called, so any particular call should not produce visible effects to the outside.) Pure: The comparator does not mutate the objects being compared or any external state.More formally, the comparator is expected to have the following properties, in order to ensure proper sort behavior: Object.prototype._lookupGetter_() Deprecated.Object.prototype._defineSetter_() Deprecated.Object.prototype._defineGetter_() Deprecated.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |