Guava之FluentIterable使用示例


FluentIterable 是guava集合類中常用的一個類,主要用於過濾、轉換集合中的數據;FluentIterable是一個抽象類,實現了Iterable接口,大多數方法都返回FluentIterable對象,這也是guava的思想之一。

首先構造集合中的元素類型

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(); } }

常用方法

1.過濾(filter)元素

filter方法要接收Predicate接口

/** * 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)); }

過濾出年齡是20歲的用戶

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); } } }

打印效果:

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

這里有一個潛在的坑,在高版本(21.0++)的guava中Predicate接口繼承了java 8中的java.util.function.Predicate

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

2.轉換(transform)集合類型,transform接收Function接口,一般在方法中采用new接口實現回調方法apply的方式。

/** * 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)); }
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); } } }

打印效果

zhangsan,18 lisi,20 wangwu,22

Function接口的定義

public interface Function<F, T>

From-->To

拿到所有用戶的年齡

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); } }

打印結果

[18, 20, 22]
public final class Test { public static <F, T> void main(String[] args) { List<F> fromList = new ArrayList<F>(); List<T> result = FluentIterable.from(fromList).transform(new Function<F, T>() { @Override public T apply(F input) { // 可以根據需要寫一個轉換器 // 將類型F轉換成T
                return XXConverter.convert(input); } }).toList(); } } class XXConverter<F, T> { public static <F, T> T convert(F f) { return null; } }

 3.集合中的元素是否都滿足某個條件

/** * 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); }
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); } }

4.集合中的任何一個元素滿足指定的條件即可

/** * 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); }
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); } }

參考:

集合中的FluentIterable類


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM