@liruiyi962464
2017-03-21T04:00:33.000000Z
字数 2199
阅读 469
java
- ArrayList LinkedList三种方式都能使用
- HashSet TreeSet没有办法使用for,foreach循环的底层是有迭代器实现的,能用迭代器遍历就能用foreach
- 凡是能用foreac遍历的都能用迭代器遍历
HashSet<String> hashSte = new HashSet<String>();
hashSte.add("张三");
hashSte.add("李四");
hashSte.add("王五");
//直接输出
System.out.println(hashSte.hashSte(i));
// foreach
for (String string : hashSte) {
System.out.println(string);
}
// 迭代器 (Iterator)
//(hasNext:是否有数据存在)(next:迭代输出)
System.out.println("迭代器遍历");
Iterator<String> iterator = hashSte.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
//迭代器(for)
for (Iterator iterator = arratList.iterator(); iterator.hasNext();) {
System.out.println(iterator.next());
}
- 创建一个类集,使用散射表(又称哈希表)进行存储
- 没有确保其元素的顺序,因为散列处理通常不参与排序
HashSet<String> hashSte = new HashSet<String>();
//添加
System.out.println(hashSte.add("a"));
System.out.println(hashSte.add("d"));
System.out.println(hashSte.add("a"));
System.out.println(hashSte.add("b"));
System.out.println(hashSte.add("b"));
System.out.println(hashSte);
//查询
System.out.println(hashSte.contains("c"));
//查看是否包含指定元素
System.out.println(hashSte.isEmpty()+""+hashSte);
//返回对此 set 中元素进行迭代的迭代器。
Iterator<String> it = hashSte.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
//删除
System.out.println(hashSte.remove("a")+""+hashSte);
//返回此 set 中的元素的数量(set 的容量)。
System.out.println(hashSte.size());
//清除
hashSte.clear();
System.out.println(hashSte);
- 对象按照升序存储,访问和检索很快
TreeSet<String> treeSet=new TreeSet<String>();
treeSet.add("qwe");
treeSet.add("asd");
treeSet.add("zxc");
treeSet.add("rty");
treeSet.add("fgh");
System.out.println(treeSet);
//如果此 set 包含指定的元素,则返回 true。
System.out.println(treeSet.contains("qwe"));
//迭代器输出
Iterator<String> it = treeSet.iterator();
while(it.hasNext()){
System.out.print(it.next()+"\t");
}
System.out.println();
//foreach 没有办法使用for,foreach循环的底层是有迭代器实现的,能用迭代器遍历就能用foreach
for (String string : treeSet) {
System.out.print(string+"\t");
}
System.out.println();
//返回此 set 中当前第一个(最低)元素。
System.out.println(treeSet.first());
//返回此 set 中小于等于给定元素的最大元素;如果不存在这样的元素,则返回 null。
System.out.println(treeSet.floor("asd"));
//如果此 set 不包含任何元素,则返回 true。
System.out.println(treeSet.isEmpty());
// 返回此 set 的部分视图,其元素范围从 fromElement 到 toElement。
System.out.println(treeSet.subSet("a", "z"));
//返回此 set 的部分视图,其元素大于等于 fromElement。
System.err.println(treeSet.tailSet("b"));