mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
small optimization: more efficient intersection, cleanup
This commit is contained in:
@@ -601,15 +601,18 @@ public class ContainerUtil extends ContainerUtilRt {
|
||||
@NotNull
|
||||
@Contract(pure=true)
|
||||
public static <K, V> Map<K, V> intersection(@NotNull Map<? extends K, ? extends V> map1, @NotNull Map<? extends K, ? extends V> map2) {
|
||||
final Set<K> keys = new HashSet<K>();
|
||||
keys.addAll(map1.keySet());
|
||||
keys.addAll(map2.keySet());
|
||||
final Map<K, V> res = new HashMap<K, V>();
|
||||
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<? extends K, ? extends V> t = map1;
|
||||
map1 = map2;
|
||||
map2 = t;
|
||||
}
|
||||
final Map<K, V> res = new HashMap<K, V>(map1);
|
||||
for (Map.Entry<? extends K, ? extends V> 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 <K, V> Map<K,Couple<V>> diff(@NotNull Map<? extends K, ? extends V> map1, @NotNull Map<? extends K, ? extends V> map2) {
|
||||
final Set<K> keys = new HashSet<K>();
|
||||
keys.addAll(map1.keySet());
|
||||
keys.addAll(map2.keySet());
|
||||
final Map<K, Couple<V>> res = new HashMap<K, Couple<V>>();
|
||||
Set<K> keys = union(map1.keySet(), map2.keySet());
|
||||
Map<K, Couple<V>> res = new HashMap<K, Couple<V>>();
|
||||
for (K k : keys) {
|
||||
V v1 = map1.get(k);
|
||||
V v2 = map2.get(k);
|
||||
|
||||
Reference in New Issue
Block a user