List of usage examples for java.util.function IntFunction apply
R apply(int value);
From source file:Main.java
public static void main(String[] args) { IntFunction<String> i = (x) -> Integer.toString(x); System.out.println(i.apply(3).length()); }
From source file:Main.java
public static void main(String[] args) { IntFunction<Long> factorialCalc = Main::factorial; System.out.println(factorialCalc.apply(10)); }
From source file:Main.java
public static void main(String[] argv) { IntFunction<int[][]> TwoDimArrayCreator = int[][]::new; int[][] intArray = TwoDimArrayCreator.apply(5); // Creates an int[5][] array intArray[0] = new int[5]; intArray[1] = new int[5]; intArray[2] = new int[5]; intArray[3] = new int[5]; intArray[4] = new int[5]; System.out.println(Arrays.deepToString(intArray)); }
From source file:Main.java
public static void main(String[] argv) { IntFunction<int[]> arrayCreator1 = size -> new int[size]; // Creates an int array of five elements int[] intArray1 = arrayCreator1.apply(5); System.out.println(Arrays.toString(intArray1)); IntFunction<int[]> arrayCreator2 = int[]::new; int[] intArray2 = arrayCreator2.apply(5); System.out.println(Arrays.toString(intArray2)); }
From source file:Main.java
public static <T, R> R[] mapToArray(Collection<T> inputs, Function<? super T, ? extends R> mapper, IntFunction<R[]> arrayGenerator) { R[] result = arrayGenerator.apply(inputs.size()); int idx = 0;// w w w.ja v a 2s . com for (T t : inputs) { result[idx++] = mapper.apply(t); } return result; }
From source file:Main.java
public static <T> T[] toArray(Collection<T> list, IntFunction<T[]> action) { return list.toArray(action.apply(list.size())); }
From source file:Main.java
/** * Converts the <code>collection</code> into an array * using the <code>generator</code> to create the array * without creating a <code>Stream</code> on the collection. * * @param collection The collection to be converted * @param generator The generator to create the empty array * @param <T> The type of the elements * * @return An array with the length of the <code>collection</code> * containing all elements of the <code>collection</code> * * @throws NullPointerException if either <code>collection</code> * or <code>generator</code> is <code>null</code> *//*from w w w .ja va 2 s .c o m*/ public static <T> T[] toArray(Collection<T> collection, IntFunction<T[]> generator) throws NullPointerException { return collection.toArray(generator.apply(collection.size())); }
From source file:Main.java
/** * Filters the inputs, maps them given the mapping function and adds them in the array provided * by the generator./*from ww w. j av a 2s . c o m*/ */ public static <T, R> R[] filterAndMapToArray(T[] inputs, Predicate<? super T> predicate, Function<? super T, ? extends R> mapper, IntFunction<R[]> arrayGenerator) { List<R> resultList = new ArrayList<>(); for (T t : inputs) { if (predicate.test(t)) { resultList.add(mapper.apply(t)); } } return resultList.toArray(arrayGenerator.apply(resultList.size())); }
From source file:at.gridtec.lambda4j.function.bi.BiIntFunction.java
/** * Creates a {@link BiIntFunction} which uses the {@code first} parameter of this one as argument for the given * {@link IntFunction}./*from w w w . j av a2 s.c o m*/ * * @param <R> The type of return value from the function * @param function The function which accepts the {@code first} parameter of this one * @return Creates a {@code BiIntFunction} which uses the {@code first} parameter of this one as argument for the * given {@code IntFunction}. * @throws NullPointerException If given argument is {@code null} */ @Nonnull static <R> BiIntFunction<R> onlyFirst(@Nonnull final IntFunction<? extends R> function) { Objects.requireNonNull(function); return (value1, value2) -> function.apply(value1); }
From source file:at.gridtec.lambda4j.function.bi.BiIntFunction.java
/** * Creates a {@link BiIntFunction} which uses the {@code second} parameter of this one as argument for the given * {@link IntFunction}.//from ww w . j a v a 2s. c om * * @param <R> The type of return value from the function * @param function The function which accepts the {@code second} parameter of this one * @return Creates a {@code BiIntFunction} which uses the {@code second} parameter of this one as argument for the * given {@code IntFunction}. * @throws NullPointerException If given argument is {@code null} */ @Nonnull static <R> BiIntFunction<R> onlySecond(@Nonnull final IntFunction<? extends R> function) { Objects.requireNonNull(function); return (value1, value2) -> function.apply(value2); }