List of usage examples for java.util.function Predicate test
boolean test(T t);
From source file:Main.java
/** * Filters the inputs, maps them given the mapping function and adds them in the array provided * by the generator.//ww w . j a v a 2s . co 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:Main.java
static <E> Map<Boolean, List<E>> partitionBy(List<E> elements, Predicate<E> predicate) { //TODO Implement me Map<Boolean, List<E>> map = new HashMap<>(); List<E> trueList = new ArrayList<>(); List<E> falseList = new ArrayList<>(); for (E element : elements) { if (predicate.test(element)) { trueList.add(element);/*from w ww . jav a 2 s . c om*/ } else falseList.add(element); } map.replace(true, trueList); map.replace(false, falseList); return map; }
From source file:org.openehr.designer.util.WtUtils.java
public static <T> int indexOf(List<T> list, Predicate<T> predicate, int startIndex) { for (int i = startIndex; i < list.size(); i++) { T t = list.get(i);/*from ww w. j a v a 2 s. com*/ if (predicate.test(t)) { return i; } } return -1; }
From source file:Main.java
/** * Returns item in HashSet that matches the specified Predicate. * @param set the HashSet to check against * @param condition the Predicate to check * @param <T> the type of objects in the HashSet * @return an Optional containing the matching object, Empty if not found * @throws IllegalArgumentException if HashSet or Predicate is null *///w w w.jav a2s. c o m public static <T> Optional<T> getItemInHashSet(final HashSet<T> set, final Predicate<T> condition) { if (set == null) throw new IllegalArgumentException("HashSet cannot be null!"); if (condition == null) throw new IllegalArgumentException("Predicate cannot be null!"); for (final T object : set) if (condition.test(object)) return Optional.of(object); return Optional.empty(); }
From source file:Main.java
public static <ELEMENTTYPE> boolean containsAny(@Nullable final Iterable<? extends ELEMENTTYPE> aCollection, @Nullable final Predicate<? super ELEMENTTYPE> aFilter) { if (aFilter == null) return isNotEmpty(aCollection); if (isNotEmpty(aCollection)) for (final ELEMENTTYPE aElement : aCollection) if (aFilter.test(aElement)) return true; return false; }
From source file:Main.java
/** * Checks if a value userExists in a HashMap that matches the specified Predicate. * @param map the HashMap to check against * @param condition the Predicate to check * @param <K> the type of the Key in the HashMap * @param <V> the type of the Value in the HashMap * @return true if condition is true/*from w ww . j a va2 s. c o m*/ * @throws IllegalArgumentException if HashMap or Predicate is null */ public static <K, V> boolean doesItemExistInHashMap(final HashMap<K, V> map, final Predicate<V> condition) { if (map == null) throw new IllegalArgumentException("HashMap cannot be null!"); if (condition == null) throw new IllegalArgumentException("Predicate cannot be null!"); for (final Map.Entry<K, V> entry : map.entrySet()) if (condition.test(entry.getValue())) return true; return false; }
From source file:Main.java
/** * Check if the passed collection contains only elements matching the * predicate. An empty collection does not fulfill this requirement! If no * filter is provided the return value is identical to * {@link #isNotEmpty(Iterable)}/*from w w w . j a v a 2 s . co m*/ * * @param aCollection * The collection to check. May be <code>null</code>. * @param aFilter * Predicate to check against all elements. May not be * <code>null</code>. * @return <code>true</code> only if the passed collection is neither * <code>null</code> nor empty and if only matching elements are * contained, or if no filter is provided and the collection is not * empty. * @param <ELEMENTTYPE> * Collection data type */ public static <ELEMENTTYPE> boolean containsOnly(@Nullable final Iterable<? extends ELEMENTTYPE> aCollection, @Nullable final Predicate<? super ELEMENTTYPE> aFilter) { if (isEmpty(aCollection)) return false; if (aFilter == null) return isNotEmpty(aCollection); for (final ELEMENTTYPE aElement : aCollection) if (!aFilter.test(aElement)) return false; return true; }
From source file:Main.java
/** * Returns a HashSet of values in a HashMap that match the specified Predicate. * @param map the HashMap to check against * @param condition the Predicate to check * @param <K> the type of the Key in the HashMap * @param <V> the type of the Value in the HashMap * @return a HashSet of values matching the predicate, empty HashSet if no results found * @throws IllegalArgumentException if HashMap or Predicate is null *///from w ww.j a va 2 s. c om public static <K, V> HashSet<V> getValuesInHashMap(final HashMap<K, V> map, final Predicate<V> condition) { final HashSet<V> matchingValues = new HashSet<>(); if (map == null) throw new IllegalArgumentException("HashMap cannot be null!"); if (condition == null) throw new IllegalArgumentException("Predicate cannot be null!"); map.forEach((key, value) -> { if (condition.test(value)) matchingValues.add(value); }); return matchingValues; }
From source file:Main.java
@Nullable public static <ELEMENTTYPE> ELEMENTTYPE findFirst(@Nullable final Iterable<? extends ELEMENTTYPE> aCollection, @Nullable final Predicate<? super ELEMENTTYPE> aFilter, @Nullable final ELEMENTTYPE aDefault) { if (aFilter == null) return getFirstElement(aCollection); if (isNotEmpty(aCollection)) for (final ELEMENTTYPE aElement : aCollection) if (aFilter.test(aElement)) return aElement; return aDefault; }
From source file:com.samczsun.helios.utils.Utils.java
public static void save(File dest, Map<String, byte[]> data, Predicate<String> accept) { try {/*w ww . j a v a2 s. c o m*/ JarOutputStream out = new JarOutputStream(new FileOutputStream(dest)); Set<String> added = new HashSet<>(); for (Entry<String, byte[]> entry : data.entrySet()) { String name = entry.getKey(); if (added.add(name) && accept.test(name)) { out.putNextEntry(new ZipEntry(name)); out.write(entry.getValue()); out.closeEntry(); } } out.close(); } catch (IOException e) { ExceptionHandler.handle(e); } }