Comparison and analysis of traversal and implementation classes of set sets in Java

Source: Internet
Author: User
Tags set set

The set set in Java is a collection that does not contain duplicate elements, first we look at the traversal method

Package com.sort;

Import Java.util.HashSet;
Import Java.util.Iterator;
Import Java.util.Set;

/**
 * A collection that does not contain duplicate elements. More specifically, set does not contain elements that satisfy e1.equals (E2) for E1 and E2,
 * @author Owner */Public
class SetTest2 {public

	Stati c 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");//cannot be placed
		
		in duplicate data/**
		 * Traversal method One, iterative traversal
		 /for
		(iterator<string> iterator = Set.iterator (); Iterator.hasnext ();) {
			System.out.print (Iterator.next () + "");
		}
		
		System.out.println ();
		System.out.println ("********************");
		
		/**
		 * For Enhanced loop traverse * * for
		(String value:set) {
			System.out.print (value+ "")
		;
}}

Note: There is a string type in the set set, and if we put a class instance of our own definition, such as the person class instance, then we have to hashcode and equal the method ourselves and rewrite it with our own key field. Because when using HashSet, the Hashcode () method is invoked to determine whether the hash code value of the object already stored in the collection is consistent with the hash code value of the object being added; if it is inconsistent, add it directly; if consistent, compare the Equals method, If the Equals method returns True, the object is added, no new objects are added, or added.



The following is an analysis of another important implementation class TreeSet of set sets,

TreeSet The elements by using the natural order of the elements, or by ordering the Comparator provided when the set was created, depending on the construction method used.

In layman's terms, you can follow the sorted list, or you can sort by 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]

output by Sort


so if we want him to output it backwards. Of course there are many ways. Here I'm using a rule to get him to 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 () + " ");
		}
	}
}

Class Mycomparator implements comparator<string>{

	@Override public
	int Compare (string O1, string O2) { C23/>return O2.compareto (O1);//descending order
	}
	
}

output: E D C b a a


What if you put a class type that we define ourselves in the set set?


Note: Be sure to define a collation class to implement the comparator interface, similar to the above method


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 (a);
		person P2 =  New Person (a);
		Person P3 =  new Person (a);
		Person P4 =  new person (n);
		
		Set.add (p1);
		Set.add (p2);
		Set.add (p3);
		Set.add (p4);
		
		for (iterator<person> iterator = Set.iterator (); Iterator.hasnext ();) {
			System.out.print (). 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:
A


If you arrange in reverse order of a person's score, you only need to change the O2.score-o1.score in the Compare method




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.