ArrayList<HashMap<String, Object>>如何更新键值对和删除键值对?
示例:String[]name=newString[]{"张三","李四","王五","赵六","霍七"};ArrayList<HashMap<String,Object...
示例:
String[ ] name= new String[ ]{"张三","李四","王五","赵六","霍七"};
ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>();
for( int i=0 ; i <name.length ; i++){
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("Name", name[ i ]);
arrayList.add(map);
}
请教大神们:
(问题1) 假如我要把“王五”改成“小明”该怎么更新这个键值???
(问题2) 假如我要删除“李四”该怎么写呢? 展开
String[ ] name= new String[ ]{"张三","李四","王五","赵六","霍七"};
ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>();
for( int i=0 ; i <name.length ; i++){
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("Name", name[ i ]);
arrayList.add(map);
}
请教大神们:
(问题1) 假如我要把“王五”改成“小明”该怎么更新这个键值???
(问题2) 假如我要删除“李四”该怎么写呢? 展开
2个回答
展开全部
String[ ] name= new String[ ]{"张三","李四","王五","赵六","霍七"};
ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>();
for( int i=0 ; i <name.length ; i++){
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("Name", name[ i ]);
arrayList.add(map);
}
// 修改制定key
for (HashMap<String, Object> hashMap : arrayList)
{
if (hashMap.get("Name").equals("王五"))
{
hashMap.put("Name", "小明");
}
}
// 官方推荐的删除方法
Iterator<HashMap<String, Object>> iterator = arrayList.iterator();
while(iterator.hasNext())
{
if(iterator.next().get("Name").equals("李四"))
{
iterator.remove();
}
}
System.out.println(arrayList);
运行结果:
[{Name=张三}, {Name=小明}, {Name=赵六}, {Name=霍七}]
展开全部
public static void main(String[] args) {
String[ ] name= new String[ ]{"张三","李四","王五","赵六","霍七"};
ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>();
for( int i=0 ; i <name.length ; i++){
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("Name", name[ i ]);
arrayList.add(map);
}
HashMap<String, Object> remove = null ;
for (HashMap<String, Object> hashMap : arrayList) {
if(hashMap.get("Name").equals("王五"))
{
hashMap.put("Name", "小明");
}
if(hashMap.get("Name").equals("李四"))
{
remove = hashMap;
}
}
arrayList.remove(remove);
System.out.println(arrayList);
}
String[ ] name= new String[ ]{"张三","李四","王五","赵六","霍七"};
ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>();
for( int i=0 ; i <name.length ; i++){
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("Name", name[ i ]);
arrayList.add(map);
}
HashMap<String, Object> remove = null ;
for (HashMap<String, Object> hashMap : arrayList) {
if(hashMap.get("Name").equals("王五"))
{
hashMap.put("Name", "小明");
}
if(hashMap.get("Name").equals("李四"))
{
remove = hashMap;
}
}
arrayList.remove(remove);
System.out.println(arrayList);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询