canonical example by Tabnine
private void usingArrayList() { ArrayList<String> list = new ArrayList<>(Arrays.asList("cat", "cow", "dog")); list.add("fish"); int size = list.size(); // size = 4 list.set(size - 1, "horse"); // replacing the last element to "horse" String removed = list.remove(1); // removed = "cow" String second = list.get(1); // second = "dog" }
canonical example by Tabnine
public long getDirectorySize(File file) { if (!file.exists()) { return 0; } if (file.isFile()) { return file.length(); } File[] files; if (!file.isDirectory() || (files = file.listFiles()) == null) { return 0; } return Arrays.stream(files).mapToLong(f -> getDirectorySize(f)).sum(); }
List<String> lines = Arrays.asList("The first line", "The second line"); Path file = Paths.get("the-file-name.txt"); Files.write(file, lines, Charset.forName("UTF-8")); //Files.write(file, lines, Charset.forName("UTF-8"), StandardOpenOption.APPEND);
int[] intArray = { 7, 9, 5, 1, 3 }; System.out.println(Arrays.toString(intArray));
/** * Returns a copy of {@code multiset} as an {@link ImmutableMultiset} whose iteration order is * highest count first, with ties broken by the iteration order of the original multiset. * * @since 11.0 */ @Beta public static <E> ImmutableMultiset<E> copyHighestCountFirst(Multiset<E> multiset) { Entry<E>[] entries = (Entry<E>[]) multiset.entrySet().toArray(new Entry[0]); Arrays.sort(entries, DecreasingCount.INSTANCE); return ImmutableMultiset.copyFromEntries(Arrays.asList(entries)); }
String[][] deepArray = new String[][] {{"John", "Mary"}, {"Alice", "Bob"}}; System.out.println(Arrays.toString(deepArray)); //output: [[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922] System.out.println(Arrays.deepToString(deepArray));
/** * Sort the given array with a default AnnotationAwareOrderComparator. * <p>Optimized to skip sorting for lists with size 0 or 1, * in order to avoid unnecessary array extraction. * @param array the array to sort * @see java.util.Arrays#sort(Object[], java.util.Comparator) */ public static void sort(Object[] array) { if (array.length > 1) { Arrays.sort(array, INSTANCE); } }
@Test public void testGetValuesUnbounded() { ReplaySubject<Object> rs = ReplaySubject.createUnbounded(); Object[] expected = new Object[10]; for (int i = 0; i < expected.length; i++) { expected[i] = i; rs.onNext(i); assertArrayEquals(Arrays.copyOf(expected, i + 1), rs.getValues()); } rs.onComplete(); assertArrayEquals(expected, rs.getValues()); }
@Test public void attributeNames() throws Exception { this.attributeAccessor.setAttribute(NAME, VALUE); this.attributeAccessor.setAttribute("abc", "123"); String[] attributeNames = this.attributeAccessor.attributeNames(); Arrays.sort(attributeNames); assertTrue(Arrays.binarySearch(attributeNames, NAME) > -1); assertTrue(Arrays.binarySearch(attributeNames, "abc") > -1); }
/** * Sort the given factory methods, preferring public methods and "greedy" ones * with a maximum of arguments. The result will contain public methods first, * with decreasing number of arguments, then non-public methods, again with * decreasing number of arguments. * @param factoryMethods the factory method array to sort */ public static void sortFactoryMethods(Method[] factoryMethods) { Arrays.sort(factoryMethods, EXECUTABLE_COMPARATOR); }
@Test public void testGetValues() { ReplaySubject<Object> rs = ReplaySubject.create(); Object[] expected = new Object[10]; for (int i = 0; i < expected.length; i++) { expected[i] = i; rs.onNext(i); assertArrayEquals(Arrays.copyOf(expected, i + 1), rs.getValues()); } rs.onComplete(); assertArrayEquals(expected, rs.getValues()); }
@Override public List<Integer> apply(Integer a, Integer b, Integer c) { return Arrays.asList(a, b, c); } })
double[] doubleArray = { 7.0, 9.0, 5.0, 1.0, 3.0 }; System.out.println(Arrays.toString(doubleArray));
/** * Sort the given constructors, preferring public constructors and "greedy" ones with * a maximum number of arguments. The result will contain public constructors first, * with decreasing number of arguments, then non-public constructors, again with * decreasing number of arguments. * @param constructors the constructor array to sort */ public static void sortConstructors(Constructor<?>[] constructors) { Arrays.sort(constructors, EXECUTABLE_COMPARATOR); }
@Test public void testGetValues() { ReplayProcessor<Object> rs = ReplayProcessor.create(); Object[] expected = new Object[10]; for (int i = 0; i < expected.length; i++) { expected[i] = i; rs.onNext(i); assertArrayEquals(Arrays.copyOf(expected, i + 1), rs.getValues()); } rs.onComplete(); assertArrayEquals(expected, rs.getValues()); }
@Override public List<Integer> apply(Integer t1, Integer t2, Integer t3, Integer t4) { return Arrays.asList(t1, t2, t3, t4); } });