代碼如下:
package cn.com.cnstrong;
import java.util.*;
public class TestSet {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HashSet set = new HashSet();
//Set set = new HashSet();
String ss1=new String("s1");
String ss2=new String("s1");//判斷條件,採用equal的判斷條件
set.add("s1");
set.add("s4");
set.add("s6");
set.add("s3");
set.add("s9");
set.add(ss1);
//1.採用for-each的遍歷方式
for(Object obj:set)
{
System.out.println(obj);
}
//2.用使用迭代器的遍歷方式
Iterator it = set.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
package cn.com.cnstrong;
public class Student {
String s1;
public Student (String s1)
{
this.s1=s1;
}
public String getName()
{
return s1;
}
}
package cn.com.cnstrong;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class TestMap {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Map map=new HashMap();
Student s1=new Student("zs");
Student s2=new Student("ls");
Student s3=new Student("ww");
Student s4=new Student("zl");
map.put("1", s1);
map.put("2", s2);
map.put("3", s3);
map.put("4", s4);
Set key=map.keySet();
for(Object k:key)
{
Student stu=(Student)map.get(k);
System.out.println(stu.getName());
}
}
}
package cn.com.cnstrong;
import java.util.*;
public class TestSet {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HashSet set = new HashSet();
//Set set = new HashSet();
String ss1=new String("s1");
String ss2=new String("s1");//判斷條件,採用equal的判斷條件
set.add("s1");
set.add("s4");
set.add("s6");
set.add("s3");
set.add("s9");
set.add(ss1);
//1.採用for-each的遍歷方式
for(Object obj:set)
{
System.out.println(obj);
}
//2.用使用迭代器的遍歷方式
Iterator it = set.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
1.同步性:Vector是同步的,並且是線程安全的;2.ArrayList是不同步的,並且線程是不安全的。
構造對象的方法:
1.new
2.clone
3.bean
加載因子:與擴容有關。
利用ArrayList可以構造HashSet
Set接口可以保證不重複,但不保證順序
List接口可以保證順序,但不保證重複
HashMap
是兩列存儲
HashSet是用HashMap做的,利用HashMap中的鍵做的Value