使用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. sql server auto increment - trace flag 272

    从 sql 2012 开始, 微软为了让 insert 时 auto increment 快一些,做了一个 cache 的机制. 这个机制虽然好,但是也有麻烦的情况,如果你的 sql 突然 resta ...

  2. appium --log-timestamp > appium.log

     appium --log-timestamp > appium.log 

  3. 编写脚本,出现 TypeError: exceptions must be old-style classes or derived from BaseException, not unicode怎样解决?

    小编使用robot framework,在编写安卓自动化脚本时,出现这样的情况: 在网上搜了好久,发现都是python的解决方法,到底怎样解决robot里面的问题呢?最终发现: (1)代码中我是这样写 ...

  4. SublimeText3按ctrl+b执行python无反应

    现象:在Sublime中打开.py文件,按”ctrl+b”执行时无反应.点击工具->编译系统中已经有且识别到Python,但执行”run(ctrl+shift+b)”时无反应,Sublime左下 ...

  5. stm32cube使用

    1.使用stm32cube生成CAN代码注意事项: a.需要手动配置CAN过滤器 { CAN_FilterConfTypeDef sFilterConfig; uint32_t filterID = ...

  6. MySQL Server and Server-Startup Programs

    1. mysqld-The MySQL Server mysqld,also known as mysql server, is the main program that does most of ...

  7. Java Config for WebProject

    1.Java EE vs Java SE They are just official SDK,when using IDE,it's ok to just install jdk/jre. &quo ...

  8. spring cloud: zuul: 微网关-简单使用与路由配置

    spring cloud: zuul: 微网关-简单使用与路由配置 首先引入依赖 <dependency> <groupId>org.springframework.cloud ...

  9. Python实现Plugin(插件化开发)

    https://www.cnblogs.com/terencezhou/p/10276167.html

  10. unitest 测试集 实例

    -->baidy.py #coding=utf-8from selenium import webdriverfrom selenium.webdriver.common.by import B ...