package template; import com.google.common.collect.Lists; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.Predicate; import org.apache.commons.collections.Transformer; import org.apache.commons.collections.functors.NOPTransformer; import org.apache.commons.collections.functors.StringValueTransformer; import org.apache.commons.collections.functors.TruePredicate; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.Collection; import java.util.List; /** * Created by wangcong on 2016/7/28. */ public class CollectionUtilsTemplate { private static final Log LOG = LogFactory.getLog(CollectionUtilsTemplate.class); public static void main(String[] args) { List<Integer> list = Lists.newArrayList(); LOG.info(CollectionUtils.isEmpty(list)); list.add(1); list.add(2); list.add(3); LOG.info(CollectionUtils.isEmpty(list)); Collection select = CollectionUtils.select(list, new Predicate() { @Override public boolean evaluate(Object object) { Integer num = (Integer) object; return num > 2; } }); LOG.info(select); CollectionUtils.transform(list, new Transformer() { @Override public Object transform(Object input) { Integer num = (Integer) input; return num+1; } }); LOG.info(list); CollectionUtils.transform(list, StringValueTransformer.getInstance()); LOG.info(list); Collection collection = CollectionUtils.collect(list,NOPTransformer.getInstance()); LOG.info(collection); boolean isEqual = CollectionUtils.isEqualCollection(list,collection); LOG.info(isEqual); List<Integer> nums = Lists.newArrayList(); nums.add(2); nums.add(5); CollectionUtils.transform(nums,StringValueTransformer.getInstance()); //找出两个集合交叉的元素 Collection intersection = CollectionUtils.intersection(list,nums); LOG.info(intersection); boolean containsAny = CollectionUtils.containsAny(nums,list); LOG.info(containsAny); //找出在list存在,在nums不存在的元素集合 Collection subtract = CollectionUtils.subtract(list,nums); LOG.info(subtract); Collection union = CollectionUtils.union(nums,list); LOG.info(union); //nums集合中有一个符合Predicate即return true boolean exists = CollectionUtils.exists(nums, TruePredicate.getInstance()); LOG.info(exists); //返回任何集合形式的长度、collection、map、Iterator、数组、Enum等 int size = CollectionUtils.size(nums); LOG.info(size); } }
时间: 2024-11-07 12:28:10