Welcome to JAVA Basic Course
Blog address: https://blog.csdn.net/houjiyu...
This series of articles will mainly focus on some of the basic knowledge points of JAVA, summarized for peacetime, whether it is just contacted with JAVA development novice or industry veterans, all hope to bring some help to the broad masses of peers. If you have any questions, please leave a message or add QQ: 243042162.
Message:
Take the long march road again, review the past years, review important historical events, move forward, and measure the long march road of the new era with footsteps. On the way to work, we should also carry forward the spirit of the Long March, persevere, step by step, down-to-earth, and move towards our goals.
aggregate
1. Set Framework Diagram
(1) Abbreviated version
(2) Detailed Edition
2. Set and array distinction
3.Collection interface
-
List interface: Elements are stored in order of entry, repeatable
(1) LinkedList: The underlying data structure is linked list, which is slow to query, fast to add and delete, insecure to threads and efficient to store duplicate elements.
(2) ArrayList: The underlying data structure is an array, querying fast, adding or deleting slowly, thread insecurity, high efficiency, and can store repetitive elements.
(3) Vector: The underlying data structure is array, fast query, slow increase and deletion, thread safety, low efficiency, and can store repetitive elements.
-
Set interface: Receive only once, do not repeat, and do internal sorting
- HashSet stores elements using hash tables (arrays)
Insertion order of maintenance elements in LinkedHashSet linked list - The underlying implementation of TreeSet is a binary tree with ordered elements
- HashSet stores elements using hash tables (arrays)
HashSet is different from TreeSet:
(1) Data in Treeset is automatically ordered and null values are not allowed.
(2) The data in HashSet is disordered and can be put into null, but only one null. The values in both can not be repeated, such as the unique constraints in the database.
(3) HashSet requires that the object put in must implement the HashCode() method. The object put in must be identified by hashcode code, while the String object with the same content, hashcode is the same, so the content put in cannot be repeated. But objects of the same class can be placed in different instances
4.Map
- HashMap and HashTable
HashMap and Hashtable both implement Map interfaces, so many features are very similar. However, they have the following differences:
(1) HashMap allows keys and values to be null, while Hashtable does not allow keys or values to be null.
(2) Hashtable is synchronous, but HashMap is not. Therefore, HashMap is more suitable for single-threaded environments, while Hashtable is more suitable for multi-threaded environments.
- TreeMap
5. Collection traversal
(1) list traversal
public class CollectionMain { public static void main(String[] args) { List<String> testList = new ArrayList<>(); testList.add("1"); testList.add("2"); testList.add("3"); System.out.println("Use Iterator iteration....."); Iterator<String> iterator = testList.iterator(); while (iterator.hasNext()){ String value = iterator.next(); System.out.printf("%s ",value); } //When using ListIterator iteration, you also need to start with a forward iteration and then iterate in reverse order. System.out.println("\n\n Use ListIterator iteration....."); System.out.println("forward traversal....."); ListIterator<String> listIterator = testList.listIterator(); while (listIterator.hasNext()){ String value = listIterator.next(); System.out.printf("%s ",value); } System.out.println("\n reverse traversal....."); while (listIterator.hasPrevious()){ String value = listIterator.previous(); System.out.printf("%s ",value); } } }
Output results
Use Iterator iteration... 1 2 3 Use ListIterator iteration... Forward traversal... 1 2 3 Reverse traversal... 3 2 1
(2) map traversal
public class MapMain { public static void main(String[] args) { Map<Integer, String> map = new HashMap<Integer, String>(); map.put(5, "a"); map.put(2, "b"); map.put(3, "c"); map.put(4, "d"); map.put(null, "e");// Same as above, it will screen itself. System.out.println(map.size()); //Way 1: Traverse keys and value s through Map.keySet for(Map.Entry<Integer,String> entry:map.entrySet()){ System.out.println("1,key:"+entry.getKey()+",value:"+entry.getValue()); } //Way 2: Use iterator to traverse key and value through Map.entrySet Iterator<Map.Entry<Integer,String>> iterator=map.entrySet().iterator(); while (iterator.hasNext()){ Map.Entry<Integer,String> entry=iterator.next(); System.out.println("2,key:"+entry.getKey()+",value:"+entry.getValue()); } //Method 3: for(Integer key:map.keySet()){ String v = map.get(key);//Get the value of multiple pairs of values for each key System.out.println("3,key:"+key+",value:"+v); } } }
Output results
5 1,key:null,value:e 1,key:2,value:b 1,key:3,value:c 1,key:4,value:d 1,key:5,value:a 2,key:null,value:e 2,key:2,value:b 2,key:3,value:c 2,key:4,value:d 2,key:5,value:a 3,key:null,value:e 3,key:2,value:b 3,key:3,value:c 3,key:4,value:d 3,key:5,value:a