used in set is test attribution, and you can easily ask if an object is in a set. The set has exactly the same interface as the collection, so there is no additional functionality. In fact set is collection, but behaves differently.The main thing that implements the set interface is HashSet, TreeSet, and Linkedhashset, which is that each of the same items is saved only one copy. They also have different points, the difference is as follows:1.HashSet:
method is called to compare the elements one by one. If the new element and all existing elements are not repeated, true is returned if the add operation is successful. Otherwise, false is returned.
Map is different from List and Set. It is stored in double rows (keys and values correspond to each other). It calls the put Method in the storage element, A duplicate key and value cannot be stored.
Duplicate rules are also compared using equals. You can obtain the value based on the key when r
Collection list: Click on the Open link
As with the list collection, the Set collection inherits the collection interface.
HashSet and TreeSet are two classes that we often use.
HashSet and TreeSet both inherited the Abstractset abstract class: But TreeSet implemented the SortedSet interface:
So TreeSet is ordered (b
() method of the Arrays tool class (array to collection)16.25 case plots for nested storage and traversal elements (slightly)16.26 Case code implementation for nested storage and traversal elements16.27 generate a random number between 10 and 1-20 requires random number cannot repeat case16.28 Keyboard input multiple data in console output max caseDark Horse Programmer _javase Learning Summary 17th Day _ Collection Framework 317.01 ArrayList Collection of ToString () method source code parsingO
traversing will need to get map map to put into the collection and then use the iterator to traverse, the following keyset view as an example to paste the small block code (EntrySet view is similar):setIteratorwhile (It2.hasnext ()) {EntrySystem.out.println ("key=" +entry.getkey () + ", value=" +entry.getvalue ());}The value view is different, it uses the collection interface, and the following is also a small block code:CollectionIteratorwhile (It3.hasnext ()) {Object Obj=it3.next ();String st
efficiency when performing different operations on data. Similarly, this is also true for ArrayList and rule list. In actual use, we need to select appropriate classes based on specific requirements. If you cannot insert or delete elements in other locations except the end, ArrayList is more efficient, if you need to insert or delete elements frequently, select the sequence list.2. Set
The Set interface extends the self-Collection. The difference between the Set interface and the List is that t
there is no type security risk System. out. println (al1.size ());}}
For more information, see the API documentation. addAll method: addAll (? Extends E), which can store all subclasses of E and E.
Iv. lower limit:The lower limit can be used to retrieve elements from a set.That is, no matter what type is stored (as long as it is a subclass of the current parent class), you can use the parent type to receive
Import java. util. Comparator; import java. util.
OverviewThis chapter provides a detailed introduction to the Concurrentskiplistset class in the Java.util.concurrent package. The content includes:Concurrentskiplistset IntroductionConcurrentskiplistset principles and data structuresList of Concurrentskiplistset functionsConcurrentskiplistset Source code (JDK1.7.0_40 version)Concurrentskiplistset ExampleReprint Please specify source: http://www.cnblogs.com/skywang12345/p/3498634.htmlConcurrentskiplistset IntroductionConcurrentskiplistset is a th
); - - //traverse: normal for loop - for(intJ=0;j){ - System.out.println (list.get (j)); -}④, Set: A collection that cannot be duplicated.1, Set hashSet = new HashSet ();HashSet: the order of elements cannot be guaranteed; not repeatable; not thread-safe; the collection element can be NULL;For HashSet: if two objects return true through the equals () method, the hashcode values of the two objects should also be the Same. * When an element is deposited into the HashSet c
Set features:
Set cannot remember the order in which elements are added. duplicate elements are not allowed.
Set determines whether two objects are equal using the equals method.
Common subclass:
HashSet Hash Storage, fast query speed
TreeSet ordered Storage
Import java. util. hashSet; import java. util. set; public class SetDemo {public static void main (String [] args) {Set set = new HashSet (); set. add ("well"); // HashSet does not allow repeated
First look at these three interfaces: List, set, map
Implementation classes: arraylist, rule list, treeset, hashset, hashmap, treemap, and hashtable
For the inheritance relationship of Java Collection classes, see:Java Collection framework
Differences between vector and arraylistVector: array-based list encapsulates some functions not available in array for our convenience, and it cannot be restricted by array. The performance cannot surpass array. T
is returned.
Pollfirst ();
Polllast ();
Get the element, but the element is deleted. If no element exists in the Set, null is returned.
These methods of linklist can simulate the storage process of heap and stack.Set
1. hashset: the underlying data structure is a hash table. Is thread unsafe. Do not synchronize.
2. treeset: You can sort the elements in the Set set.
Use set to save elements. The elements are unique.
1. How does a hashset save unique e
rotation if I take out a value for the element, for example, String str = it. next? The answer is no. First, adding generics and compiling can ensure that the data in the set is of the specified type. Therefore, no elements other than the specified type will appear. In fact, a step-by-step action is automatically added during running. You can use the getClass () method to obtain the element type and then perform a conversion operation on it, so that you do not need to force the conversion.
18-
Java Collection frameworkCollection framework Collection | -- List set. elements are ordered (stored and retrieved) and can have repeated elements because the Collection system has an index. | -- ArrayList set. The underlying data is an array structure. Features: Fast query and slow addition/deletion. The thread is not synchronized. | -- Sort list set. The underlying data is a linked list structure. Features: Fast addition, deletion, and slow query. Threads are not synchronized | -- Vector set.
For a long time, the code has been used for a lot of data lists, mainly list, and are ArrayList, feel that this thing is enough. ArrayList is used to implement a dynamic array of packaging tool classes, so write code can be pulled into the pull, iterative traversal, quite convenient.
Also do not know since when start slowly in the code will often appear hashmap and hashset and so on tool class. Should say HashMap more, and still is the interview classics question, usually also will see more. Wh
the set that are contained in the specified collection
retainAll(Collection: Preserves only those elements in the set that are contained in the specified collection
size(): Returns the number of elements in a set
toArray(): Returns an array containing all the elements in the set
Compared to the egg, the set has no get method to get the element object, only through the iterator to access the data
Our common set implementation classes are: Tree
, using generics avoids classcastexception at run time.Using the invariant Class (immutable Class) provided by the JDK as the key to the map avoids implementing the hashcode () and Equals () methods for our own classes.The interface is better than the implementation when programming.The underlying collection is actually empty, the return length is a collection of 0 or an array, do not return null.16. What are the differences between enumeration interface and iterator interface?Reference answerTh
addition is different from the order of printing.) )Four, TreeSet Collection ClassTreeSet Collection classes: Elements are sorted using the natural order of the elements, and are implemented using a two-fork tree at the bottom. Note: The objects you have stored in need to have natural sorting characteristics.TreeSet Use note points:1. When adding objects to TreeSet, if the object has a naturally ordered
1. collection list set map difference memory
These represent the set in Java. Here, we mainly analyze whether its elements are ordered and whether they can be repeat for different memory for proper use. Of course, there are still synchronization differences, see the previous article.
Ordered or not
Allow repeated Elements
Collection
No
Yes
List
Yes
Yes
Set
Abstractset
No
No
Hashset
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.