gpt4 book ai didi

java - 如何在 HashMap 中添加、删除和保存值

转载 作者:行者123 更新时间:2023-12-01 14:23:23 25 4
gpt4 key购买 nike

我正在尝试编写 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 步:我想再次在屏幕上添加第四个值
像这样

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

最佳答案

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

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

关于java - 如何在 HashMap 中添加、删除和保存值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17365177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com