Guava之Iterables使用示例


這是一個常量工具類。Iterables類包含了一系列的靜態方法,來操作或返回Iterable對象。

public final class Iterables { private Iterables() {} }

1.boolean removeAll(Iterable removeFrom,Collection elementsToRemove)

/** * Removes, from an iterable, every element that belongs to the provided * collection. * * <p>This method calls Collection#removeAll if iterable is a * collection, and Iterators#removeAll otherwise. */ @CanIgnoreReturnValue public static boolean removeAll(Iterable<?> removeFrom, Collection<?> elementsToRemove) { return (removeFrom instanceof Collection) ? ((Collection<?>) removeFrom).removeAll(checkNotNull(elementsToRemove)) : Iterators.removeAll(removeFrom.iterator(), elementsToRemove); }

實例:

public class Test { public static void main(String[] args) { List<String> list = Lists.newArrayList(); list.add("one"); list.add("two"); list.add("three"); List<String> list2 = Lists.newArrayList(); list2.add("two"); list2.add("four"); System.out.println(Iterables.removeAll(list, list2)); // true
        System.out.println(list.toString()); // [one, three]
 } }

2.boolean retainAll(Iterable removeFrom,Collection elementsToRetain)

/** * Removes, from an iterable, every element that does not belong to the * provided collection. * * <p>This method calls Collection#retainAll if iterable is a * collection, and Iterators#retainAll otherwise. */ @CanIgnoreReturnValue public static boolean retainAll(Iterable<?> removeFrom, Collection<?> elementsToRetain) { return (removeFrom instanceof Collection) ? ((Collection<?>) removeFrom).retainAll(checkNotNull(elementsToRetain)) : Iterators.retainAll(removeFrom.iterator(), elementsToRetain); }

實例:

public class Test { public static void main(String[] args) { List<String> list = Lists.newArrayList(); list.add("one"); list.add("two"); list.add("three"); List<String> list2 = Lists.newArrayList(); list2.add("two"); list2.add("three"); list2.add("four"); System.out.println(Iterables.retainAll(list, list2)); // true
        System.out.println(list.toString()); // [two, three]
 } }

3.boolean removeIf(Iterable removeFrom,Predicate predicate)

/** * Removes, from an iterable, every element that satisfies the provided * predicate. * * <p>Removals may or may not happen immediately as each element is tested * against the predicate. The behavior of this method is not specified if * {@code predicate} is dependent on {@code removeFrom}. */ @CanIgnoreReturnValue public static <T> boolean removeIf(Iterable<T> removeFrom, Predicate<? super T> predicate) { if (removeFrom instanceof RandomAccess && removeFrom instanceof List) { return removeIfFromRandomAccessList((List<T>) removeFrom, checkNotNull(predicate)); } return Iterators.removeIf(removeFrom.iterator(), predicate); }

實例:

public class Test { public static void main(String[] args) { List<String> list = Lists.newArrayList(); list.add("one"); list.add("three"); list.add("two"); list.add("two"); list.add("three"); Iterables.removeIf(list, new Predicate<String>() { // 移除集合中使得apply()方法返回為true的元素
 @Override public boolean apply(String input) { return input.length() == 5; } }); // [one, two, two]
 System.out.println(list.toString()); } }


免責聲明!

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



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