com.jdroid.java.collections.Lists Maven / Gradle / Ivy
package com.jdroid.java.collections;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class Lists {
// ArrayList
/**
* Creates a mutable, empty {@code ArrayList} instance.
*
* @return a new, empty {@code ArrayList}
*/
public static ArrayList newArrayList() {
return new ArrayList<>();
}
/**
* Creates a mutable {@code ArrayList} instance containing the given elements.
*
* @param elements the elements that the list should contain, in order
* @return a new {@code ArrayList} containing those elements
*/
@SafeVarargs
public static ArrayList newArrayList(E... elements) {
ArrayList list = new ArrayList<>();
Collections.addAll(list, elements);
return list;
}
public static List safeArrayList(List list) {
return list != null ? list : Lists.newArrayList();
}
/**
* Creates a mutable {@code ArrayList} instance containing the given elements.
*
* @param elements the elements that the list should contain, in order
* @return a new {@code ArrayList} containing those elements
*/
public static ArrayList newArrayList(Iterable extends E> elements) {
return (elements instanceof Collection) ? new ArrayList<>((Collection extends E>)elements)
: newArrayList(elements.iterator());
}
/**
* Creates a mutable {@code ArrayList} instance containing the given elements.
*
* @param elements the elements that the list should contain, in order
* @return a new {@code ArrayList} containing those elements
*/
public static ArrayList newArrayList(Iterator extends E> elements) {
ArrayList list = newArrayList();
while (elements.hasNext()) {
list.add(elements.next());
}
return list;
}
// LinkedList
/**
* Creates an empty {@code LinkedList} instance.
*
* @return a new, empty {@code LinkedList}
*/
public static LinkedList newLinkedList() {
return new LinkedList<>();
}
/**
* Creates a {@code LinkedList} instance containing the given elements.
*
* @param elements the elements that the list should contain, in order
* @return a new {@code LinkedList} containing those elements
*/
public static LinkedList newLinkedList(Iterable extends E> elements) {
LinkedList list = newLinkedList();
for (E element : elements) {
list.add(element);
}
return list;
}
/**
* @param list
* @param maxCount
* @return same list if size doesn't exceeds maxCount, new list with trimmed element otherwise
*/
public static List trim(List list, int maxCount) {
if (list.size() > maxCount) {
return Lists.newArrayList(list.subList(0, maxCount));
}
return list;
}
public static Boolean isNullOrEmpty(List> list) {
return (list == null) || list.isEmpty();
}
public static List filter(List unfilteredList, Predicate predicate) {
List filteredList = Lists.newArrayList();
for (T each : unfilteredList) {
if (predicate.apply(each)) {
filteredList.add(each);
}
}
return filteredList;
}
public static boolean intersect(Collection> a, Collection> b) {
return !Collections.disjoint(a, b);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy