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());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值