Java Set collection traversal and implementation class comparison

  • 2020-06-15 09:06:10
  • OfStack

Java Set collection traversal and implementation class comparison

The Set set in Java is an Collection without repeating elements, so let's first look at the traversal method


package com.sort; 
 
import java.util.HashSet; 
import java.util.Iterator; 
import java.util.Set; 
 
/** 
 * 1 Three that do not contain repeating elements  collection . To be more precise, set  No satisfaction  e1.equals(e2)  The elements of  e1  and  e2 .  
 * @author Owner 
 * 
 */ 
public class SetTest2 { 
 
  public static void main(String[] args) { 
    Set<String> set = new HashSet<String>(); 
     
    set.add("a"); 
    set.add("b"); 
    set.add("c"); 
    set.add("d"); 
    set.add("e"); 
     
    set.add("e");// Duplicate data cannot be put in  
     
    /** 
     *  Traversal methods 1 , iteration traversal  
     */ 
    for(Iterator<String> iterator = set.iterator();iterator.hasNext();){ 
      System.out.print(iterator.next()+" "); 
    } 
     
    System.out.println(); 
    System.out.println("********************"); 
     
    /** 
     * for Enhance loop traversal  
     */ 
    for(String value : set){ 
      System.out.print(value+" "); 
    } 
  } 
} 

Note: Set collection into String type, if we put in a defined class instances of your own, such as Person class instances, this time we want oneself to hashcode and equal method, with its own key fields to rewrite, because when use HashSet hashCode () method will get called, judgment has been stored in the collection of objects in the hash code whether value with the increase of the object hash code 1 to value; If it doesn't send 1, just add it; If 1 sends, the equals method is compared again. If the equals method returns true, indicating that the object has been added, no new object will be added; otherwise, it will be added.

The following is an analysis of another important implementation class TreeSet of the Set collection under 1,

TreeSet sorts the elements using their natural order or according to the Comparator provided when set was created, depending on the constructor used.

Generally speaking, it can be displayed according to the sorted list, also can be sorted according to the specified rules


Set<String> set = new TreeSet<String>(); 
     
    set.add("f"); 
    set.add("a"); 
    set.add("b"); 
    set.add("c"); 
    set.add("d"); 
    set.add("e"); 
     
    System.out.println(set); 

Output: [a, b, c, d, e, f]

Sorted output

So what if we want it to output in reverse order? Of course there are many ways. Here I'm going to specify 1 rule to have it output in reverse order


package com.sort; 
 
import java.util.Comparator; 
import java.util.Iterator; 
import java.util.Set; 
import java.util.TreeSet; 
 
public class TreeSetTest3 { 
 
  public static void main(String[] args) { 
    Set<String> set = new TreeSet<String>(new MyComparator()); 
     
    set.add("a"); 
    set.add("b"); 
    set.add("c"); 
    set.add("d"); 
    set.add("e"); 
    set.add("A"); 
     
    for(Iterator<String> iterator = set.iterator();iterator.hasNext();){ 
      System.out.print(iterator.next()+" "); 
    } 
  } 
} 
 
class MyComparator implements Comparator<String>{ 
 
  @Override 
  public int compare(String o1, String o2) { 
     
    return o2.compareTo(o1);// Descending order  
  } 
   
} 

e d c b a A

What if the Set collection contains one of our own defined class types?

Note: 1 you must define a collation class that implements the Comparator interface, similar to the method above


package com.sort; 
 
import java.util.Comparator; 
import java.util.Iterator; 
import java.util.Set; 
import java.util.TreeSet; 
 
public class TreeSetTest2 { 
 
  public static void main(String[] args) { 
    Set<Person> set = new TreeSet<Person>(new PersonComparator()); 
     
    Person p1 = new Person(10); 
    Person p2 = new Person(20); 
    Person p3 = new Person(30); 
    Person p4 = new Person(40); 
     
    set.add(p1); 
    set.add(p2); 
    set.add(p3); 
    set.add(p4); 
     
    for(Iterator<Person> iterator = set.iterator();iterator.hasNext();){ 
      System.out.print(iterator.next().score+" "); 
    } 
  } 
} 
 
class Person{ 
  int score; 
   
  public Person(int score){ 
    this.score = score; 
  } 
   
  public String toString(){ 
    return String.valueOf(this.score); 
  } 
} 
 
class PersonComparator implements Comparator<Person>{ 
 
  @Override 
  public int compare(Person o1, Person o2) { 
     
    return o1.score - o2.score; 
  } 
   
} 

Output: 10, 20, 30, 40

If you rank the scores of 1 person in reverse order, you only need to change o2. score-o1.score in the compare method

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: