a natural sort of treeset:
Steps:
1. Let the elements themselves have a comparative nature,
2. Implement Compareable interface, cover its CompareTo method
Cases:
class Student implementscomparable//first: Implement Compareable interface
{
Private String name;
private int age;
Replication constructor initialization name and age
Student (String name,int age)
{
this. Name = name;
this. Age = age;
}
public int compareTo (Object obj)//Second: Replication
In the Java language, the set interface has 3 common implementations HashSet : TreeSet,LinkedHashSet。 TreeSet is a collection of internal elements that can be sorted, using a red-black tree to store internal elements, sorted based on the values of the elements, which is slower than hashset.TreeSet requires the inner element to implement the comparable interface, because it is sorted according to the Compare
Set: Unordered, can not repeat element|--hashset: The data structure is a hash table, and the thread is non-synchronous.The principle of guaranteeing element uniqueness: Determining whether the hashcode value of an element is the sameIf same, the Equals method of the element will continue to be judged, whether true|--treeset: You can sort the elements in the Set collectionNote: When sorting, when the main conditions are the same, be sure to judge the
TreeSet implements the SortedSet also implements the set, but it is not a random object can be stored:public class Treesettest {public static void main (string[] args) {TreeSet tree = new TreeSet ();Tree.add (New Pepole ("Zhangsan"));Tree.add (New Pepole ("Lisi"));System.out.println (tree);}}class pepole{int age;String name;Public pepole (String name) {this.name
"One Minute Knowledge" HashSet and Treeset,hashmap and Hashtable
HashSet and TreeSet
HashSet
Hash table is implemented, the data in the HashSet is unordered.
There is no guarantee that the order of the elements may change.
It's not synchronized.
The collection element can be null but can only be placed in a null
When an element is deposited into the HashSet collection, HashSet calls the object's Hashco
during compilation, but not duplicate objects, which are added only once for repeating elements. The order in which elements are added to the collection when the 2.Set set class program runs is not the order in which the collection is saved. Elements in the 3.TreeSet are stored in an orderly fashion, treeset each object's class must implement the comparable interface to use*/The
1. TreeSet class overview:• ability to sort elements according to a certain rule.• or sort according to the comparator provided when the set was created• Depending on the construction method used2. code example:1 Packagecn.itcast_05;2 3 ImportJava.util.TreeSet;4 5 /*6 * TreeSet: Ability to sort elements according to a certain rule. 7 * There are two ways of sorting8 * A: Natural sort9 * B: Comparator sortT
calculate the value of the hashcode.TreeSet classTreeSet is the only implementation class for the SortedSet interface, and TreeSet ensures that the collection elements are in the sorted state. TreeSet supports two sorting methods, natural sorting and custom sorting, in which the natural sort is the default sorting method. The object that is added to the treeset
TreeSet is the only implementation class for SortedSet, as the SortedSet name implies,TreeSet allows the collection element to be in the orderly state. Compared with HashSet, TreeSet also provides the following methods (listed, mixed face cooked:)SortedSet subset (Object fromelement,object toelement): Returns the subset of this set, ranging from fromelement (incl
TreeSet: You can sort the elements in the Set collectionSorting is sorted according to ASCII.Import Java.util.iterator;import Java.util.treeset;public class Treesetdemo {public static void main (string[] args) {//T ODO auto-generated Method Stubmethod1 ();} public static void Method1 () {TreeSet ts = new TreeSet () Ts.add ("CBA"), Ts.add ("AAA"), Ts.add ("BCA"),
Some of the major features of TreeSet:1. The type stored in TreeSet must be consistent, cannot save int, and then save string2, TreeSet in the traversal of the collection elements, is the order of "from small to large" (my understanding, if the letters stored, sorted by dictionary order)3, Sort: When adding custom objects to
In a simple example, the internal process of TreeSet collection storage is described, and it is only guaranteed that the custom objects stored here are perfect.Requirement: A:* Natural Sorting, sorted by age from small to largeB* member variable values are the same as the same elementCustom student classes are given out:public class Student implements comparableThe test class was given out:public class TreeSetDemo2 {public static void main (string[]
TreeSet: If you want to sort objects, the object class implements the comparable interface!TREEMAP: If you want to sort objects, the object class implements the comparable interface!The following is a small program that I write myself main transfer objectpublic class News implements comparableString title;String content;Public News (string title, string content) {//constructor with parameter functionSuper ();This.title = title;this.content = content;}
TreeMap implements the SortedMap interface, which means that the key elements in the Map are sorted in the order of size, and the key size can be judged by their natural order (natural Ordering), can also be constructed by passing in the comparator (Comparator). The TreeMap Bottom is realized by the red and black trees (red-black tree), which means,,, containsKey() get() put() remove() All log(n) of the time complexity.The TreeMap is non-synchronous (not synchronized) and can be packaged in sync
Treeset () Class
The treeset () class implements the set interface, which is supported by the treemap instance. This class ensures that sorted sets arrange elements in ascending order, and mayNatural orderSort (seeComparable), Or sort by the comparator provided when the set is created.TreesetThe instance will use itsCompareto(OrCompare) Method to execute all key comparisons. Therefore, from the set perspect
only implementation class for the SortedSet interface, and TreeSet ensures that the collection elements are in the sorted state. TreeSet supports two sorting methods, natural sorting and custom sorting, in which the natural sort is the default sorting method. The object that is added to the treeset should be the same class.
code example:Packagecom.dt.scala.datasetobjectset_map{defmain (args:Array[String]) :unit={//set the collection, the data in the collection is non-repeatable. But the data is unordered. valdataset=set (1,2,5,4,1,3,2) println (dataSet ) valdataset2=dataset.+ (1) //add elements to the collection, Generate another collection valdataset3=dataset.++ (DataSet2) // Two collection merge, return new collection importscala.collection.mutable// Define variable collection valdata=mutable. set.empty[int]data
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.