Esempio n. 1
1
 @Override
 public <C> Map<C, Array<T>> groupBy(Function<? super T, ? extends C> classifier) {
   return foldLeft(
       HashMap.empty(),
       (map, t) -> {
         final C key = classifier.apply(t);
         final Array<T> values = map.get(key).map(ts -> ts.append(t)).getOrElse(of(t));
         return map.put(key, values);
       });
 }
Esempio n. 2
1
 @SuppressWarnings("unchecked")
 @Override
 default <C> Map<C, Seq<T>> groupBy(Function<? super T, ? extends C> classifier) {
   Objects.requireNonNull(classifier, "classifier is null");
   if (isEmpty()) {
     return HashMap.empty();
   } else {
     return (Map<C, Seq<T>>) traverse().groupBy(classifier);
   }
 }
Esempio n. 3
0
 public static HashMap<Long, Long> factorization(long num) {
   if (num == 1) {
     return HashMap.empty();
   } else {
     return primeFactors(num)
         .map(p -> HashMap.of(Tuple.of(p, 1L)).merge(factorization(num / p), (a, b) -> a + b))
         .orElseGet(HashMap::empty);
   }
 }
Esempio n. 4
0
 @Override
 public <U, W> HashMap<U, W> map(
     BiFunction<? super K, ? super V, ? extends Tuple2<? extends U, ? extends W>> mapper) {
   Objects.requireNonNull(mapper, "mapper is null");
   return foldLeft(
       HashMap.empty(),
       (acc, entry) ->
           acc.put(
               entry.flatMap((BiFunction<K, V, Tuple2<? extends U, ? extends W>>) mapper::apply)));
 }
Esempio n. 5
0
 @Test
 public void shouldConvertToMap() {
   final Value<Integer> value = of(1, 2, 3);
   final Map<Integer, Integer> map = value.toMap(v -> Tuple.of(v, v));
   if (value.isSingleValued()) {
     assertThat(map).isEqualTo(HashMap.of(1, 1));
   } else {
     assertThat(map).isEqualTo(HashMap.empty().put(1, 1).put(2, 2).put(3, 3));
   }
 }
Esempio n. 6
0
 @Override
 public <C> Map<C, HashMap<K, V>> groupBy(Function<? super Tuple2<K, V>, ? extends C> classifier) {
   Objects.requireNonNull(classifier, "classifier is null");
   return foldLeft(
       HashMap.empty(),
       (map, entry) -> {
         final C key = classifier.apply(entry);
         final HashMap<K, V> values =
             map.get(key).map(entries -> entries.put(entry)).orElse(HashMap.of(entry));
         return map.put(key, values);
       });
 }
Esempio n. 7
0
 @Override
 public HashMap<K, V> filter(Predicate<? super Tuple2<K, V>> predicate) {
   Objects.requireNonNull(predicate, "predicate is null");
   return foldLeft(
       HashMap.<K, V>empty(),
       (acc, entry) -> {
         if (predicate.test(entry)) {
           return acc.put(entry);
         } else {
           return acc;
         }
       });
 }
Esempio n. 8
0
 @Override
 public <U, W> HashMap<U, W> flatMap(
     BiFunction<
             ? super K,
             ? super V,
             ? extends java.lang.Iterable<? extends Tuple2<? extends U, ? extends W>>>
         mapper) {
   Objects.requireNonNull(mapper, "mapper is null");
   return foldLeft(
       HashMap.<U, W>empty(),
       (acc, entry) -> {
         for (Tuple2<? extends U, ? extends W> mappedEntry : mapper.apply(entry._1, entry._2)) {
           acc = acc.put(mappedEntry);
         }
         return acc;
       });
 }
Esempio n. 9
0
 @Override
 public HashMap<K, V> clear() {
   return HashMap.empty();
 }