instanceof Double) {System.out.println ("Value:" +obj);}else{System.out.println (obj);}}}}--------------------------------------------------------------Package cn.hncu.col.sort.v1;Import Java.util.HashSet;Import Java.util.Iterator;Import Java.util.Set;Import Java.util.TreeSet;public class SortDemo1 {public static void Main (string[] args) {Set set = new HashSet ();//hashset is not implemented: custom sortSet set = new TreeSet ();//
is an interface, is a highly abstracted collection, it contains the basic operation and properties of the collection . The collection contains the list and set of the two major branches.(1) List is an ordered queue, and each element has its index. The index value of the first element is 0. The list implementation class has LinkedList, ArrayList, Vector, Stack.(2) Set is a set that is not allowed to have duplicate elements. The implementation class for set has Hastset and
collection element cannot be modified using the loop procedure, so the following code throws an exceptionBooks.remove ("Book3");}}SYSTEM.OUT.PRINTLN (books);}}Set interfaceThe set is actually collection, but the set does not allow repeating elements to be includedSet the Equals method to determine whether two objects are the same, not = =, so as long as equals returns True, two objects are the same, regardless of how large the two objects differ, and as long as equals returns false, even if two
element is stored based on the hashcode value of the element, but it maintains the order of the elements using the linked list, which makes the elements appear to be saved in the order in which they were inserted. Linkedhashset Insert performance is slightly lower than hashset, but it performs well when iterating through all the elements of the set. Linkedhashset does not allow collection elements to be duplicated. The third of the Set implementation class:
1. compareto is used for treeset, while treeset personally thinks it is best to read the DB result set. In the future, subset can be obtained based on compareto sorting results. When writing a database, you can use hashset instead of treeset, because the sorting and subset of the structure before writing do not need to be considered.
2. How to Implement compareto
contains (Object O): Returns True if the set contains the specified element;Public Iterator Iterator (): Returns an iterator to the elements in the set, which returns no specific order unless set raises instances of certain classes of the guarantee;Public boolean Add (Object O): Adds to set if the specified element does not exist in the set;public boolean remove (Object o): Removes from set if the specified element exists in the set;public boolean RemoveAll (Collection C): If the set contains t
collection in which objects in the collection are unordered and cannot be duplicated. The main implementation classes include:
1. HashSet: According to the hashing algorithm to access the objects in the collection, access speed is relatively fast;
2. Linkedhashset:hashset subclass, not only realizes the hash algorithm, but also realizes the chain table data structure, the link table data structure can improve inserts and deletes the element the performance;
3.
Student as a Comparator, and you can pass Student as a parameter to the sort method, because Student is a Comparator. But such code is not a good code, because we use the comparator (Comparator), one of the important reasons is that this can be the comparison algorithm and the specific class separation, reduce the coupling between classes.
TreeSet provides support for both methods of comparison, corresponding to the two construction methods of
Dobule.doubletolongbits (f)If the field is an object reference, call Hashcode recursivelyIf the field is an array, each element is treated as a separate field, and a hash code is computed for each important element.Comparison:The order of HashMap is independent of the output order.Linkedhashmap preserves the order in which key-value pairs are stored.TreeMap is the ordering of the elements in the map.Because HashMap and Linkedhashmap store data faster than the direct use of treemap, access effic
.
Object
clone()Returns a shallow copy of thisHashsetInstance: The elements themselves are not cloned.
boolean
contains(Objecto)ReturnsTrueIf this set contains the specified element.
boolean
isEmpty()ReturnsTrueIf this set contains no elements.
Iterator
iterator()Returns an iterator over the elements in this set.
boolean
remove(Objecto)Removes the specified element from this set if it is present.
int
size()Returns the number
maintains the orderly order of elements. The sortedset interface provides access methods for the view (subset) of a set and its two ends (header and tail. When you process the subset of the list, changing the view will be reflected in the source set. In addition, changes to the source set are also reflected in the subset. The reason for this is that the view is specified by the element at both ends rather than the subscript element. Therefore, if you want a special high-end element in the subse
, elements are not repeatable (note: The element is not in order, but the position of the element in set is determined by the hashcode of the element, its position is actually fixed)The list interface has three implementation classes: Linkedlist,arraylist,vectorLinkedList: The underlying is based on a linked list, and the list memory is fragmented, and each element stores its own memory address while storing the address of the next element. Link list and delete fast, find slowThe difference betw
containsall (collection C): returns true if set contains all elements of the specified set. If the specified set is also a set, the method returns true only when it is a subset of the current set.
U public Boolean addall (collection C): If there is no element in the specified set, add all elements to the set.
U public Boolean retainall (collection C): retains only the elements of the specified set contained in the Set (optional ). In other words, all elements not included in the specified set a
of the features of a hash table is the bucket structure.
Hashset: The underlying layer is supported by hash tables. Features: Fast Access
By default, Hashcode indicates the memory address. The String class has overwritten the hashcode method of the object.
Treeset
Treeset features: If elements are naturally ordered, they are sorted and stored in order.
Notes for Treese
, equals is compared with "= =", so the hashcode of different objects must be different, Equals is not true either. But there are conventions in Java: If two objects are equal (equal), then you must have the same hash code (hash code), so it is common to rewrite equals and hashcode at the same time. HashSet Another feature is the disorder, hashset inside is a hash table (also known as a hash table), each input will go through the hash function to get a fixed-length hash (Key) =hashcode, the s
The previous blog spoke about the basic operation of the collection interface, this blog mainly introduces the sub-interface set of the collection interface.Set is an unordered set, its basic operation and collection interface is similar, the main difference is that the set cannot repeat the elements and the collection collection is possible. For set sets We are primarily concerned about its Hashset,treeset two implementation classes.I. HashSetHashSet
elements in the set.
Toarray () returns an array containing all elements in the set.
Both the list and set interfaces inherit from the collection interface. These types of objects can use the above methods, but they cannot call the above methods for map objects.
(5) iterator Interface
A. The iterator interface hides the data structure of the underlying set and provides a unified interface for Traversing various types of Sets
B. The iterator interface declares the following methods:
Hasnext ()
=23]person [name= John Doe, Age=24]person [name= John Doe, Age=23]person [name= Harry, Age=25]person [Name= 赵六, age=25]Compared with the results of the above hashset, the order of deposit is the same as the order taken out.TreeSetTreeSet is the only implementation of the SortedSet interface, and TreeSet ensures that the collection elements are in a sorted state (elements are ordered).The TreeSet provides se
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.