如何在hashmap中添加,删除和保存值

时间:2013-06-28 12:52:33

标签: java object methods hashmap

我试着写3种方法说
1. getAddedTasks()
2. getRemovedTasks()
3. saveTasks()

完成以下操作 有两个hashmap
1.在屏幕上和2.实际
onscreen: {},actual: {}

步骤1:我必须使用添加按钮添加3个任务,1,2,3在屏幕上,实际将为空 像这样

onscreen: {1,2,3}, actual: {}

第2步:点击保存时点击保存 - >这应该发生 getRemovedTasks()=“”,getAddedTasks()=“1,2,3” 然后hashmap状态将是这样的

onscreen: {1,2,3}, actual: {1,2,3}

第3步:我想再次在屏幕上添加第4个值 像这样

onscreen: {1,2,3,4}, actual: {1,2,3}

同时我想删除第3个值 像这样

onscreen: {1,2,4}, actual: {1,2,3}

第4步:点击保存 - > getRemovedTasks()=“3”,getAddedTasks()=“4” 最后输出应该是这样的

onscreen: {1,2,4}, actual: {1,2,4}

我尝试使用以下代码

import java.util.*;

public class  getList
{   

     private  static HashMap<Integer, Object[]> dataz = new HashMap<Integer,Object[]>();
     private static  HashMap<Integer, Object[]> screen_dataz = new HashMap<Integer,Object[]>();
     public final static Object[][] longValues = {{"10", "kstc-proc", "10.10.","5","O"},{"11", "proc-lvk1", "12.1.2.","4","O"},{"13", "trng-lvk1", "4.6.1.","3","O"}};
     public static String sl, pid, tid, mval,status;    

     public  static String findRowsRemoved()
     {
            ArrayList<String> datazList = new ArrayList<String>();
            for(int index: dataz.keySet())
            {
                Object[] data = dataz.get(index);
                datazList.add(data[1]+":"+data[2]);
            }

            for (int index: screen_dataz.keySet())
            {
                Object[] data = screen_dataz.get(index);
                String check = data[1]+":"+data[2];
                if (datazList.contains(check))
                    datazList.remove(check);
            }

            HashMap<String,String> p = new HashMap<String,String>();
            for(String d: datazList)
            {
                String pId = d.split(":")[0];
                String tId = d.split(":")[1];
                if (p.containsKey(pId))
                    p.put(pId, p.get(pId)+","+tId);
                else
                    p.put(pId, tId);
            }

            String fullStr = "";
            for(String pId: p.keySet())
            {
                fullStr += pId + ":" + p.get(pId) + "|";
            }

            fullStr = fullStr.substring(0, fullStr.length()-1);

            return fullStr;
     }




     public static void  addTask(HashMap<Integer,Object[]> d, Object[] data)
     {
        d.put(screen_dataz.size(), data);
     }

     public static void saveTask()
     {   
       System.out.println("Save Task"); 
       System.out.println("-------------");
       dataz.putAll(screen_dataz);

         for (int i=0; i<longValues.length; i++)        
        {
            for (int j=0; j<longValues.length; j++)
            {        
                sl = (String) longValues[i][0];
                pid = (String) longValues[i][1];
                tid = (String) longValues[i][2];
                mval = (String) longValues[i][3];
                status = (String) longValues[i][4];
            }         
        }            

    }  


     public static void main(String args[])
     {
         //addTask();  
         Object[]  obj = new Object[5];
         String[] strArray = new String[]{"1","kstc-proc","1.1","5","O"};
         String[] strArray1 = new String[]{"2","proc-lvk1","1.2.","6","O"};
         String[] strArray2 = new String[]{"3","proc-lvk1","1.3.","7","O"};
         addTask(screen_dataz, strArray);
         addTask(screen_dataz, strArray1);
         addTask(screen_dataz, strArray2);

         Object[]  obj1= new Object[5];
         String[] strArray3 = new String[]{"4","kstc-proc","1.4","8","O"};
         addTask(dataz, strArray2);
         addTask(dataz, strArray3);

         String str = findRowsRemoved();
         System.out.println("RowsRemoved: " + str);
         str = findRowsAdded();
         System.out.println("RowsAdded: " + str);
         //saveTask();
     }
 }    

1 个答案:

答案 0 :(得分:1)

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);
    }
}

通过这种方式,您可以使用这些方法轻松添加和删除项目。