Detailed explanation of set relation diagram and common operations in Java
The following is a downloaded inheritance diagram of collection types in Java to facilitate correct understanding and use of corresponding collection types.
Several interview FAQs:
1. Q: what is the difference between ArrayList and vector? What is the difference between HashMap and hashtable?
A: Vector and hashtable are thread synchronized. In terms of performance, ArrayList and HashMap are better than vector and hashtable respectively.
2. Q: roughly explain the architecture of Java collection
A: List, set and map are the three main interfaces in this collection system. Where list and set inherit from the collection interface. Set does not allow duplicate elements. HashSet and TreeSet are the two main implementation classes. The list is ordered and allows elements to repeat. ArrayList, LinkedList, and vector are the three main implementation classes. Map also belongs to the collection system, but it is different from the collection interface. Map is the mapping set of key to value, and the key column is a set. Key cannot be repeated, but value can be repeated. HashMap, treemap and hashtable are the three main implementation classes. Sortedset and SortedMap interfaces sort elements according to specified rules. SortedMap sorts key columns.
3. Q: difference between comparable and comparator
A: Call Java util. Collections. When sorting with the sort (list) method, all objects in the list must implement the comparable interface. java. util. Collections. Sort (list, comparator C), you can temporarily declare a comparator to implement sorting. Collections. sort(imageList,new Comparator() { public int compare(Object a,Object b) { int orderA = Integer.parseInt( ( (Image) a).getSequence()); int orderB = Integer.parseInt( ( (Image) b).getSequence()); return orderA - orderB; } }); If you need to change the order, change it to return orderb - ordera.
The list interface simply extends the collection. Its specific implementation classes commonly include ArrayList and LinkedList. You can put anything into a list container and take it out when you need it. From its name, ArrayList is stored in the form of an array, so its random access speed is very fast. The internal implementation of LinkedList is a linked list, which is suitable for frequent insertion and deletion operations in the middle of the linked list. You can choose freely according to your needs in specific applications. The iterator mentioned earlier can only traverse the container forward, while the listiterator inherits the idea of iterator and provides a two-way traversal method for the list.
The set interface is also an extension of the collection. Unlike the list, the object elements in the set cannot be repeated, that is, you cannot put the same thing into the same set container twice. Its common concrete implementations include HashSet and TreeSet classes. HashSet can quickly locate an element, but the object you put in the HashSet needs to implement the hashcode () method, which uses the hash code algorithm mentioned above. TreeSet stores the elements in order, which requires that the objects you put in are sortable, which uses the other two utility classes comparable and comparator provided by the collection framework. If a class is sortable, it should implement the comparable interface. Sometimes multiple classes have the same sorting algorithm, so there is no need to repeatedly define the same sorting algorithm in each class, as long as the comparator interface is implemented. There are also two useful common classes in the collection framework: collections and arrays. Collections provides some very useful methods for sorting, copying, finding and filling a collection container, while arrays performs similar operations on an array.
Map is a container that associates key objects with value objects, and a value object can be a map, and so on, so as to form a multi-level mapping. For key objects, like set, key objects in a map container are not allowed to be repeated in order to maintain the consistency of search results; If two key objects are the same, there is a problem when you want to get the value object corresponding to the key object. Maybe you don't get the value object you want, which will cause confusion. Therefore, the uniqueness of the key is very important and conforms to the nature of the set. Of course, during use, the value object corresponding to a key may change. At this time, the value object will correspond to the key according to the last modified value object. There is no uniqueness requirement for value objects. You can map any number of keys to a value object, There will be no problem (however, it may be inconvenient for you to use. You don't know which key you get is the value object corresponding to). There are two common implementations of map: HashMap and treemap. HashMap also uses the hash code algorithm to quickly find a key. Treemap stores the keys in order, so it has some extension methods, such as firstkey(), Lastkey (), you can also specify a range from the treemap to get its sub map. The association between key and value is very simple. You can associate a key with a value object by using the pub (object key, object value) method. Use get (object key) to get the value object corresponding to this key object.
How to traverse the map:
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.