List of usage examples for java.util List get
E get(int index);
From source file:Main.java
/** * Finds the first index of a maximal element in a list. * @param list The <code>List</code> to search. * @return The first index of a maximal element in the list. *///from w ww .j av a 2s. com public static <T extends Comparable<? super T>> int indexOfMax(List<T> list) { T min = list.get(0); int index = 0; for (int i = 0, n = list.size(); i < n; i++) { T value = list.get(i); if (value.compareTo(min) > 0) { min = value; index = i; } } return index; }
From source file:Main.java
@Nullable public static <T> T getOrNull(List<T> list, int position) { try {/*from w w w.j a va 2 s . co m*/ return list.get(position); } catch (Exception e) { return null; } }
From source file:com.textocat.textokit.commons.util.TrainDevTestCorpusSplitter.java
private static <T> T getAndRemove(List<T> list, int index) { T result = list.get(index); list.remove(index);//from w w w .j av a 2 s. c om return result; }
From source file:Main.java
/** * Provides the first item from a list, unless the list is <tt>null</tt> or empty, in which case <tt>null</tt> is * returned. This is useful because the behaviour of List.get(0) is to throw an exception is the list is empty. * * @param list The list to take the first item from. * @param <T> The type of the items in the list. * * @return The first item from a list, unless the list is <tt>null</tt> or empty, in which case <tt>null</tt> is * returned.//from w w w . j av a2 s.c o m */ public static <T> T first(List<T> list) { return list.isEmpty() || list == null ? null : list.get(0); }
From source file:Main.java
public static <T> T last(Collection<T> c) { if (isEmpty(c)) return null; if (c instanceof List) return (T) ((List<T>) c).get(c.size() - 1); List<T> a = new ArrayList<T>(c); return a.get(a.size() - 1); }
From source file:Main.java
public static <T> void fillArray(T[] arr, List<T> col) { for (int i = 0; i < arr.length; i++) { arr[i] = col.get(i); }// w w w . jav a2s . c o m }
From source file:io.github.bunnyblue.droidfix.classcomputer.gradleImpl.GradleImpl15.java
public static void extract() { File srcDir = new File(Configure.getInstance().getProguardJarFolder()); Collection<File> jars = FileUtils.listFiles(srcDir, new String[] { "jar" }, true); List<File> jarsList = (List<File>) jars; File jar = jarsList.get(0); String extractClasses = jar.getParentFile().getAbsolutePath() + File.separator + jar.getName().substring(0, jar.getName().indexOf(".jar")); Configure.getInstance().setTransformedClassDir(extractClasses); File targetFile = new File(extractClasses); try {/*from w ww. j a v a2 s. c o m*/ FileUtils.deleteDirectory(targetFile); } catch (IOException e) { e.printStackTrace(); } ZipUtil.unpack(jar, targetFile); }
From source file:Main.java
/** * Creates a map by taking the items from "in", as the key and value alternatly. * @param in The data for the map./*from ww w . ja v a 2 s . co m*/ * @param <T> The type of the data, the resulting map will be <T,T> * @return the map with keys and values. */ public static <T> Map<T, T> mapFromIterable(final Iterable<T> in) { Map<T, T> properties = Maps.newHashMap(); for (List<T> pair : Iterables.partition(in, 2)) { properties.put(pair.get(0), pair.get(1)); } return properties; }
From source file:Main.java
/** * Swap item in <code>list</code> at position <code>firstIndex</code> with item at position <code>secondIndex</code> * * @param list The list in which to swap the items. * @param firstIndex The position of the first item in the list. * @param secondIndex The position of the second item in the list. *//* w w w . j ava 2 s . c om*/ public static void swap(List list, int firstIndex, int secondIndex) { Object firstObject = list.get(firstIndex); Object secondObject = list.get(secondIndex); list.set(firstIndex, secondObject); list.set(secondIndex, firstObject); }
From source file:Main.java
public static <T> T next(List<T> items, T t) { int i = items.indexOf(t); return i != -1 ? items.get((i + 1) % items.size()) : null; }