HashSet
来源:互联网 发布:服务器购买平台源码 编辑:程序博客网 时间:2024/05/16 18:11
HashSet
1.it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.
2.is not synchronized
settlement: Set s = Collections.synchronizedSet(new HashSet(...));
3.the fail-fast behavior of iterators should be used only to detect bugs
4.permits the null element
Constructor Summary
HashSet()
Constructs a new, empty set; the backing HashMap instance has default initial capacity (16) and load factor (0.75).
HashSet(Collection<? extends E> c)
Constructs a new set containing the elements in the specified collection.
HashSet(int initialCapacity)
Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor (0.75).
HashSet(int initialCapacity, float loadFactor)
Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and the specified load factor.
boolean
add(E e)
Adds the specified element to this set if it is not already present.
void
clear()
Removes all of the elements from this set.
Object
clone()
Returns a shallow copy of this HashSet instance: the elements themselves are not cloned.
boolean
contains(Object o)
Returns true if this set contains the specified element.
boolean
isEmpty()
Returns true if this set contains no elements.
Iterator<E>
iterator()
Returns an iterator over the elements in this set.
boolean
remove(Object o)
Removes the specified element from this set if it is present.
int
size()
Returns the number of elements in this set (its cardinality).
Sorts the Hashset two different situations:
if sort type is HashSet, it will use default Comparator as nature sort ,sample 1
else sort type is HashSet<T> ,you must implements Comparator as sort way ,sample 2
else you can use Collections.sort(); sample 3
sample 1
HashSet hashSet=new HashSet();
hashSet.add("2");
hashSet.add("a");
hashSet.add("i");
hashSet.add("1");
TreeSet treeSetOne=new TreeSet(hashSet);
Iterator iteratorOne=treeSetOne.iterator();
while(iteratorOne.hasNext()){
System.out.println(iteratorOne.next());
}
sample 2
class Person {
String name;
Integer age;
Person(String name,Integer age){
this.name=name;
this.age=age;
}
}
class MyComparator implements Comparator{
public int compare(Object o1, Object o2) {
// TODO Auto-generated method stub
Person person1=(Person)o1;
Person person2=(Person)o2;
if(person1.age>person2.age){
return 1;
}else if(person1.age<person2.age){
return -1;
}else if(person1.age==person2.age){
if(person1.name.compareTo(person2.name)>0){
return 1;
}
}
return 0;
}
}
public class test {
public static void main(String[] args) {
HashSet<Person> set=new HashSet<Person>();
Person person=new Person("张三",14);
set.add(person);
person=new Person("张三",14);
set.add(person);
person=new Person("李四",43);
set.add(person);
person=new Person("利益",11);
set.add(person);
person=new Person("里尔",24);
set.add(person);
person=new Person("红旗",75);
set.add(person);
person=new Person("试试",24);
set.add(person);
TreeSet<Person> treeSet=new TreeSet<Person>(new MyComparator());
treeSet.addAll(set);
Iterator<Person> i1=treeSet.iterator();
while(i1.hasNext()){
Person t1=i1.next();
System.out.println(t1.name+": "+t1.age);
}
}
}
sample 3
- HashSet
- Hashset
- HashSet
- HashSet
- HashSet
- HashSet
- HashSet
- HashSet
- HashSet
- HashSet
- HashSet
- HashSet
- HashSet
- HashSet
- HashSet
- HashSet
- hashset
- HashSet
- Android的问题与解决
- TreeSet
- C#使用API屏蔽系统热键和任务管理器
- Android安装步骤
- DB2中like使用
- HashSet
- 我十一后做了什么
- ................
- 关于用文件随机读写
- 感冒好了之后
- 深入解析c++静态联编和类的成员函数调用
- dos攻击
- oracle环境变量作用
- 在Dreamweaver(DM)中创建jsp站点+Tomcat服务器