Java tutorial
/** * @(#)CollectionsUtil.java 201697 * * Copyright ( c ) 2015 Wonders Information Co., Ltd. All Rights Reserved. * * This software is the confidential and proprietary information of Wonders * Information Co., Ltd. ("Confidential Information"). You shall not disclose * such Confidential Information and shall use it only in accordance with the * terms of the license agreement you entered into with Wonders Information * Co., Ltd. or a Wonders authorized reseller (the "License Agreement"). Wonders * may make changes to the Confidential Information from time to time. Such * Confidential Information may contain errors. * * EXCEPT AS EXPLICITLY SET FORTH IN THE LICENSE AGREEMENT, WONDERS DISCLAIMS ALL * WARRANTIES, COVENANTS, REPRESENTATIONS, INDEMNITIES, AND GUARANTEES WITH * RESPECT TO SOFTWARE AND DOCUMENTATION, WHETHER EXPRESS OR IMPLIED, WRITTEN OR * ORAL, STATUTORY OR OTHERWISE INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A * PARTICULAR PURPOSE. WONDERS DOES NOT WARRANT THAT END USER'S USE OF THE * SOFTWARE WILL BE UNINTERRUPTED, ERROR FREE OR SECURE. * * WONDERS SHALL NOT BE LIABLE TO END USER, OR ANY OTHER PERSON, CORPORATION OR * ENTITY FOR INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL * DAMAGES, OR DAMAGES FOR LOSS OF PROFITS, REVENUE, DATA OR USE, WHETHER IN AN * ACTION IN CONTRACT, TORT OR OTHERWISE, EVEN IF WONDERS HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. WONDERS' TOTAL LIABILITY TO END USER SHALL NOT * EXCEED THE AMOUNTS PAID FOR THE WONDERS SOFTWARE BY END USER DURING THE PRIOR * TWELVE (12) MONTHS FROM THE DATE IN WHICH THE CLAIM AROSE. BECAUSE SOME * STATES OR JURISDICTIONS DO NOT ALLOW LIMITATION OR EXCLUSION OF CONSEQUENTIAL * OR INCIDENTAL DAMAGES, THE ABOVE LIMITATION MAY NOT APPLY TO END USER. * * Copyright version 1.0 */ package com.wdhis.util; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.lang3.StringUtils; import com.google.common.primitives.Doubles; import com.google.common.primitives.Ints; import com.google.common.primitives.Longs; /** * Collections. * * ?? * * 1. ?Empty. * * 2. ?List, Guava. * * 2. ?Apache Commons Collection, ??. * * 3. ????????. * * JDKCollectionsGuavaCollections2/Iterables?, ??Collections3. * * ?: * * 1. JDK CollectionssingletonList()/singletonMap()/emptyList()/emptyMap() * * 2. Guava Lists/MapsnewArrayLists(Element...elements) * * @author calvin */ @SuppressWarnings("rawtypes") public class CollectionsUtil { /** * ?. */ public static boolean isEmpty(Collection collection) { System.out.println("3333333333333333333333"); return (collection == null) || collection.isEmpty(); } /** * ?. */ public static boolean isEmpty(Map map) { return (map == null) || map.isEmpty(); } /** * ??. */ public static boolean isNotEmpty(Collection collection) { return (collection != null) && !(collection.isEmpty()); } /** * ??. */ public static boolean isNotEmpty(Map map) { return (map != null) && !(map.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; } /** * long?List, ?Long. */ public static List<Long> asList(long... backingArray) { return Longs.asList(backingArray); } /** * int?List, ?Integer. */ public static List<Integer> asList(int... backingArray) { return Ints.asList(backingArray); } /** * double?Double, ?Double. */ public static List<Double> asList(double... backingArray) { return Doubles.asList(backingArray); } /** * ?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(); } }