如何添加和删除多值HashMap中的项目? [英] How do I add and remove items from a multivalue HashMap?

查看:157
本文介绍了如何添加和删除多值HashMap中的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将值添加到具有以下结构的多值HashMap中:

  Map<整数,列表<浮点> > map = new HashMap<整数,列表<浮点> >(); 

我实际上想要保留对某个特定项目的引用(例如Android中的A View的信息),因此 HashMap Integer 值将包含 ID >这是唯一的, Float s的 List 将包含项目的X坐标值。用户可以在屏幕上有很多项目,他也可以有100个具有相同ID的项目,所以相应的列表将包含每个项目X坐标值。



更多清除我的HashMap将包含以下数据:

{1,{200,400.5,500.6 ...}},其中1是密钥,其余是浮点值

现在我添加List值,如下所示...

 列表与LT;浮法> list = new ArrayList< Float>(); 

list.add(x_coord_1);
list.add(x_coord_2);
list.add(x_coord_3)
map.put(1,list);

现在我所面临的问题是弄清楚如何每次新建一个新List ID创建..



我将不得不创建100个列表100个项目,这是不可行的,不知道ID的数量。



有没有更好的方法来解决这个问题...



另外我想找到一种方法来删除特定的特定值 HashMap

解决方案

这个怎么样:

  public class multivalueHashmap {
private Map<整数,列表<浮点> > map = new HashMap<整数,列表<浮点> >();
$ b public void add(Integer id,Float value){
if(!map.containsKey(id)){
map.put(id,new ArrayList< Float>( ));
}
map.get(id).add(value);
}

public void delete(Integer id,Float value){
if(!map.containsKey(id)){
return;
}
map.get(id).remove(value);


code


$ b这样你就可以使用这些方法来轻松地添加和删除项目。

I am trying to add values to a multivalue HashMap which is of the following structure:

Map< Integer, List<Float> > map = new HashMap< Integer, List<Float> >();

I actually wanted to hold a reference to a particular item (A View's info in Android for e.g.), so the Integer value of the HashMap will contain the items ID which is unique, the List of Floats will contain the items X coordinate values. The user can have many items on the screen, he can also have 100 items with same ID, so accordingly the List will contain each Items X coordinate value.

To be more clear my HashMap will contain the following data

{1,{200, 400.5, 500.6 ...}}, where 1 is the key and the rest are Float values for Item with ID 1.

Right now I add the List values as follows...

List<Float> list = new ArrayList<Float>();

list.add(x_coord_1);
list.add(x_coord_2);
list.add(x_coord_3)
map.put(1, list);

The problem I am facing now is figuring out as of how can I instantiate a new List everytime a new ID is created..

I would have to create 100 List's for 100 items which is not feasible, not knowing the number of ID's..

Is there a better approach to solve this issue...

Also I wanted to find a way to delete a specific value of a particular key from the HashMap

解决方案

How about this :

public class multivalueHashmap {
    private Map< Integer, List<Float> > map = new HashMap< Integer, List<Float> >();

    public void add(Integer id, Float value){
        if(!map.containsKey(id)){
            map.put(id, new ArrayList<Float>());
        }
        map.get(id).add(value);
    }

    public void delete(Integer id, Float value){
        if(!map.containsKey(id)){
            return;
        }
        map.get(id).remove(value);
    }
}

This way you can use the methods to easily add and remove items.

这篇关于如何添加和删除多值HashMap中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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