Java Summary 1111/1112

A: Some features of the underlying sub-implementation class of the List collection:
ArrayList: The underlying data structure is in the form of an array, fast query, slow addition and deletion
Thread perspective: Single-threaded unsafe execution is efficient
Since ArrayList is a sub-implementation class of List, elements can be repeated and stored and retrieved consistently
LinkedList: The underlying data structure is in the form of a linked list, the query is slow, and the addition and deletion are fast
Thread perspective: single thread unsafe high execution efficiency

Unique features:
public void addFirst(E e) inserts the specified element at the beginning of the linked list
public void addLast(E e) inserts the specified element at the end of the linked list

public Object getFirst() returns the first element at the beginning of the linked list
public Object getLast()  returns the last element of the linked list

public Object removeFirst() removes and returns the first element of the linked list
public Object removeLast() removes and returns the last element of the linked list

Vector: The underlying data structure is in the form of an array, the query is fast, the addition and deletion are slow 
Threading point of view: Multi-threaded safe execution is inefficient
Unique features:
public void addElement(E obj) ------> equivalent to add(Object e)
public Enumeration<E> elements ------>相当于 Iterator iterator()


Enumeration<E> interface: vector enumeration of components has two methods
boolean hasMoreElements():------> equivalent to:hasNext()
Object nextElement():----------> Equivalent to: next();

Source code:

Synchronized: synchronization lock (in multi-threading): it can guarantee thread safety!
public synchronized void addElement(E obj) {//The synchronization method evolved from the synchronization code block
 
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = obj;


If you implement a multithreaded program, you generally need thread-safe classes: such as
StringBuffer Vertor hashtable

synchronized(synchronized lock object) {
Code:
}

B: Generics: 
Java introduces generics according to the characteristics of arrays: <>
Generics: A special type that provides explicit work on data types ahead of time when objects are created or methods are called.
Parameterized types, which can be passed like parameters
Format: 
<reference type>
Generics can only hold reference types

The benefits of generics:
Move runtime exceptions to compile time (class conversion exceptions)
No downcast required
Fixed yellow warning line issue

Generic application: 
Define generics on the class
class Object<E>
Define generics on methods
public <E> void show(E e) 
Define generics on interfaces
interface Inter<E>{
public abstract void show(E e);
}


Advanced Generics: Wildcards
 <?> : Can be any type, including Object type and any Java class
 <? extends E>: down-limited, type E and subclasses of type E
 <? super E>: Up-limited, type E and parent class of type E

C: Enhanced for loop:
  writing format:
  for(data type variable name in collection or array: object name of collection or array){
  output variable name;
  }
Disadvantages:
When traversing an array or collection, the object of the array or collection cannot be empty 
Otherwise, a null pointer exception will occur
Generally, when using the enhanced for loop, a non-empty judgment is made.
Enhanced for loop is to replace iterator traversal

Traversing the ArrayList collection:
1) toArray traversal
2) Iterator traversal in the Collection collection
3) Iterator traversal in List collection
4) Use get() and size() and normal for loop to traverse
5) Traverse with enhanced for loop

Nested traversal of collections:
Suppose there is a Java class, regarded as a container, there are many students in this container
 ArrayList<Student>, but more than one Java class,
 Large collection: ArrayList<ArrayList<Student>>
 
D : Set collection
The Set collection inherits from the Collection collection
The underlying data structure is a hash table
It can guarantee the uniqueness of the elements, and the elements are not repeated
It is instantiated through the underlying collection HashSet collection, the bottom layer of HashSet is an instance of HashMap collection
This class implements the Set interface and is backed by a hash table (actually a HashMap instance). It does not guarantee the iteration order of the set;
  In particular it does not guarantee that the order will be permanent. .
The difference between List collection and Set collection:
Set collection: the elements are unique, the collection is unordered (inconsistent storage and retrieval)
List collection: elements can be repeated, the collection is ordered (storage and retrieval are consistent)

The bottom layer of HashSet relies on the two methods of HashCode and equals to ensure the uniqueness of elements
If you want the collection of custom objects not to repeat, you need to override these two methods in the custom object
Whether the HashCode value corresponding to each element is the same, if the HashCode value is the same, also compare their equals() method


E: LinkedHashSet collection:
The bottom layer ensures the uniqueness of elements by a hash table
The order of the elements is ensured by the linked list

F: TreeSet collection:
The TreeSet collection is a key point in the Set collection
The bottom layer of TreeSet is an instance that depends on TreeMap, and TreeMap<K,V> is implemented by relying on the red-black tree structure
There are two types:
1) Natural sorting:
2) Comparator sort:
The use of this sort depends on what constructor the user uses
When TreeSet is constructed using TreeSet() without parameters 
You must make the custom object Student implements Comparable inside
And it needs to override the compareTo method

When TreeSet is constructed using TreeSet(Comparator<? super E> comparator)

Method 1) Define a sub-implementation class MyComparator of an interface Comparator and override the compare method in it

Way 2) Use anonymous inner class:

TreeSet<Student> st = new TreeSet<Student>(new Comparator<Student>{

@Override
public int compare(Student s1 , Student s2){
//code;

}
});

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325860280&siteId=291194637