Guava exemplary use of FluentIterable

FluentIterable guava collection class is a class commonly used, mainly for filtering, converting data set ; FluentIterable is an abstract class that implements the Iterable interface, most of the methods return FluentIterable objects, one of which is thought to guava.

First set of configuration element type

Copy the code
public class User {
    private int age;
    private String name;

    public User() {
    }

    public User(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("User{");
        sb.append("age=").append(age);
        sb.append(", name='").append(name).append('\'');
        sb.append('}');
        return sb.toString();
    }
}
Copy the code

Common method

1. Filter (filter) element

To filter the interface receiving Predicate

Copy the code
/**
  *    Returns the elements from this fluent iterable that satisfy a predicate. 
  * The resulting fluent iterable's iterator does not support remove().
 */
public final FluentIterable<E> filter(Predicate<? super E> predicate) {
  return from(Iterables.filter(getDelegate(), predicate));
}
  /**
 * Returns the elements from this fluent iterable that are instances of class type.
 *
 */
@GwtIncompatible // Class.isInstance
public final <T> FluentIterable<T> filter(Class<T> type) {
  return from(Iterables.filter(getDelegate(), type));
}
Copy the code

Filter out the age of the user is 20 years old

Copy the code
public class Test {
    public static void main(String[] args) {
        List<User> userList = Lists.newArrayList();
        userList.add(new User(18, "zhangsan"));
        userList.add(new User(20, "lisi"));
        userList.add(new User(22, "wangwu"));
        FluentIterable<User> filter = FluentIterable.from(userList).filter(
                new Predicate<User>() {
                    @Override
                    public boolean apply(User user) {
                        return user.getAge() == 20;
                    }
        });
        for (User user : filter) {
            System.out.println(user);
        }
    }
}
Copy the code

Printing:

User{age=20, name='lisi'}

There is a potential pit, the interface inherits java.util.function.Predicate in 8 java in high version (21.0 +) of guava in Predicate

@FunctionalInterface
@GwtCompatible
public interface Predicate<T> extends java.util.function.Predicate<T>

2. The conversion (Transform) collection type , receiving Transform Function interface, usually by way of new interface callback methods apply in the method.

Copy the code
/**
 * Returns a fluent iterable that applies function to each element of this fluent
 * iterable.
 *
 * <p>The returned fluent iterable's iterator supports remove() if this iterable's
 * iterator does. After a successful remove() call, this fluent iterable no longer
 * contains the corresponding element.
 */
public final <T> FluentIterable<T> transform(Function<? super E, T> function) {
  return from(Iterables.transform(getDelegate(), function));
}
Copy the code
Copy the code
public class Test {
    public static void main(String[] args) {
        List<User> userList = Lists.newArrayList();
        userList.add(new User(18, "zhangsan"));
        userList.add(new User(20, "lisi"));
        userList.add(new User(22, "wangwu"));
        FluentIterable<String> transform = FluentIterable.from(userList).transform(
                new Function<User, String>() {
                    @Override
                    public String apply(User user) {
                        return Joiner.on(",").join(user.getName(), user.getAge());
                    }
                });
        for (String user : transform) {
            System.out.println(user);
        }
    }
}
Copy the code

Prints

zhangsan, 18 is 
Lisi, 20 is 
wangwu, 22 is

Defined Function Interface

public interface Function<F, T>

From-->To

Get all the user's age

Copy the code
public class Test {
    public static void main(String[] args) {
        List<User> userList = Lists.newArrayList();
        userList.add(new User(18, "zhangsan"));
        userList.add(new User(20, "lisi"));
        userList.add(new User(22, "wangwu"));
        List<Integer> ages = FluentIterable.from(userList).transform(
                new Function<User, Integer>() {
                    @Override
                    public Integer apply(User input) {
                        return input.getAge();
                    }
        }).toList();
        System.out.println(ages);
    }
}
Copy the code

Print results

[18, 20, 22]
Copy the code
Final the Test class {public 

    public static <F., T> void main (String [] args) { 
        List <F.> = new new fromList the ArrayList <F.> (); 
        List <T> = Result FluentIterable.from (fromList) .transform ( Function new new <F, T> () { 
            @Override 
            public Apply T (F INPUT) { 
                // a transducer may be required to write 
                // convert to type F T 
                return XXConverter.convert (INPUT); 
            } 
        }.) toList () ; 
    } 
} 
class XXConverter <F., T> { 

    public static <F., T> T Convert (F F.) { 
        return null; 
    } 
}
Copy the code

 3. Does the elements of the collection are a condition is met

Copy the code
/**
 * Returns true if every element in this fluent iterable satisfies the predicate. If this
 * fluent iterable is empty, true is returned.
 */
public final boolean allMatch(Predicate<? super E> predicate) {
  return Iterables.all(getDelegate(), predicate);
}
Copy the code
Copy the code
public class Test {
    public static void main(String[] args) {
        List<User> userList = Lists.newArrayList();
        userList.add(new User(18, "zhangsan"));
        userList.add(new User(20, "lisi"));
        userList.add(new User(22, "wangwu"));
        boolean allMatch =  FluentIterable.from(userList).allMatch(
                new Predicate<User>() {
                    @Override
                    public boolean apply(User input) {
                        return input.getAge() >= 18;
                    }
        });
        //true
        System.out.println(allMatch);
    }
}
Copy the code

4. The set of any one element can satisfy the specified condition

/**
 * Returns true if any element in this fluent iterable satisfies the predicate.
 */
public final boolean anyMatch(Predicate<? super E> predicate) {
  return Iterables.any(getDelegate(), predicate);
}
Copy the code
public class Test {
    public static void main(String[] args) {
        List<User> userList = Lists.newArrayList();
        userList.add(new User(18, "zhangsan"));
        userList.add(new User(20, "lisi"));
        userList.add(new User(22, "wangwu"));
        boolean allMatch =  FluentIterable.from(userList).anyMatch(
                new Predicate<User>() {
                    @Override
                    public boolean apply(User input) {
                        return input.getAge() >= 22;
                    }
        });
        //true
        System.out.println(allMatch);
    }
}
Copy the code

 

Reprinted from: https: //www.cnblogs.com/winner-0715/p/8412655.html

Guess you like

Origin www.cnblogs.com/PengChengLi/p/11006000.html