Set of arrays?
:( array can store basic data types) is a container for storing an object, but the length of the array is fixed, is not suitable for use in the case of an unknown number of objects
Set :( can store objects, object type is not the same) sets of variable length, may be used in most cases
Collection interface is the root interface collection of classes, Java does not provide this direct interface implementation class, but it puts it inherited had two interfaces
Set: does not contain duplicate elements
List: ordered set can contain duplicate elements, it provides a way of accessing by index
Map is another interface Java.util package, and it does not matter the Collection interface, independent of each other, but are part of the set, comprising the Map Key - Value pair of, can not contain duplicate Map Key, but may comprise the same the Value.
Iterator, all collection classes implement Iterator interface, which is an interface for traversing a set of elements, comprising the following three main methods:
- hasnext (): Are there the next element
- next (): returns the next element
- remove (): Delete the current element
Several important interfaces and classes Introduction
- List (ordered, repeatable)
List of objects stored in an orderly, but also repeatable, List index is concerned, with a series of index and related methods, query speed, because the collection into the List insert or delete data, accompanied by behind moving data, the insertion data is very slow
- Set (unordered, can not be repeated)
Set in the object storage are unordered, can not be repeated, the objects in the collection is not sorted in a particular way, simply put objects in the collection.
- Map (key-value pairs, Unique, not unique)
When the key set stored in the Map, the key can not be repeated, the values may be repeated, according to the key value is obtained, to obtain a set of keys when traversing Set map collection of collections Set traversal, to yield the corresponding value.
Traversal
It provides the following four common output in the collection
- Iterator: iterative output, it is the most used output
- ListIterator: Iterator is a sub-interface for outputting the content of the List of specialized '
- Foreach output: new features presented after JDK1.5. Or may set the output array
- For loop
ArrayList和LinkedList
ArrayList and LinkedList no difference in usage, but there are differences in function of
LinkedList is often used in the case of many additions and deletions operation and query very little
Arraylist more often used in queries, additions less under operating conditions.
Map collection
Implementation class: HashMap, HashTable, LinkedHashMap and TreeMap
HashMap
Map HashMap it is the most common storage element according to HashCode bond, it can be obtained directly from the value of the key, with fast access speed.
Traversing, in order to obtain the data is completely random because the key must not repeat, so HashMap allows a maximum of one key record is Null, to allow a plurality of records is Null, unsynchronized
HashTable
HashTable and HashMap is similar to HashMap is thread-safe version, it supports simultaneous threads, at any one time only one thread can write HashTable
HashTable therefore resulted in writing will be relatively slow, it integrates sub Dictionary class, it does not allow the recording of different key or value is null, while the lower efficiency
LinkedHashMap
LinkedHashMap Save to record the insertion order, when traversed by Iteraor LinkedHashMap, to get the record must be inserted first, when traversing slower than HashMap, HashMap has all the characteristics of
TreeMap
TreeMap implement SortMap interface able to save records based on the key sequence, the default is ascending sort key value (natural sequence), you can specify the sort comparator when traversing ThreeMap with the Iterator, record obtained is row over sequence, Key is not allowed to empty, asynchronous
Map traversal
- KeySet();
Map all the keys will be deposited into the collection set as iterator set times, it is possible to remove all the keys iteration, then obtaining a value corresponding to each key according to the get method, KeySets () after only through iterative get () take Key.
- ebtrySet ()
Set <Map.Entry <K, V >> entrySet () Returns a Set view of the mappings contained in this map value (that is, a relationship of a part) is to (Key-value) as a whole is stored in one set in the collection, Map.Entry a map showing the relationship between
EntrySet () after iteration may e.getKey (), e.getValue () and two methods come Key Value, the return value is the interface Entry
Recommended entrySet () method, high efficiency
For KeySet actually traversed twice, once into iterator, one that is removed from the Key Value corresponding to the HashMap
Entryset () just to traverse the first time, the Key and Value have it put in the entry, so fast. Two kinds of traversal of traversal time difference is obvious.
ArrayList difference and the LinkedList
- ArrayList data structure is implemented based on dynamic array, LinkedList based linked list data structure
- For random access get and set, ArrayList LinkedList superior performance, as to move the pointer LinkedList
- For new and delete, add and remove, linkedList comparative advantage, because ArrayList to move data, it depends on the actual situation, if the value of a single insertion or deletion of data, but the speed is better than ArrayList LinkedList, but if batch random insertion and deletion of data, LinkedList chance to read much better than ArrayList, ArrayList because each insert a data, all the data you want to move the insertion point and after
HashMap与TreeMap
- HashMap by its hashcode quickly find content, and TreeMap all the elements have maintained a certain fixed order, if you need to get a result you should have a lot to use TreeMap (HashMap elements in the order is not fixed)
- Inserted in a Map, delete and locating elements, HashMap is the best choice, but if you want to follow the natural order or custom order traversal key, then TreeMap would be better to use key class HashMap require the addition of a clear definition of the hashCode () and equals () implementation
Even as a Map of elements, but the order is not the same, resulting in hashCode () is not the same
Do the same test:
In the HashMap, map the same value. Different order, when equals, false
In TreeMap, the same map values, different order, when equals, true, described in TreeMap equals (time) of the sequence is analyzed.
HashTable difference between the HashMap
- Synchronization: HashTable are thread-safe, that is synchronized, and HashMap is thread safe, not synchronized
- HashMap allows one to null key, the plurality of elements is null
- HashTable of Key and Value are not allowed to be null
Java What Collections Framework is to say some of the advantages of the Collections Framework
Each programming language has a set of the original Java version includes hits collections: Vector, Stack, HashTable and Array, with the widespread use of the collection, Java1.2 proposed to include all collection interfaces, and algorithms to achieve the set framework in the case of guarantee thread-safe, and concurrent use of generic collection classes, Java has experienced for a long time, it also includes Java and contracting, the blocking interface as well as their implementation. Some of the benefits of the following set of frame
- The use of core collections to reduce development costs, rather than implement our own collection classes
- By using a combination of the framework through rigorous testing, code quality will be improved
- By using the supplied collections JDK, code maintenance costs can be reduced
- Operability and reusability
Collections Framework Generics What are the advantages
Java1.5 introduced generics, all the collection interfaces and implementations use a lot of it, allows us to offer a generic object types can accommodate is a collection, so if you want to add any other type of element, it will be in compile-time error, to avoid errors when run, so you will get an error message at compile time, the generic code is clean, we do not need to use explicit conversion operators and instanceOf.
Java basic framework of what set of interfaces
Collection is a set of interfaces root level, a collection on behalf of a group of objects that is, its elements, Java does not provide any direct implementation of this interface.
Set is a collection that contains no duplicate elements, this collection of abstract digital interface modeling is used to represent a collection, just like a deck of cards.
List is an ordered collection, can comprise repeated elements, you can access any element by its index, Transform List like dynamic array length
Map is a map to the Key Value of the object, a Map can not contain duplicate Key, each Key can map up to a Value
Why not inherited Map interface Collection Interface
Despite the Map interface and its implementations are part of a collection of frames, but not a collection Map collection Map is not, therefore inherit Map Collection meaningless, and vice versa
Iterator what is?
Iterator interface provides an interface to traverse any Collection, we can use the method from a Collection iterator to obtain the iterator instance, iterators allows the caller in the iterative process element removal
HashMap How it works
HashMap using the hash algorithm, the put and get methods, he is using hashCode () and equals () method, when we passed through Key-value pair, call the put method, using HashMap key hashCode () and hash algorithm whether, to find out the index Entry storage storage Key-value pairs in the LinkedList, if Entry exist, it uses equals () method to check delivery Key already exists if there is, it will cover the value, if not, it will create a new entry and then save, let us pass key calls get () method is that it uses again hashCode () to find the index into the array, and then use the equals () method to find the right entry, and then returns its value.
HashCode () and equals () methods and the importance of
HashMap use Key object HashCode () and equals () method to determine the index Key-Value pairs, when we try to get the value from the HashMap, these methods will be used, if these methods are not implemented correctly, in this case, two different Key will produce the same HashCode () and equals () output
HashMap will think they are the same, then covered them, rather than store them to different places, the same set of classes all stored duplicate data does not allow the use HashCode () and equals () to find duplicate, so they are very important
Implement Equals () and hashCode () should follow the following rules:
- If o1.equals (o2), then o1, hashCode () == o2.hashCode () is always true of
- If o1.hashCode () == 02.hashCode (), does not mean o1.equals (o2) will be true.
simply speaking:
HashCode same, the same is not necessarily an object
Of the same object, HashCode necessarily the same.
Can you use any kind as a Key Map.
can!
Before using the points to consider
- If the class overrides the equals () method, it should be rewritten HashCode () method
- All instances of a class to be followed and equals () and HashCode () the relevant rules
- If a class does not use equals (), you should not use it in HashCode () in
HashMap and HashTable What is the difference
- HashMap allows key and value is empty, but does not allow HashTable
- HashTable is synchronized to be safe, rather than HashMap, so for single-threaded environment HashMap, HashTable for multi-threaded environment
How decided to use HashMap or ThreeMap
Insertion in the Map, the operation of deleting such an element and positioning, the HashMap is selected hole, however, if you need an ordered set of Key traverse, ThreeMap a higher selectivity, based on the size of your Collection, HashMap may want to add elements faster, the transposition TheeMap orderly Key Map traversal
ArrayList similarities and differences and Vertor
ArrayList and Vector are very similar in many cases
- Both are based on the index, and an array of internal support
- Both maintain the order of insertion, we can get to the element according to insertion order
- Both ArrayList and Vector allow null value, the index value may be used for random access to the elements
difference
- Vector is synchronized to be safe, rather than ArrayList
- ArrayList is faster than Vector
- ArrayList more common
Array What is the difference and ArrayList? When more suitable Array?
- Array basic types and can accommodate objects, and can accommodate objects ArrayList
- Array is specified size, the size is fixed ArrayList
- Array not so much ArrayList function, such as addAll, rumoveAll and Iterator etc.
- If the size of the list has been specified, most of the love ah mad or storage and traversing them
- If you are using multidimensional arrays, use Array [] [] than the List <List <>> more easily
ArrayList What is the difference and LinkedList?
Both have achieved ArrayList and Linked List interface, but there are some differences between them
- ArrrayList time-based data structure is a dynamic array, ListedList linked list data structure is based on
- For random access get () and set () ArrayList LinkedList than speed, as to move the pointer LinkedList
- For new and delete operations add and remove, LinkedList comparative advantage, because ArrayList to move data,
- Both are readily available unsafe allow duplicate.
Which collections offer random access to the elements?
ArrayList, HashMap, TreeMap and HashTable provide random access to elements.