Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package de.invation.code.toval.misc;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import de.invation.code.toval.file.FileWriter;
import de.invation.code.toval.math.CombinationsCalculator;
import de.invation.code.toval.math.Permutations;
import de.invation.code.toval.types.HashList;
public class ListUtils {
private static java.util.Random rand = new Random();
/**
* Returns a random element of the given list.
* @param Type of list elements
* @param list List
* @return Random element of list
*/
public static T getRandomItem(List list){
return list.get(rand.nextInt(list.size()));
}
/**
* Inserts a header
* @param
* @param list
* @param headerValue
* @param headerSize
*/
public static void insertHeader(List list, T headerValue, int headerSize){
for(int i=0; inull-value entries for every missing intermediate integer value.
* @param list List containing integer values
* @return An expanded list containing null values for missing intermediate integer values
*/
public static List fillUpWithNulls(List list){
return fillUpWithNulls(list, null);
}
/**
* Expands an integer list to a size equal to its value range
* and adds null-value entries for every missing intermediate integer value.
* If replace is not null, all original values are replaced by replace.
* @param list List containing integer values
* @param replace Replacement for existing values within list
* @return An expanded list containing null values for missing intermediate integer values
* and optionally replaced original values
*/
public static List fillUpWithNulls(List list, Integer replace){
Collections.sort(list);
int minValue = list.get(0);
int maxValue = list.get(list.size()-1);
int range = (int) (maxValue-Math.signum(minValue)*Math.abs(minValue));
ArrayList result = new ArrayList(range+1);
for(int i=0; i void swapElements(List list, T element1, T element2){
int index1 = list.indexOf(element1);
int index2 = list.indexOf(element2);
if(index1 == -1 || index1 == -1){
return;
}
Collections.swap(list, index1, index2);
}
/**
* Returns a new list containing all elements of the original list but exclude
* @param Type of list elements
* @param list Basic list for operation
* @param exclude Element to exclude
* @return A new List containing all elements of the original list but exclude
*/
public static List getListWithout(List list, T exclude){
List result = new ArrayList(list.size());
for(T t: list)
if(!t.equals(exclude))
result.add(t);
return result;
}
/**
* Returns a new list containing all elements of the original list but the elements in exclude
* @param Type of list elements
* @param list Basic list for operation
* @param exclude Elements to exclude
* @return A new List containing all elements of the original list but the elements in exclude
*/
public static List getListWithout(List list, List exclude){
List result = new ArrayList(list.size());
for(T t: list)
if(!exclude.contains(t))
result.add(t);
return result;
}
public static boolean containsOnlyNulls(List list){
for(T t: list)
if(t!=null)
return false;
return true;
}
/**
* Converts a list to an array of the same type.
* @param Type of list elements
* @param list Basic list for operation
* @return An array of the same type containing all elements of list
*/
@SuppressWarnings("unchecked")
public static T[] asArray(List list) {
return (T[]) list.toArray();
}
/**
* Creates a mutable list containing n copies of value.
* @param Type of list elements
* @param value Basic value for list generation
* @param n Number of copies
* @return A mutable list containing n copies of value
*/
public static List createList(T value, int n){
ArrayList result = new ArrayList(n);
for(int i=0; isize incrementing integer values beginning with begin.
* @param size Number of integer values within the result list
* @param begin First integer to start with
* @return A list containing incrementing integer values beginning with the specified value
*/
public static List createAndInitializeList(int size, int begin){
ArrayList result = new ArrayList(size);
for(int i=begin; icuts.
* Cuts are interpreted in an inclusive way, which means that a single cut at position i
* divides the given list in 0...i-1 + i...n
* This method deals with both cut positions including and excluding start and end-indexes
* @param Type of list elements
* @param list The list to divide
* @param cuts Cut positions for divide operations
* @return A list of sublists of list according to the given cut positions
*/
public static List> divideList(List list, Integer... cuts) {
Arrays.sort(cuts);
int c = cuts.length;
if(cuts[0]<0 || cuts[c-1]>list.size()-1)
throw new IllegalArgumentException();
int startIndex = cuts[0]==0 ? 1 : 0;
if(cuts[c-1]!=list.size()-1) {
cuts = Arrays.copyOf(cuts, cuts.length+1);
cuts[cuts.length-1] = list.size()-1;
c++;
}
List> result = new ArrayList>(c-startIndex);
int lastEnd = 0;
for(int i=startIndex; i<=c-1; i++) {
int c2 = icuts.
* Cuts are interpreted in an inclusive way, which means that a single cut at position i
* divides the given list in 0...i-1 + i...n
* This method deals with both cut positions including and excluding start and end-indexes
* @param list List to divide
* @param cuts Cut positions for divide operations
* @return A list of sublists of list according to the given cut positions
* @see #divideList(List, Integer...)
*/
public static List> divideObjectList(List