Ignore:
Timestamp:
2016-02-08T00:01:48+01:00 (8 years ago)
Author:
bastiK
Message:

add preference type MultiMap (closes #12437)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/MultiMap.java

    r9371 r9755  
    44import java.util.ArrayList;
    55import java.util.Collection;
     6import java.util.Collections;
    67import java.util.HashMap;
    78import java.util.LinkedHashSet;
     
    4344
    4445    /**
     46     * Constructs a new {@code MultiMap} from an ordinary {@code Map}.
     47     * @param map0 the {@code Map}
     48     */
     49    public MultiMap(Map<A, Set<B>> map0) {
     50        if (map0 == null) {
     51            map = new HashMap<>();
     52        } else {
     53            map = new HashMap<>(Utils.hashMapInitialCapacity(map0.size()));
     54            for (Entry<A, Set<B>> e : map0.entrySet()) {
     55                map.put(e.getKey(), new LinkedHashSet<>(e.getValue()));
     56            }
     57        }
     58    }
     59
     60    /**
    4561     * Map a key to a value.
    4662     *
     
    216232    public int hashCode() {
    217233        return Objects.hash(map);
     234    }
     235
     236    /**
     237     * Converts this {@code MultiMap} to a {@code Map} with {@code Set} values.
     238     * @return the converted {@code Map}
     239     */
     240    public Map<A, Set<B>> toMap() {
     241        Map<A, Set<B>> result = new HashMap<>();
     242        for (Entry<A, Set<B>> e : map.entrySet()) {
     243            result.put(e.getKey(), Collections.unmodifiableSet(e.getValue()));
     244        }
     245        return result;
    218246    }
    219247
Note: See TracChangeset for help on using the changeset viewer.