Have lots of fun with Funs!
Funs class contains methods that can be used with other functions or Stream API (or other Monads).
To use it, just do static import of j8plus.Funs
.
Copy import static j8plus . Funs . * ;
isNull()
# isNull()
method returns a Predicate
which checks if the given parameter is null
.
Copy System . out . println ( isNull ( ) . test ( null ) ) ;
System . out . println ( isNull ( ) . test ( 1 ) ) ;
System . out . println ( isNull ( ) . test ( "abc" ) ) ;
It can be useful when using Stream. Copy final List < String > listOfNull =
Arrays . asList ( "a" , "b" , null , "c" , null , null , "d" )
. stream ( )
. filter ( isNull ( ) )
. collect ( toList ( ) ) ;
System . out . println ( "null found: " + listOfNull . size ( ) ) ;
isNotNull()
# isNotNull()
method returns a Predicate
which checks if the given parameter is not null
.
Copy System . out . println ( isNotNull ( ) . test ( null ) ) ;
System . out . println ( isNotNull ( ) . test ( 1 ) ) ;
System . out . println ( isNotNull ( ) . test ( "abc" ) ) ;
It can be useful when using Stream. Copy final List < String > listOfNotNullString =
Arrays . asList ( "a" , "b" , null , "c" , null , null , "d" )
. stream ( )
. filter ( isNotNull ( ) )
. collect ( toList ( ) ) ;
System . out . println ( "list of not null String values: " + listOfNotNullString ) ;
reversed()
# reversed()
returns a Comparator
which imposes the reversed order of the given Comparator.
Examples# With this simple JavaBean Copy public static class Product {
private Long id ;
private String name ;
private BigDecimal price ;
public Product price ( final BigDecimal price ) {
setPrice ( price ) ;
return this ;
}
}
Copy final List < Integer > numbers = Arrays . asList ( 4 , 2 , 5 , 3 , 1 ) ;
final Comparator < Integer > intCmp = Integer : : compareTo ;
final Comparator < Integer > reversedIntCmp = reversed ( intCmp ) ;
final List < Integer > numbersInAsc =
numbers . stream ( )
. sorted ( intCmp )
. collect ( toList ( ) ) ;
System . out . println ( "Number in ascending order: " + numbersInAsc ) ;
final List < Integer > numbersInDsc =
numbers . stream ( )
. sorted ( reversedIntCmp )
. collect ( toList ( ) ) ;
System . out . println ( "Number in descending order: " + numbersInDsc ) ;
Copy final List < Product > products =
Arrays . asList ( product ( 1L , "A" , new BigDecimal ( "30.00" ) ) ,
product ( 2L , "B" , new BigDecimal ( "12.50" ) ) ,
product ( 3L , "C" , new BigDecimal ( "5.45" ) ) ) ;
final List < Product > productsSortedByPriceInAsc =
products
. stream ( )
. sorted ( comparing ( Product : : getPrice ) )
. collect ( toList ( ) ) ;
System . out . println (
"Products sorted by price in ascending order: \n" + productsSortedByPriceInAsc
) ;
final List < Product > productsSortedByPriceInDsc =
products
. stream ( )
. sorted ( reversed ( comparing ( Product : : getPrice ) ) )
. collect ( toList ( ) ) ;
System . out . println (
"Products sorted by price in descending order: \n" + productsSortedByPriceInDsc
) ;
.sorted(BigDecimal::compareTo.reversed())
// This results in compile-time error but, the following one doesn't.Copy final List < BigDecimal > bigDecimalsInDsc =
Arrays . asList ( new BigDecimal ( "3" ) , new BigDecimal ( "1" ) , new BigDecimal ( "2" ) )
. stream ( )
. sorted ( reversed ( BigDecimal : : compareTo ) )
. collect ( toList ( ) ) ;
System . out . println ( "bigDecimalsInDsc: " + bigDecimalsInDsc ) ;
toStringOf
# toStringOf
returns a Function
which returns String. toStringOf
takes a Function
as a parameter then combines that with String::valueOf
. So it will eventually work likeCopy
String . valueOf ( f . apply ( x ) )
Copy System . out . println (
products . stream ( )
. map ( Product : : getPrice )
. collect ( joining ( ", " ) )
) ;
Copy System . out . println (
products . stream ( )
. map ( toStringOf ( Product : : getPrice ) )
. collect ( joining ( ", " ) )
) ;
satisfying
# satisfying
takes BiPredicate<O, T>
and an additional parameter of type T
then returns a Predicate<O>
. This is meant to be used with method references to simplifying filtering. It would be much clear with some examples.
Where there is a list of String and you want to filter in all String values start with a certain word. If you do it like this using lambda expressions.
Copy System . out . println (
Arrays . asList ( "Hello world" , "Hello Kevin" , "Hi world" , "Hey" , "Hello" )
. stream ( )
. filter ( s -> s . startsWith ( "Hello" ) )
. collect ( toList ( ) )
) ;
Because startWith()
method takes a parameter, you can't use method reference.
Copy . filter ( String : : startsWith ( "Hello" ) )
However, if you use satisfying()
method, you can. Just like this.
Copy System . out . println (
Arrays . asList ( "Hello world" , "Hello Kevin" , "Hi world" , "Hey" , "Hello" )
. stream ( )
. filter ( satisfying ( String : : startsWith , "Hello" ) )
. collect ( toList ( ) )
) ;
applying
# Copy final List < Product > products = Arrays . asList (
product ( 1L , "A" , $ ( "30.00" ) ) ,
product ( 2L , "B" , $ ( "12.50" ) ) ,
product ( 3L , "C" , $ ( "5.45" ) )
) ;
final BigDecimal specialPrice = new BigDecimal ( "10.00" ) ;
System . out . println (
products
. stream ( )
. map ( applying ( Product : : price , specialPrice ) )
. collect ( toList ( ) )
) ;