java HashSet的使用

来源:互联网 发布:翻译软件 win7 编辑:程序博客网 时间:2024/05/29 09:32
日志 > 软件编程
设置置顶 | 编辑 | 删除

java HashSet的使用

发表于:2008年5月3日 16时57分31秒阅读(3)评论(0)本文链接:http://user.qzone.qq.com/169188811/blog/1209805051
java HashSet的使用
import [url=javascript:;]java[/url].util.HashSet;
import java.util.Iterator;
public class WpsklHashSet
{
//java 中Set的使用(不允许有重复的对象):
public static void main(String[] args)
{
  HashSet hashSet=new HashSet();
  String a=new String("A");
  String b=new String("B");
  String c=new String("B");
  hashSet.add(a);
  hashSet.add(b);
  System.out.println(hashSet.size());
  String cz=hashSet.add(c)?"此对象不存在":"已经存在";
  System.out.println("测试是否可以添加对象    "+cz);
  System.out.println(hashSet.isEmpty());
  //测试其中是否已经包含某个对象
  System.out.println(hashSet.contains("A"));
  Iterator ir=hashSet.iterator();
  while(ir.hasNext())
  {
   System.out.println(ir.next());
  }
  //测试某个对象是否可以删除
  System.out.println(hashSet.remove("a"));
  System.out.println(hashSet.remove("A"));
  //经过测试,如果你想再次使用ir变量,必须重新更新以下
  ir=hashSet.iterator();
  while(ir.hasNext())
  {
   System.out.println(ir.next());
  }

}
}
  

 
想第一个看到日志抢沙发?评论列表
请选择道具
<textarea class="content" id="commentEditor" style="BORDER-RIGHT: #ccc 1px solid; BORDER-TOP: #ccc 1px solid; BORDER-LEFT: #ccc 1px solid; COLOR: gray! important; BORDER-BOTTOM: #ccc 1px solid" onfocus="getUBBeditor(this)" rows="13" cols="50" name="content">点击这里发表评论</textarea>
温馨提示:点击验证码输入框,以获取验证码
请输入验证码:
     
上一篇|下一篇|返回日志列表