使用Iterator迭代器

  1. public class Demo {
  2. public static void main(String[] args) {
  3. Set<Object> obj = new HashSet<Object>();
  4. obj.add("a");
  5. obj.add("b");
  6. obj.add("c");
  7. System.out.println("移除前:" + obj.toString());
  8. Iterator<Object> it = obj.iterator();  
  9.         for(int i=0; i<obj.size(); i++){  
  10. System.out.println(i);
  11. Object name = it.next();
  12. if("a".equals(name) || "b".equals(name)){
  13.      it.remove();  
  14. i--;
  15. }
  16. }
  17. System.out.println("移除后: " + obj.toString());
  18. }
  19. }

set循环遍历删除特定元素的更多相关文章

  1. Java集合类ArrayList循环中删除特定元素

    在项目开发中,我们可能往往需要动态的删除ArrayList中的一些元素. 一种错误的方式: <pre name="code" class="java"&g ...

  2. 如何python循环中删除字典元素

    //下面这行就是在循环中遍历删除字典元素的方法! for i in list(dictheme2.keys()): if dictheme2[i]<self.countFortheme: dic ...

  3. java 在循环中删除数组元素

    在写代码中经常会遇到需要在数组循环中删除数组元素的情况,但删除会导致数组长度变化. package com.fortunedr.thirdReport; import java.util.ArrayL ...

  4. java集合遍历删除指定元素异常分析总结

    在使用集合的过程中,我们经常会有遍历集合元素,删除指定的元素的需求,而对于这种需求我们往往使用会犯些小错误,导致程序抛异常或者与预期结果不对,本人很早之前就遇到过这个坑,当时没注意总结,结果前段时间又 ...

  5. STL——遍历 删除 set 元素

    ==================================声明================================== 本文版权归作者所有. 本文原创,转载必须在正文中显要地注明 ...

  6. map循环遍历删除

    typedef map<string,int> MapFileList; int main() { MapFileList m_SingleList; m_SingleList.inser ...

  7. Java 循环遍历删除set list中的元素

    删除List和Set中的某些元素 错误代码的写法: Set<String> set = new HashSet<String>(); set.add("aaaaaa& ...

  8. ArrayList删除特定元素的方法

    最朴实的方法,使用下标的方式: ArrayList<String> al = new ArrayList<String>(); al.add("a"); a ...

  9. vector删除特定元素

    删除vector中小于20的元素,注意要使迭代器失效,不能简单的删除. #include <iostream>#include <vector>using namespace ...

随机推荐

  1. Hive json字符串解析

    在做数据测试时,会遇到数据库表里字段类型为json 的情况,而我们可能只关注该字段中的各别内容的信息,如下 content {"测试内容1":,"测试内容2": ...

  2. Selenium WebDriver Api 知识梳理

    之前一直没有系统的梳理WebDriver Api的相关知识,今天借此机会整理一下. 1.页面元素定位 1.1.8种常用定位方法 # id定位 driver.find_element_by_id() # ...

  3. (9)进程---JoinableQueue队列

    消费者模型-->存和取得过程 和Queue队列区别:解决了Queue队列拿取完,程序阻塞不能自动关闭(依靠放入None来解决)的问题--->参见上个例子 put 存入, get 获取 q. ...

  4. English Voice of <<Beautiful now>>

    Beautiful Now  -Zedd & Jon Bellion I see what you're wearing, there's nothing beneath it 我看见了你身着 ...

  5. English trip M1 - PC7 Can I Borrow Your Ping-Pong? Teacher:Patrick

    In this lesson you will learn to desribe abilities.  这节课你将学习到描述你的能力 课上内容(Lesson) 三种常见情态动词 can aux. 能 ...

  6. Python自学:第二章 使用制表符或换行符来添加空白

    print("Languages:\n\tPython\n\tC\n\tJava") 输出为: Languages: Python C Java

  7. fedora21 中lamp的搭建(测试没有问题)

    LAMP Stands for Linux,Apache,MySQL and PHP. Most of the websites works with the above combination. T ...

  8. react中创建组件

    第1种 - 创建组件的方式 > 使用构造函数来创建组件,如果要接收外界传递的数据,需要在 构造函数的参数列表中使用`props`来接收:> 必须要向外return一个合法的JSX创建的虚拟 ...

  9. 04 爬虫数据存储之Mongodb

    MongoDB 认识MongoDB MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案.MongoDB是一个介于关系数据库和非关系数据 ...

  10. https请求排错过程

    1. 看请求有没有到nginx 此时需要查看nginx的日志.一般每一个项目都会配置一个nginx站点,而一个站点都会又一个nginx配置文件,这个文件位于哪里呢?不出意外应该在:下面,如果找不到的话 ...