鱼C论坛

 找回密码
 立即注册
查看: 3889|回复: 8

[已解决]Map集合删除其中的特定的元素

[复制链接]
发表于 2020-2-29 14:42:42 | 显示全部楼层 |阅读模式
5鱼币
题目为:创建Map集合,创建Emp对象,并将Emp对象添加到集合中(Emp对象的id作为Map集合的键),并将id为015的对象从集合中移除。删除语句该怎么写


import java.util.HashMap;
class Emp{
        private String e_id;
        private String e_name;
        public Emp(String name,String id) {//构造方法
                this.e_id=id;
                this.e_name=name;
        }
        public void setId(String id) {
                this.e_id=id;
        }
        public String getId() {
                return e_id;
        }
        public void setName(String name) {
                this.e_name=name;
        }
        public String getName() {
                return e_name;
        }
}

public class Test3 {

        public static void main(String[] args) {
                Map<String, String> map=new HashMap<>(); //由HashMap实现Map对象
               
                Emp emp1=new Emp("011","li");//创建Emp对象
                Emp emp2=new Emp("012","da");
                Emp emp3=new Emp("013","ad");
                Emp emp4=new Emp("014","sw");
                Emp emp5=new Emp("015","ws");
               
                map.put(emp1.getId(), emp1.getName());//将对象添加进集合中
                map.put(emp2.getId(), emp2.getName());
                map.put(emp3.getId(), emp3.getName());
                map.put(emp4.getId(), emp4.getName());
                map.put(emp5.getId(), emp5.getName());
                Set<String> set=map.keySet();//获取map集合中key对象集合
                Iterator <String> it=set.iterator();
                while(it.hasNext()) {
                        String str=(String)it.next();
                        String name=(String)map.get(str);
                        System.out.println(name+""+str);
                }

        }

}
最佳答案
2020-2-29 14:42:43
  1. public static void main(String[] args) {
  2. HashMap<String, String> map = new HashMap<String, String>();
  3. map.put("1", "test1");
  4. map.put("2", "test2");
  5. map.put("3", "test3");
  6. map.put("4", "test4");
  7.   
  8. //完整遍历Map
  9. for (Entry<String, String> entry : map.entrySet()) {
  10.   System.out.printf("key: %s value:%s\r\n", entry.getKey(), entry.getValue());
  11. }
  12.   
  13. //删除元素
  14. Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
  15. while(it.hasNext()){
  16.   Map.Entry<String, String> entry= it.next();
  17.   String key= entry.getKey();
  18.   int k = Integer.parseInt(key);
  19.   if(k%2==1){
  20.    System.out.printf("delete key:%s value:%s\r\n", key, entry.getValue());
  21.    it.remove();
  22.   }
  23. }
  24.   
  25. //完整遍历Map
  26. for (Entry<String, String> entry : map.entrySet()) {
  27.   System.out.printf("key: %s value:%s\r\n", entry.getKey(), entry.getValue());
  28. }
  29. }
复制代码

结果:

  1. key: 1 value:test1
  2. key: 2 value:test2
  3. key: 3 value:test3
  4. key: 4 value:test4
  5. delete key:1 value:test1
  6. delete key:3 value:test3
  7. key: 2 value:test2
  8. key: 4 value:test4
复制代码



注意

但对于iterator的remove()方法,也有需要我们注意的地方:

每调用一次iterator.next()方法,只能调用一次remove()方法。

调用remove()方法前,必须调用过一次next()方法。

JDK-API中对于remove()方法的描述:

void remove()从迭代器指向的集合中移除迭代器返回的最后一个元素(可选操作)。每次调用 next 只能调用一次此方法。如果进行迭代时用调用此方法之外的其他方式修改了该迭代器所指向的集合,则迭代器的行为是不明确的。

抛出:UnsupportedOperationException - 如果迭代器不支持 remove 操作。IllegalStateException - 如果尚未调用 next 方法,或者在上一次调用 next 方法之后已经调用了remove 方法。

