Java中hashSet

package frank;
import java.lang.*;
import java.util.HashSet;
import java.util.Set;
import java.util.Iterator;

/**
 *  HashSet 无序集合,如果有相等的就不会被添加进去,判断标准:equals和hashCode相等就认为相等
 * */
public class App
{
	public static void main(String[] args)throws Exception
	{
		Set books = new HashSet();
		books.add(new String("4"));
		boolean result = books.add(new String("1"));//返回是否添加成功
		System.out.println(result + "---->" + books);
		Iterator i =books.iterator();//获得迭代对象
		String str= (String)i.next();//通过取得第一个对象
		str=new String("2");//在添加对象到hashSet中时一定要小心避免修改,否则会出现无法定位到改对象
		System.out.println(books);
	}
}

  

posted on 2013-10-19 16:19  wp456  阅读(237)  评论(0)    收藏  举报

导航