1. The difference between ArrayList and LinkList
- ArrayList is the data structure of array and LinList is the data structure of linked list
- During random access, ArrayList is more efficient because LinkList needs to move the pointer, and ArrayList is a data structure given index, which can be mapped directly to.
- LinkList is more efficient when inserting and deleting data, because ArrayList needs to move a large amount of data
- LinkedList is more expensive than ArrayList because LinkedList nodes store references as well as data.
Coolections.sort and arrays Implementation principle of sort
- Collection.sort is to sort the list, array Sort is to sort the array.
HashMap principle, what changes has java8 made
- HashMap is a collection container that stores data in key value pairs
- HashMap is non thread safe
- The underlying data structure of HashMap: array + (linked list, red black tree). Jdk8 was previously implemented in the way of array + linked list, and jdk8 introduced red black tree
- The default length of HashMap array is 16, and null is allowed for both key and value
- The internal implementation array of HashMap is the Node [] array, which stores the nodes of key value pairs. HashMap is stored and obtained through put and get methods.
The difference between List, set and map
- List accesses elements by index. They are ordered. Elements are allowed to be repeated, and multiple null s can be inserted.
- Set cannot store duplicate elements. Only one null is allowed for unordered elements.
- Map saves the mapping of key value pairs. The mapping relationship can be one-to-one or many to one
- List can be implemented based on array and linked list
- Set and Map containers can be implemented based on Hash storage and red black tree
- Set is implemented based on Map. The element value in set is the key value of Map
Similarities and differences of HashMap, hashtable and concurrenthash
Bottom implementation | Can it be blank | Is the thread safe | Initial capacity and expansion | |
---|---|---|---|---|
HashMap | Linked list + array + red black tree | You can store null keys and null values | Thread unsafe | The initial capacity is 16, and the n-th power of each expansion is 2 |
HashTable | Linked list + array + red black tree | Neither key nor value can be empty | Thread unsafe use of synchronized keyword | Initial capacity 11 |
ConcurrentHashMap | Linked list + array + red black tree | null keys and values cannot be stored | Thread safe, using lock segmentation technology to ensure thread safety |
Write a piece of code to remove an element when traversing the ArrayList
//Flashback traversal deletion
for(int i=list.size()-1;i>-1;i--){ if(list.get(i).equals("jay")){ list.remove(list.get(i); } }
//Iterator deletion
Iterator itr = list.iterator(); while(itr.hasNext()){ if(itr.next().equals("jay"){ itr.remove(); } }
//How to print arrays
public class Test{ public static void main(String[] args){ String[] jayArray ={"jay","boy"}; Stream.of(jayArray).forEach(System.out::println); System.out.println(Arrays.toString(jayArray)) } } //output jay boy //output2 [jay,boy]
HashMap expansion process
- The first step is to double the length of the array
- The second step is to recalculate the elements of the old array and insert the hash into the new array
What is the difference between ArrayList and Vector
- vector is thread safe, but ArrayList is not thread safe
- When the underlying array is not enough, the ArrayList is expanded by 0.5 times and the vector is expanded by 1 times
- As long as Vector is a key operation, the synchronized keyword is added in front of the method to ensure the safety of the thread.
How to decide whether to use HashMap or TreeMap?
- TreeMap implements the SortMap interface, which can ensure that the records are sorted according to the key value. By default, the records are sorted according to the ascending order of the key. You can also specify the sorting comparator. When traversing TreeMap with Iterator, the records obtained are sorted.
- TreeMap uses Iterator iteration method:
TreeMap<Integer,String> treeMap = new TreeMap(); treeMap.put(1,"hello"); treeMap.put(9,"big"); treeMap.put(18,"beautiful"); treeMap.put(5,"world"); Set set = treeMap.entrySet(); Iterator iterator = set.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } //outPut 1=hello 5=world 9=big 18=beautiful //It has been sorted according to the value of key
How to realize the conversion between array and List?
- To convert List to Array, you must use the toArray of the set (t [] Array):
List<String> list = new ArrayList<String>(); list.add("shuo"); list.add("❤"); list.add("lin"); String[] array = list.toArray(new String[list.size()]); System.out.println(Arrays.toString(array)); //outPut [shuo, ❤, lin]
- Array to List
String[] str = new String[]{"lin","❤","shuo"}; ArrayList<String > arrayList = new ArrayList(str.length); Collections.addAll(arrayList,str); System.out.println(Arrays.toString(arrayList.toArray(new String[str.length]))); //outPut [lin, ❤, shuo]
What is the Iterator? How to use it? What are its characteristics?
Iterator is mainly used to traverse the collection. Its feature is more secure, because it can ensure that the ConcurrentModificationException exception will be thrown when the traversed collection elements are changed.
- The next() method gets the next element in the collection.
- hasNext() checks whether there are any elements in the collection
- remove() deletes the newly returned element from the iterator.
- Foreachremaining (consumer <? Super E > action) traverses all elements.
List<String> list = new ArrayList<>(); Iterator<String> it = list.iterator(); while(it.hasNext()){ String obj = it.next(); System.out.println(obj); }