0

我正在尝试编写 3 种方法说
1. getAddedTasks()
2. getRemovedTasks()
3. saveTasks()

完成以下两个哈希图
1. 在屏幕上和 2. 实际
onscreen: {} ,actual: {}

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

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

第 2 步:当点击保存点击保存 -> 这应该发生 getRemovedTasks()="", getAddedTasks()="1,2,3" 然后哈希图状态将是这样的

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


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

onscreen: {1,2,3,4}, actual: {1,2,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();
     }
 }    
4

1 回答 1

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

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

于 2013-06-28T13:00:47.800 回答