I have previously written a blog post about hash table, which can be used to implement hashset and hashmap ).
Hashset
The hashset <t> class has four constructor types:
Constructor and description |
HashSet() Constructs a new, empty set; the backingHashmapInstance 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 backingHashmapInstance has the specified initial capacity and default load factor (0.75 ). |
HashSet(int initialCapacity, float loadFactor) Constructs a new, empty set; the backingHashmapInstance has the specified initial capacity and the specified load factor. |
All of its methods:
Modifier and type |
Method and description |
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 thisHashsetInstance: The elements themselves are not cloned. |
boolean |
contains(Object o) ReturnsTrueIf this set contains the specified element. |
boolean |
isEmpty() ReturnsTrueIf 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 ). |
Treeset
A treeset is an ordered collection ). This class is sorted in a certain order, and the red and black trees are used.
Constructor:
TreeSet() Constructs a new, empty tree set, sorted according to the natural ordering of its elements. |
TreeSet(Collection<? extends E> c) Constructs a new tree set containing the elements in the specified collection, sorted according toNatural OrderingOf its elements. |
TreeSet(Comparator<? super E> comparator) Constructs a new, empty tree set, sorted according to the specified comparator. |
TreeSet(SortedSet<E> s) Constructs a new tree set containing the same elements and using the same ordering as the specified sorted set. |
There are two sorting methods:
1. Implement the comparable Interface
By default, treeset assumes that the inserted elements implement the comparable interface. This interface defines a method:
Public interface comparable <t> {
Int comparto (t other );
}
For example, for the string class, if A is equal to B, A. comparto (B) returns 0. If A> B, a positive value is returned.
However, this method has the disadvantage that only one sorting method can be used for a class. For example, if the employee class wants to sort by ID or salary, is it swollen? This requires the second method.
2. Implement the comparator Interface
Notice the constructorTreeset(Comparator <?
Super E> comparator), you only need to pass a comparator object to the treeset.
For example:
Set <employee> otherstaff = new treeset <employee> (new comparator <employee> () {public int compare (Employee A, employee B) {return (INT) (. getsalary ()-B. getsalary (); // sort by salsay size }});
Test procedure
Test Programs for hashset and treeset:
Hashcode and equals methods are rewritten here.
Package COM. xujin; import Java. util. comparator; import Java. util. hashset; import Java. util. iterator; import Java. util. set; import Java. util. treeset; public class implements listtest {public static void main (string... arg) {employee Jim = new employee ("Jim", 8000); employee Bob = new employee ("Bob", 9000); Manager Jin = new manager ("gin ", 10000,400 0 ); *************************** * *******/set <employee> Staff = new hashset <employee> (); staff. add (Jim); staff. add (Bob); staff. add (Jin); staff. add (Bob); // The hashcode is the same and the system is not allowed to be added. out. println (staff. size (); // 3 iterator <employee> iter = staff. iterator (); While (ITER. hasnext () {employee e = ITER. next (); system. out. println (E );} *************************** * *******/set <employee> otherstaff = new treeset <employee> (new comparator <employee> () {public int compare (Employee A, employee B) {return (INT) (. getsalary ()-B. getsalary (); // sort by salsay size}); otherstaff. add (Bob); otherstaff. add (Jim); system. out. println (otherstaff); // [ID: 1 Name: Jim salary: 8000.0, ID: 2 Name: Bob salary: 9000.0]} class employee {public employee (string name) {This. name = Name; id = nextid; nextid ++;} Public String tostring () {return "ID:" + ID + "name:" + name + "salary: "+ salary;} public int hashcode () {return ID;} public Boolean equals (employee e) {If (ID-e. GETID () = 0) return true; return false;} public employee (string name, double salary) {This (name); // call another constructor this. salary = salary;} // defines the accessors method public final string getname () {return name;} public double getsalary () {return salary;} public final int GETID () {return ID;} // defines the modifier method public final void setname (string name) {This. name = Name;} public final void setsalary (double salary) {This. salary = salary;} public final void raisesalary (double percent) {This. salary * = (1 + percent);} // defines the variable private string name = ""; // instance domain initialization private double salary; private int ID; private Static int nextid = 1;} class manager extends employee {Public Manager (string name, double salary, double bonus) {super (name, salary ); // super is used in the constructor. You can call the constructor setbonus (bonus);} Public String tostring () {return Super. tostring () + ", bonus:" + bonus;} public double getbonus () {return bonus;} // rewrite the getsalary method public double getsalary () {double basesalary = super. getsalary (); // call the getsalary method of the superclass return basesalary + bonus;} public void setbonus (double bonus) {This. bonus = bonus;} private double bonus ;}
Usage Policy
Hashset and treeset
The hashset has a higher speed and does not need to be sorted.
If you do not need to sort data, you do not need to use treeset.