Java tutorial
/* * Copyright (C) 2015 The Workbench Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.cloudkit.enterprises.infrastructure.utilities; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.lang3.StringUtils; import java.util.*; /** * Collections. * JDKCollectionsGuavaCollections2?, ??Collections3. * ????????Apache Commons Collection, ?? * * @author hongquanli <hongquanli@qq.com> * @version 1.0 20131121 ?2:58:10 */ public class Collections3Utility { /** * ????(Getter), ??Map. * * @param collection ???. * @param keyPropertyName ????MapKey??. * @param valuePropertyName ????MapValue??. */ public static Map<Object, Object> extractToMap(final Collection<?> collection, final String keyPropertyName, final String valuePropertyName) { Map<Object, Object> map = new HashMap<Object, Object>(collection.size()); try { for (Object obj : collection) { map.put(PropertyUtils.getProperty(obj, keyPropertyName), PropertyUtils.getProperty(obj, valuePropertyName)); } } catch (Exception e) { throw ReflectionHelper.convertReflectionExceptionToUnchecked(e); } return map; } /** * ????(Getter), ??List. * * @param collection ???. * @param propertyName ??????. */ public static List<Object> extractToList(final Collection<?> collection, final String propertyName) { List<Object> list = new ArrayList<Object>(collection.size()); try { for (Object obj : collection) { list.add(PropertyUtils.getProperty(obj, propertyName)); } } catch (Exception e) { throw ReflectionHelper.convertReflectionExceptionToUnchecked(e); } return list; } /** * ????(Getter), ??. * * @param collection ???. * @param propertyName ??????. * @param separator . */ public static String extractToString(final Collection<?> collection, final String propertyName, final String separator) { List<?> list = extractToList(collection, propertyName); return StringUtils.join(list, separator); } /** * ?Collection(toString())String, separator */ public static String convertToString(final Collection<?> collection, final String separator) { return StringUtils.join(collection, separator); } /** * ?Collection(toString())String, ???prefix??postfix<div>mymessage</div> */ public static String convertToString(final Collection<?> collection, final String prefix, final String postfix) { StringBuilder builder = new StringBuilder(); for (Object o : collection) { builder.append(prefix).append(o).append(postfix); } return builder.toString(); } /** * ?. */ public static boolean isEmpty(Collection<?> collection) { return ((collection == null) || collection.isEmpty()); } /** * ?. */ public static boolean isNotEmpty(Collection<?> collection) { return ((collection != null) && !(collection.isEmpty())); } /** * ?Collectioncollectionnull. */ public static <T> T getFirst(Collection<T> collection) { if (isEmpty(collection)) { return null; } return collection.iterator().next(); } /** * ?Collection? collectionnull. */ public static <T> T getLast(Collection<T> collection) { if (isEmpty(collection)) { return null; } // List?? if (collection instanceof List) { List<T> list = (List<T>) collection; return list.get(list.size() - 1); } // iterator?. Iterator<T> iterator = collection.iterator(); while (true) { T current = iterator.next(); if (!iterator.hasNext()) { return current; } } } /** * a+bList. */ public static <T> List<T> union(final Collection<T> a, final Collection<T> b) { List<T> result = new ArrayList<T>(a); result.addAll(b); return result; } /** * a-bList. */ public static <T> List<T> subtract(final Collection<T> a, final Collection<T> b) { List<T> list = new ArrayList<T>(a); for (T element : b) { list.remove(element); } return list; } /** * abList. */ public static <T> List<T> intersection(Collection<T> a, Collection<T> b) { List<T> list = new ArrayList<T>(); for (T element : a) { if (b.contains(element)) { list.add(element); } } return list; } }