两个或更多(散列)地图的联合 [英] Union of two or more (hash)maps

查看:113
本文介绍了两个或更多(散列)地图的联合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个包含相同类型对象的地图:

  Map< String,TaskJSO> a = new HashMap< String,TaskJSO>(); 
Map< String,TaskJSO> b = new HashMap< String,TaskJSO>();

public class TaskJSO {String id; }

地图键是id属性。

  a.put(taskJSO.getId(),taskJSO); 

我想获得一个列表:Map b中的所有值+Map a不在Map b中。



执行此操作的最快速度是什么?



谢谢

编辑:比较由id完成。因此,如果两个TaskJSO具有相同的id,则认为它们是相等的(equals方法被忽略)。

我的意图是知道哪个是执行此操作的最快方式从性能角度来看。例如,如果我在地图中进行比较(如Peter所建议的),是否有区别:

  Map<字符串,TaskJSO> ab = new HashMap< String,TaskJSO>(a); 
ab.putAll(b);
ab.values()

或者如果我使用一个集合(如Nishant ):

  Set s = new Hashset(); 
s.addAll(a.values());
s.addAll(b.values());


解决方案

方法1:

  Set s = new HashSet(); 
s.addAll(a.values());
s.addAll(b.values());

Set是唯一对象的集合。请参阅: http://download.oracle。 com / javase / 1.4.2 / docs / api / java / util / HashSet.html




方法2:



会比较键,如果找到相同的键 - 值将被后面的Map值覆盖。

 映射< String,TaskJSO> ab = new HashMap< String,TaskJSO>(a); 
ab.putAll(b);
ab.values()

现在,无论如何......比较将使用等于进行。因此,Method-1将在所有值上调用 equals ,Method2将在所有键上调用它。取决于比较的复杂程度,性能会有所不同。

在方法1中,您需要创建一个新的Set,但它可以确保使用相同键的不同值不会被过度控制。但方法2很聪明,如果您有唯一的ID。



编辑#1 随着问题的更新而更新


I have two Maps that contain the same type of Objects:

Map<String, TaskJSO> a = new HashMap<String, TaskJSO>();
Map<String, TaskJSO> b = new HashMap<String, TaskJSO>();

public class TaskJSO { String id; }

The map keys are the "id" properties.

a.put(taskJSO.getId(), taskJSO);

I want to obtain a list with: all values in "Map b" + all values in "Map a" that are not in "Map b".

What is the fastest way of doing this operation?

Thanks

EDIT: The comparaison is done by id. So, two TaskJSOs are considered as equal if they have the same id (equals method is overrided).

My intention is to know which is the fastest way of doing this operation from a performance point of view. For instance, is there any difference if I do the "comparaison" in a map (as suggested by Peter):

Map<String, TaskJSO> ab = new HashMap<String, TaskJSO>(a);
ab.putAll(b);
ab.values()

or if instead I use a set (as suggested by Nishant):

Set s = new Hashset();
s.addAll(a.values());
s.addAll(b.values());

解决方案

Method 1:

 Set s = new HashSet();
 s.addAll(a.values());
 s.addAll(b.values());

Set is collection of unique objects. Refer: http://download.oracle.com/javase/1.4.2/docs/api/java/util/HashSet.html


Method 2:

This will compare keys, and if same keys are found -- the value will be overwritten by the later Map's value.

Map<String, TaskJSO> ab = new HashMap<String, TaskJSO>(a);
ab.putAll(b);
ab.values()

Now, no matter what's the case... the comparison will take place using equals. So, Method-1 will call equals on all the values and Method2 will call it on all the keys. Depending on how complex the comparison is the performance will vary.

In method 1, you need to create a new Set but it ensures that different values with same keys are not overwitten. But Method-2 is smart, if you have unique IDs.

Edit#1 updates as the question was updated

这篇关于两个或更多(散列)地图的联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