/**
* Set实现类HashSet 和 TreeSet
*/
package core.collection;
import java.util.HashSet;
import java.util.Iterator;
/**
* @author 不负代码不负卿
* @date 2017年8月9日
* @description:实现set接口中的HashSet类用法
*/
public class SetDemo {
public static void main(String[] args) {
HashSet hset = new HashSet<>();
hset.add("aa");
hset.add("bb");
hset.add(true);
hset.add("bb");
// set集合的特点:无序,不重复
System.out.println("HashSet:" + hset);
hset.add(null);
// 用for each遍历,不需要用到index
System.out.print("HashSet:");
for (Object obj : hset) {
System.out.print(obj + " ");
}
System.out.println();
// 无序 没有索引值,所以没有get方法不能通过简单的变量I循环控制遍历集合
// for(int i=0;i<hset.size();i++)
// 使用迭代器罗列集合中的元素
Iterator iterator = hset.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
}
}
* Set实现类HashSet 和 TreeSet
*/
package core.collection;
import java.util.HashSet;
import java.util.Iterator;
/**
* @author 不负代码不负卿
* @date 2017年8月9日
* @description:实现set接口中的HashSet类用法
*/
public class SetDemo {
public static void main(String[] args) {
HashSet hset = new HashSet<>();
hset.add("aa");
hset.add("bb");
hset.add(true);
hset.add("bb");
// set集合的特点:无序,不重复
System.out.println("HashSet:" + hset);
hset.add(null);
// 用for each遍历,不需要用到index
System.out.print("HashSet:");
for (Object obj : hset) {
System.out.print(obj + " ");
}
System.out.println();
// 无序 没有索引值,所以没有get方法不能通过简单的变量I循环控制遍历集合
// for(int i=0;i<hset.size();i++)
// 使用迭代器罗列集合中的元素
Iterator iterator = hset.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
}
}