All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opentripplanner.framework.collection.MapUtils Maven / Gradle / Ivy

The newest version!
package org.opentripplanner.framework.collection;

import gnu.trove.map.TLongObjectMap;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

public class MapUtils {

  public static  void addToMapSet(TLongObjectMap> mapSet, long key, U value) {
    Set set = mapSet.get(key);
    if (set == null) {
      set = new HashSet<>();
      mapSet.put(key, set);
    }
    set.add(value);
  }

  /**
   * Map a collection of objects of type S to a list of type T using the provided
   * mapping function.
   * 

* Nullsafe: if entities is null, then null is returned. */ public static List mapToList(Collection entities, Function mapper) { return entities == null ? null : entities.stream().map(mapper).collect(Collectors.toList()); } /** * Takes a list of maps and returns the union of all of them. *

* If there are duplicate keys then the one from the later argument overwrites the value in an * earlier argument. */ @SafeVarargs public static Map combine(Map... maps) { var ret = new HashMap(); Arrays.stream(maps).forEach(ret::putAll); return Map.copyOf(ret); } }