From c0cd7d2b3a3e9507648ad9b5a5bb24bce92805a0 Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Thu, 13 Dec 2018 14:06:31 +0300 Subject: [PATCH] small optimization: more efficient intersection, cleanup --- .../util/containers/ContainerUtil.java | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/platform/util/src/com/intellij/util/containers/ContainerUtil.java b/platform/util/src/com/intellij/util/containers/ContainerUtil.java index b3b4a2921db3..1fc645e6190f 100644 --- a/platform/util/src/com/intellij/util/containers/ContainerUtil.java +++ b/platform/util/src/com/intellij/util/containers/ContainerUtil.java @@ -601,15 +601,18 @@ public class ContainerUtil extends ContainerUtilRt { @NotNull @Contract(pure=true) public static Map intersection(@NotNull Map map1, @NotNull Map map2) { - final Set keys = new HashSet(); - keys.addAll(map1.keySet()); - keys.addAll(map2.keySet()); - final Map res = new HashMap(); - for (K k : keys) { - V v1 = map1.get(k); - V v2 = map2.get(k); - if (v1 == v2 || v1 != null && v1.equals(v2)) { - res.put(k, v1); + if (map2.size() < map1.size()) { + Map t = map1; + map1 = map2; + map2 = t; + } + final Map res = new HashMap(map1); + for (Map.Entry entry : map2.entrySet()) { + K key = entry.getKey(); + V v2 = entry.getValue(); + V v1 = map1.get(key); + if (!(v1 == v2 || v1 != null && v1.equals(v2))) { + res.remove(key); } } return res; @@ -618,10 +621,8 @@ public class ContainerUtil extends ContainerUtilRt { @NotNull @Contract(pure=true) public static Map> diff(@NotNull Map map1, @NotNull Map map2) { - final Set keys = new HashSet(); - keys.addAll(map1.keySet()); - keys.addAll(map2.keySet()); - final Map> res = new HashMap>(); + Set keys = union(map1.keySet(), map2.keySet()); + Map> res = new HashMap>(); for (K k : keys) { V v1 = map1.get(k); V v2 = map2.get(k);