Java tutorial
/* * Copyright (C) 2008 feilong (venusdrogon@163.com) * * 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 com.feilong.commons.core.util; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.feilong.commons.core.lang.ObjectUtil; /** * ListUtil {@link List}. * * @author 2010-3-2 ?03:20:12 * @since 1.0.0 */ public final class ListUtil { /** The Constant log. */ private static final Logger log = LoggerFactory.getLogger(ListUtil.class); /** Don't let anyone instantiate this class. */ private ListUtil() { //AssertionError?. ?????. ???. //see Effective Java 2nd throw new AssertionError("No " + getClass().getName() + " instances for you!"); } /** * / el. * * @param collection * ?,?Iterator,??,?Iterator. * @param value * ?,toString . * @return true, if successful * @see ObjectUtil#toIterator(Object) * @see #isContain(Iterator, Object) * @deprecated */ @Deprecated public static boolean isContainTag(Object collection, Object value) { Iterator<?> iterator = ObjectUtil.toIterator(collection); return isContain(iterator, value); } /** * iterator???. * * @param iterator * iterator * @param value * value * @return iterator???,iteratornull/empty,false * @see Iterator#hasNext() * @see Iterator#next() * @deprecated ?? ,?? */ @Deprecated public static boolean isContain(Iterator<?> iterator, Object value) { boolean flag = false; if (Validator.isNotNullOrEmpty(iterator)) { Object object = null; while (iterator.hasNext()) { object = iterator.next(); if (object.toString().equals(value.toString())) { flag = true; break; } } } else { log.debug("iterator is null/empty"); } return flag; } /** * <code>collection</code> <code>remove</code>. ?. The cardinality of an * element <code>e</code> in the returned collection is the same as the cardinality of <code>e</code> in <code>collection</code> unless * <code>remove</code> contains <code>e</code>, in which * case the cardinality is zero.?,? <code>collection</code>?,? <code>collection.removeAll(remove);</code>. * * @param <T> * the generic type * @param collection * the collection from which items are removed (in the returned collection) * @param remove * the items to be removed from the returned <code>collection</code> * @return a <code>List</code> containing all the elements of <code>c</code> except * any elements that also occur in <code>remove</code>. * @see org.apache.commons.collections.ListUtils#removeAll(Collection, Collection) * @since Commons Collections 3.2 * @since 1.0.8 */ @SuppressWarnings("unchecked") public static <T> List<T> removeAll(Collection<T> collection, Collection<T> remove) { return org.apache.commons.collections.ListUtils.removeAll(collection, remove); } /** * <code>collection</code> <code>removeElement</code>. ?. ?,? <code>collection</code>?,? * <code>collection.remove(removeElement);</code>. * * @param <T> * the generic type * @param collection * the collection from which items are removed (in the returned collection) * @param removeElement * the remove element * @return a <code>List</code> containing all the elements of <code>c</code> except * any elements that also occur in <code>remove</code>. * @see org.apache.commons.collections.ListUtils#removeAll(Collection, Collection) * @since Commons Collections 3.2 * @since 1.0.8 */ public static <T> List<T> remove(Collection<T> collection, T removeElement) { Collection<T> remove = new ArrayList<T>(); remove.add(removeElement); return removeAll(collection, remove); } /** * ?(collection?,?collection?). * * @param <T> * the generic type * @param collection * the item src list * @return if Validator.isNullOrEmpty(collection) null<br> * else {@link ArrayList} * @see ArrayList#ArrayList(java.util.Collection) * @see LinkedHashSet#LinkedHashSet(Collection) * @see <a * href="http://www.oschina.net/code/snippet_117714_2991?p=2#comments">http://www.oschina.net/code/snippet_117714_2991?p=2#comments</a> */ public static <T> List<T> removeDuplicate(Collection<T> collection) { if (Validator.isNullOrEmpty(collection)) { return null; } // contains??. // 100Wlist0.546contains?.10W2??. // [foo1] 100000 -> 50487 : 48610 ms. // [foo2] 100000 -> 50487 : 47 ms. return new ArrayList<T>(new LinkedHashSet<T>(collection)); } /** * list. * * @param <T> * the generic type * @param list * list * @return <br> * if (Validator.isNotNullOrEmpty(list),return null */ public static <T> T getFirstItem(List<T> list) { if (Validator.isNotNullOrEmpty(list)) { return list.get(0); } return null; } // *********************************************************************************** /** * list???,[]?? ??()??,?<br> * : * * <pre> * List<String> testList = new ArrayList<String>(); * testList.add("xinge"); * testList.add("feilong"); * * toStringReplaceBrackets(testList)-----{@code >}(xinge, feilong) * </pre> * * @param list * list? * @return list???,[]?? ??()??,? */ public static String toStringReplaceBrackets(List<String> list) { return list.toString().replace('[', '(').replace(']', ')'); } /** * list???,[]??, <br> * : * * <pre> * {@code * * List<String> testList = new ArrayList<String>(); * testList.add("xinge"); * testList.add("feilong"); * * toStringRemoveBrackets(testList)----->xinge,feilong * } * </pre> * * @param list * list? * @return list???,[]??, */ public static String toStringRemoveBrackets(List<String> list) { String s = list.toString(); return s.substring(1, s.length() - 1).replaceAll(" ", ""); } /** * list??? : * * <pre> * {@code * * List<String> testList = new ArrayList<String>(); * testList.add("xinge"); * testList.add("feilong"); * * toString(testList,true)----->'xinge','feilong' * toString(testList,false)----->xinge,feilong * } * </pre> * * @param list * list? * @param isAddQuotation * ??? * @return list??? */ public static String toString(List<String> list, boolean isAddQuotation) { String returnValue = toStringRemoveBrackets(list); if (isAddQuotation) { returnValue = "'" + returnValue.replaceAll(",", "','") + "'"; } return returnValue; } }