最佳答案

查看完整内容

结果: 注意 但对于iterator的remove()方法,也有需要我们注意的地方: 每调用一次iterator.next()方法,只能调用一次remove()方法。 调用remove()方法前,必须调用过一次next()方法。 JDK-API中对于remove()方法的描述: void remove()从迭代器指向的集合中移除迭代器返回的最后一个元素(可选操作)。每次调用 next 只能调用一次此方法。如果进行迭代时用调用此方法之外的其他方式修改了该迭代器所指 ...
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-2-29 14:42:43 | 显示全部楼层    本楼为最佳答案   
  1. public static void main(String[] args) {
  2. HashMap<String, String> map = new HashMap<String, String>();
  3. map.put("1", "test1");
  4. map.put("2", "test2");
  5. map.put("3", "test3");
  6. map.put("4", "test4");
  7.   
  8. //完整遍历Map
  9. for (Entry<String, String> entry : map.entrySet()) {
  10.   System.out.printf("key: %s value:%s\r\n", entry.getKey(), entry.getValue());
  11. }
  12.   
  13. //删除元素
  14. Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
  15. while(it.hasNext()){
  16.   Map.Entry<String, String> entry= it.next();
  17.   String key= entry.getKey();
  18.   int k = Integer.parseInt(key);
  19.   if(k%2==1){
  20.    System.out.printf("delete key:%s value:%s\r\n", key, entry.getValue());
  21.    it.remove();
  22.   }
  23. }
  24.   
  25. //完整遍历Map
  26. for (Entry<String, String> entry : map.entrySet()) {
  27.   System.out.printf("key: %s value:%s\r\n", entry.getKey(), entry.getValue());
  28. }
  29. }
复制代码

结果:

  1. key: 1 value:test1
  2. key: 2 value:test2
  3. key: 3 value:test3
  4. key: 4 value:test4
  5. delete key:1 value:test1
  6. delete key:3 value:test3
  7. key: 2 value:test2
  8. key: 4 value:test4
复制代码



注意

但对于iterator的remove()方法,也有需要我们注意的地方:

每调用一次iterator.next()方法,只能调用一次remove()方法。

调用remove()方法前,必须调用过一次next()方法。

JDK-API中对于remove()方法的描述:

void remove()从迭代器指向的集合中移除迭代器返回的最后一个元素(可选操作)。每次调用 next 只能调用一次此方法。如果进行迭代时用调用此方法之外的其他方式修改了该迭代器所指向的集合,则迭代器的行为是不明确的。

抛出:UnsupportedOperationException - 如果迭代器不支持 remove 操作。IllegalStateException - 如果尚未调用 next 方法,或者在上一次调用 next 方法之后已经调用了remove 方法。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-2-29 17:37:00 | 显示全部楼层
可以直接用remove()
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-2-29 17:38:47 | 显示全部楼层
或者是del 删除
del info[“”]
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-1 12:24:38 | 显示全部楼层
如果对你有帮助。请设置最佳答案。谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-3-1 15:24:06 | 显示全部楼层
蒋博文 发表于 2020-3-1 12:24
如果对你有帮助。请设置最佳答案。谢谢

可以写一下语句嘛,因为是初学,所以一些方法调用,不知道该如何写进去
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-1 17:27:40 | 显示全部楼层
实习生五六七 发表于 2020-3-1 15:24
可以写一下语句嘛,因为是初学,所以一些方法调用,不知道该如何写进去

你是想要JAVA的代码还是python的?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-3-2 11:14:53 | 显示全部楼层
蒋博文 发表于 2020-3-1 17:27
你是想要JAVA的代码还是python的?

java的,麻烦了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-2 17:28:04 | 显示全部楼层
本帖最后由 蒋博文 于 2020-3-2 17:29 编辑

下一个是,
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-18 19:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表