现在的位置: 首页 > 综合 > 正文

HashSet

2014年02月10日 ⁄ 综合 ⁄ 共 3223字 ⁄ 字号 评论关闭

 

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.

 

Method Summary
 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

 

抱歉!评论已关闭.