TreeSet and TreeMap sorting when comparing elements require element objects to implement comparable interfacesThere are two ways to compare elements in the collections sort method:Element object implementation comparable interfaceEntity class Dog1 Public classDogImplementsCloneable,comparable {2 3 PrivateString name;4 PrivateString age;5 6 PublicString GetName () {7 returnname;8 }9 Public voidsetName (String name) {T
0.0.5 times 2, 1 takes 1.0.125-->0.001(PS. How binary decimals are converted to decimal:The nth number after the decimal point is multiplied by the negative N of 2 to add all:Take 0.001 for example 0 x 0 x + 1 x 1/8 = 0.125)Step TwoThe highest bit of floating-point number is positive or negative, 1 is positive, 0 is negativeThe second high position indicates the positive or negative of the exponent, 1 is positive, and 0 is negative.110.001 = 1.10001 x 2 x 2Index is 4-->0000 100 (seven bit binar
Import Java. util. *; // use comparable to sort class persons of custom types // implements comparable {public string name; Public int age; Public Person (string name, int age) {This. name = Name; this. age = age;} public int compareto (Object OBJ) {
Randomly generating a Chinese character/** * Randomly generating a Chinese character * @return * @throws unsupportedencodingexception/public static String Createstr () throws unsupportedencodingexception {String str = null; int hightpos,
* and then map the operation in map Use TreeSet to get the top three (both size and can be sorted) * * PROBLEM: * The above plan in the production process with caution * because, the execution of Groupbykey, will be the same key data will be pulled into the same partition, and then perform operations, * the process of drawing is Shuffle, is a distributed performance killer! Another, if the key corresponds to too much data, it is likely to cause data
The previous article describes the general knowledge of set sets. The Set collection contains three more important implementation classes: HashSet, TreeSet, and Enumset. This article will focus on these three classes. I. Introduction of HashSet class HashSetHashSet is a typical implementation of the set interface, implements all the methods in the set interface, and does not add additional methods, most of the time using the set set is the implementa
(); // determines whether the elements to be iterated exist in the Temporary container. If (! Temp. contains (obj) {temp. add (obj) ;}} return temp ;}}
Result: abc1, abc2, abc2, abc1, and abc
Abc1, abc2, abc
16-9, LinkedHashSet set
Features: Implements hash tables and link lists with Set interfaces that can be iterated.
To put it bluntly, this set ensures that the object is unique and ordered. HashSet is unordered.
For example:
public class LinkedHashSetDemo {public static void main(String[] a
= line.toCharArray(); //将字符串转换成字符数组HashSet
8. Set FRAME (practice)
removes duplicate elements from the collection
public static void Main (string[] args) {arraylist
9. Set FRAME (treeset store integer type elements and Traverse)
A: Case Demo
TreeSet stores an integer type of element and traverses
10. Collection Framework (
Reprinted please indicate the source: http://blog.csdn.net/droyon/article/details/22781533
The Collection interface represents a group of objects, which are also elements of the Collection. Some Collection interfaces allow repeated elements, while others do not. Some Collection interfaces are ordered, while others are disordered.
Duplicate: two objects are equal by equals. (Equals subclass objects can be rewritten)
Order: The order in which elements are stored is the same as that in which elem
, and inserting duplicate elements is equivalent to inserting only 1 of times. Note: If you do not need to maintain the order in which elements are inserted, you should use HashSet, which is more efficient than linkedhashset.Tree-Shaped collection TreeSetSet expansion order: TreeSet implements the Navigableset interface, the Navigableset interface is the sub-interface of SortedSet, and the SortedSet interface is the sub-interface of the set interface.
stuname) {this.stuname = Stuname;} public int Getscore () {return score;} public void SetScore (int score) {This.score = score;}}2. Then constructs an object of type listpublic static list3. Then the test sorts the listpublic static void Main (string[] args) {list2, TreeSet sortIn a real project, when there is a one-to-many or many-to-many relationship in an entity association, many of the parties are generally represented by a set set (HashSet), so
authoritative guide books.add ("first "); SYSTEM.OUT.PRINTLN (books);//[second, first]}}Output: [Second, first] you can see that the order is in the order of insertion.3.TreeSet classTreeSet is an implementation class for the SortedSet interface, and TreeSet ensures that the collection element is in the sorted state. TreeSet supports two sorting methods, natural
/* Set: Unordered, elements cannot be repeated. |--hashset: The data structure is a hash table.
Threads are unsynchronized.
The principle of ensuring element uniqueness: Determining whether the hashcode value of an element is the same.
If the same, the element's Equals method will continue to be judged to be true. |--treeset: You can sort the elements in the set collection.
The underlying data structure is a two-fork tree.
The bas
method Collection is the parent interface of the Set, List, and Queue interfaces. The sub-classes of Set, List, and Queue can use the following methods.
Boolean add (E) _______________________________ add the specified Element boolean addAll (Collection c) _______________ add all elements in the specified set to this collectionboolean contains (Object o) _____________________ whether the Collection contains the specified Element boolean containsAll (Collection Map Interface Method
Boolean cont
efficient than directly using TreeMap.
After all elements are stored, we sort the elements in the whole Map. This can improve the efficiency of the entire program and shorten the execution time.
Note: In TreeMap, keys are sorted. If we want to use TreeMap for normal sorting, the objects stored in the Key must implement the Comparable interface.
Set:
Extended Collection Interface
Repeated elements are not allowed.
Added restrictions on the add ()
. Its elements and sorting methods are the same as the input ordered set.
(5) Some Methods
Comparator
Comparator (); // returns the comparator for sorting the elements in this set; if this set uses the natural sequence of its elements, it returns nullE first (); // returns the current first (lowest) element E last () in this set; // returns the current last (highest) element SortedSet in this set
SubSet (E from, E to); // return some views of this set, whose elements are from (inclu
Package Com.yang;Import java.util.*;public class Apriori {Private double minsup = 0.2;//Minimum support degreePrivate double minconf = 0.2;//minimum confidence levelNote Use Identityhashmap, otherwise overwrite occurs because the association rule produces the same key valuePrivate Identityhashmap Rulemap = new Identityhashmap ();Private string[] Transset = {"abc", "abc", "Acde", "BCDF", "ABCD", "ABCDF"};//Transaction Collection,You can pass in the constructor as neededPrivate string[] Transset =
Introduction: TreeMap and TreeSet are two important members of the Java Collection Framework, where TreeMap is a common implementation class for the Map interface, and TreeSet is a common implementation class for Set interfaces. Although the interface specification implemented by HASHMAP and HashSet is different, the TreeSet is implemented through TREEMAP, so the
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.