HashSet

 

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
 booleanadd(E e)
          Adds the specified element to this set if it is not already present.
 voidclear()
          Removes all of the elements from this set.
 Objectclone()
          Returns a shallow copy of this HashSet instance: the elements themselves are not cloned.
 booleancontains(Object o)
          Returns true if this set contains the specified element.
 booleanisEmpty()
          Returns true if this set contains no elements.
 Iterator<E>iterator()
          Returns an iterator over the elements in this set.
 booleanremove(Object o)
          Removes the specified element from this set if it is present.
 intsize()
          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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值