1、HashSet
public static void main(String[] args) {
// TODO Auto-generated method stub
HashSet<String> hs = new HashSet<String>();
hs.add("12");
hs.add("13");
hs.add("14");
hs.add("15");
hs.add("16");
hs.add("14");
Iterator<String> it =hs.iterator();
while(it.hasNext()){
String s =it.next();
System.out.println(s);
}
2、LinkedHashSet是Set集合的一个实现,具有set集合不重复的特点,同时具有可预测的迭代顺序,也就是我们插入的顺序。并且linkedHashSet是一个非线程安全的集合。如果有多个线程同时访问当前linkedhashset集合容器,并且有一个线程对当前容器中的元素做了修改,那么必须要在外部实现同步保证数据的冥等性.
3、TreeSet
1) TreeSet是SortedSet接口的唯一实现, TreeSet可以确保集合元素处于排序状态. TreeSet并不是根据元素的插入顺序进行排序,
而是根据元素实际值来进行排序.(可以确保元素唯一并且元素排序) TreeSet采用红黑树的数据结构对元素进行排序.
2)TreeSet支持两种排序方法: 自然排序和比较器排序. 默认情况下,TreeSet 采用自然排序.(Integer类型元素自然升序)
4、HashMap
HashMap 是基于哈希表的 Map 接口的非同步实现。此实现提供所有可选的映射操作,并允许使用 null 值和 null 键。
此类不保证映射的顺序,特别是它不保证该顺序恒久不变。
HashMap():构建一个初始容量为 16,负载因子为 0.75 的 HashMap。
ashMap(int initialCapacity):构建一个初始容量为 initialCapacity,负载因子为 0.75 的 HashMap。
HashMap(int initialCapacity, float loadFactor):以指定初始容量、指定的负载因子创建一个 HashMap。
HashMap<String,Student> hm = new HashMap<String,Student>();
hm.put("1hao", new Student("zhangsan",19));
hm.put("2hao", new Student("zhangsi",20));
hm.put("3hao", new Student("zhangwu",21));
hm.put("4hao", new Student("zhangliu",22));
System.out.print(hm.get("1hao").getName());
1)keySet()获取key集合,再获取迭代器
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<String,Student> hm = new HashMap<String,Student>();
hm.put("1hao", new Student("zhangsan",19));
hm.put("2hao", new Student("zhangsi",20));
hm.put("3hao", new Student("zhangwu",21));
hm.put("4hao", new Student("zhangliu",22));
System.out.print(hm.get("1hao").getName());
Iterator<String> it = hm.keySet().iterator();//获取key集合
while(it.hasNext()){
String key = it.next();//获取key
Student st = hm.get(key);//获取key对应的value
System.out.println(st.getName());
}
}
5、Iterator和foreach遍历集合
LinkedList<Student> ll = new LinkedList<Student>();
ll.add(new Student("zhangsan",13));
ll.add(new Student("zhangsi",14));
ll.add(new Student("zhangwu",15));
ll.add(new Student("zhangliu",16));
ll.add(new Student("zhangqi",17));
Iterator<Student> it = ll.iterator();
while(it.hasNext()){
Student s = it.next();
System.out.println(" xingm "+s.getName()+" nianl "+s.getAge());
}
System.out.println(" 545555555555555555555555555 ");
for(Student s:ll){
System.out.println(" xingm "+s.getName()+" nianl "+s.getAge());
}
扫码关注一起随时随地学习!!!就在洋葱攻城狮,更多精彩,等你来!!