List of usage examples for java.util List get
E get(int index);
From source file:Main.java
/** * Selects pseudo-random elements from a collection * * @param c Collection to select from * @param n Number of elements to select * @param unique Whether the selection should be unique * @return Random element(s) from collection */// www.j av a 2 s .c o m public static <E> Collection<E> randomElement(Collection<E> c, int n, boolean unique) { List<E> out = new ArrayList<>(); List<E> l = new ArrayList<>(c); while (out.size() < n) { E e = l.get(ThreadLocalRandom.current().nextInt(0, l.size())); out.add(e); if (unique) l.remove(e); } return out; }
From source file:Main.java
public static Map<String, String> convertFromString(String text) { Map<String, String> result = new LinkedHashMap<String, String>(); List<String> keyValuePairs = decode(text, ';'); for (String pair : keyValuePairs) { List<String> keyAndValue = decode(pair, '='); String key = keyAndValue.get(0); String value = keyAndValue.get(1); result.put(key, value);/*from w w w .j av a2s . c o m*/ } return result; }
From source file:Main.java
public static long[] LongListTolongArray(List<Long> lg) { long[] l = new long[lg.size()]; for (int i = 0; i < lg.size(); i++) { l[i] = lg.get(i).longValue(); }/*from ww w .ja va 2s . c om*/ return l; }
From source file:Main.java
/** * This method will return a random element of the given list. * /*from w ww . j a v a 2 s . c o m*/ * @param list * - list from which a random element will be picked up. * @return an element picked up from the supplied list. */ public static <T> T getRandomItem(List<T> list) { if (list == null || list.size() <= 0) { return null; } return list.get(rand.nextInt(list.size())); }
From source file:Main.java
/** * Method to get a single Bitmap combining multiple pieces side by side. * Pieces are combined from left to right iterating over {@code bitmapListCopy}. * * @param bitmapListCopy The List of Bitmaps' pieces. * @param numStages the maximum number of stages * @return The file Bitmap with all pieces combined. *//* www. jav a 2s.co m*/ @Deprecated public static Bitmap getCombinedBitmapByPieces(List<Bitmap> bitmapListCopy, int numStages) { Bitmap finalBitmap = bitmapListCopy.get(0); for (int i = 0; i < numStages; i++) { if (i > 0) { //skip first cycle finalBitmap = combineImagesSideBySide(finalBitmap, bitmapListCopy.get(i)); } } return finalBitmap; }
From source file:Main.java
public static <T> T getLastElement(List<T> liste) { if (isEmpty(liste)) { return null; } else {//w w w. j a v a2 s . c om return liste.get(liste.size() - 1); } }
From source file:Main.java
public static <T> T lastOrDefault(List<T> source) { if (source == null || source.size() == 0) return null; return source.get(source.size() - 1); }
From source file:Main.java
public static String componentsJoinedByString(List<String> list, String separator) { StringBuilder sb = new StringBuilder(); sb.append(list.get(0)); int length = list.size(); for (int i = 1; i < length; i++) { sb.append(separator).append(list.get(i)); }/*from ww w . ja va2 s.c om*/ return sb.toString(); }
From source file:Main.java
public static <A, B> List<Pair<A, B>> zip(final List<A> as, final List<B> bs) { return IntStream.range(0, Math.min(as.size(), bs.size())).mapToObj(i -> new Pair<>(as.get(i), bs.get(i))) .collect(Collectors.toList()); }
From source file:Main.java
public static String[] listToArray(List<String> l) { String[] a = new String[l.size()]; for (int i = 0; i < l.size(); i++) { a[i] = l.get(i); }/*w w w. jav a 2 s. c o m*/ return a; }