com.squeakysand.commons.util.CollectionUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of squeakysand-commons Show documentation
Show all versions of squeakysand-commons Show documentation
Classes, interfaces and enums that assist with everyday Java development tasks.
The newest version!
/*
* Copyright 2010-2012 Craig S. Dickson (http://craigsdickson.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.squeakysand.commons.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
/**
* Utility class containing helpful methods related to manipulating Collections.
*/
public final class CollectionUtils {
/**
* Converts the values to a {@link List}.
*
* @param values
* the values to convert to a {@link List}, can be null.
* @return a {@link List} of the supplied values, which may be empty, but never null.
*/
public static List asList(T... values) {
List result = new ArrayList();
if (values != null) {
for (T tmp : values) {
result.add(tmp);
}
}
return result;
}
/**
* Converts the values to a {@link Set}.
*
* @param values
* the values to convert to a {@link Set}, can be null.
* @return a {@link Set} of the supplied values, which may be empty, but never null.
*/
public static Set asSet(T... values) {
Set result = new HashSet();
if (values != null) {
for (T tmp : values) {
result.add(tmp);
}
}
return result;
}
public static List filter(Collection collection, Predicate predicate) {
List result = new ArrayList();
for (T element : collection) {
if (predicate.evaluate(element)) {
result.add(element);
}
}
return result;
}
public static Map filter(Map source, Predicate> predicate) {
Map result = new HashMap();
for (Map.Entry entry : source.entrySet()) {
if (predicate.evaluate(entry)) {
result.put(entry.getKey(), entry.getValue());
}
}
return result;
}
public static T getFirstMatchingElement(Collection collection, Predicate super T> predicate) {
T result = null;
for (T element : collection) {
if (predicate.evaluate(element)) {
result = element;
break;
}
}
return result;
}
public static K getKeyByValue(Map map, V value) {
K result = null;
for (Map.Entry entry : map.entrySet()) {
if (value.equals(entry.getValue())) {
result = entry.getKey();
}
}
return result;
}
public static T getLastMatchingElement(Collection collection, Predicate super T> predicate) {
T result = null;
for (T element : collection) {
if (predicate.evaluate(element)) {
result = element;
}
}
return result;
}
public static List removeDuplicates(List list) {
return removeDuplicates(list, null);
}
public static List removeDuplicates(List list, Comparator comparator) {
if (list == null) {
throw new IllegalArgumentException("parameter list cannot be null");
}
Set extensionSet = new TreeSet(comparator);
extensionSet.addAll(list);
return toList(extensionSet);
}
/**
* Creates a new {@link java.util.List} object based on a {@link java.util.Set} object.
*
* @param set
* the source Collection.
* @return a new Collection containing all of the elements from the source collection.
* @param
* a T object.
*/
public static List toList(Set set) {
List list = new ArrayList();
list.addAll(set);
return list;
}
/**
* Creates a new {@link java.util.Set} object based on a {@link java.util.List} object.
*
* @param
* @param list
* the source Collection.
* @return a new Collection containing only the unique elements from the source Collection.
*/
public static Set toSet(List list) {
Set set = new HashSet();
set.addAll(list);
return set;
}
public static List values(Map source) {
List result = new ArrayList();
for (Map.Entry entry : source.entrySet()) {
result.add(entry.getValue());
}
return result;
}
/**
* Hide the constructor because this is a utility class.
*/
private CollectionUtils() {
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy