Advantages of collection than arrays:
1. The set can store any type of variable, the length is variable
2. Arrays can only have the same type of variable, the length is fixed
3. Only objects can be stored in the collection
List collection: Interfaces provide public methods
Unique method:
The unique approach in 1.List is to manipulate elements by subscript
Find:
Add (int index,object 0) Adds an element to the specified position
Get (int index), gets the element at the specified position in the collection
Set (int index,object o), which is used to decorate the value of the element at the specified position in the collection
Iterators:
Function: Manipulate the elements in the set, can (increase, delete, change, check)
Root iterators: Getting iterators in the Iterator interface set: Getting iterators through methods in the collection
Get iterator in collection collection: Iterator ();
Methods in Iterators:
Hasnext ();//Determine if there are elements in the position pointed to by the current pointer
Next (); Gets the element that the current pointer is pointing to, and the pointer moves down one
Remove ();//Remove the last action is the element pointed to by the pointer
Listiterator: A unique method in:
Hasprpvious (); Determines whether an element exists at the previous position pointed to by the current pointer
Provious (); Get previous element
Proviousindex (); Gets the location of the previous element
Add (); Adds an element where the current pointer is pointing
Set (); Modify the element that the current pointer points to
1.add and set cannot be used by colleagues
2. If you use iterators to manipulate elements, do not use collections to manipulate elements
Arratlist: The bottom is to maintain a object[] array, the default capacity is 10, if the capacity is not enough, increase to 1.5 times times the original,
Pros: Find blocks, additions and deletions slow
TrimToSize (). Adjust the collection's capacity and data consistency
ensurecapacity (int mincapacity) custom capacity
LinkedList: Connection list Implementation, features: Add elements faster. Find Slow
LinkedList-specific methods:
Method Description:
AddFirst (e E)
AddLast ()
GetLast ()
Removefirst ()
Removelast ()
If there are no elements in the collection, get or delete elements thrown: nosuchelementexception: No exception found for the element
Data:
The first method: List.push ("* * *")//pushes a value into the stack of the collection
SYSTEM.OUT.PRINTLN ("Fetch value from Stack in collection:" +list.pop ());//dog Remove the push-in Object Pop () takes the topmost object in the stack
Advanced First Out:
List.offer ("Dog Doll");
SYSTEM.OUT.PRINTN (List.poll ()); The element is removed from the collection
Get an iterator for reverse order
descendingIterator()
Returns an iterator that iterates over the elements of this double-ended queue in reverse order.
Eg:iterator it= list. descendingIterator()
while (It.hasnext) {
SYSTEM.OUT.PRINTLN (IT); Print it back.
}
Vector: Describes a thread-safe ArrayList using the same as ArrayList (just understand)
Vector and ArrayList differences
Same point: The underlying is implemented with an object array
Different points:
1.ArrarList is the thread is out of sync, operation efficiency is high, ArrayList is appearing in jdk1.2
2.Vector threads are synchronous and operate inefficiently, which occurs at jdk1.0 time.
Set set: is also an interface, inheriting the collection interface, the element is unordered, cannot use the index value to take the value, cannot repeat
HashSet is worth a principle:
First, the object's Hashcode method is called to get the Hashcode value, and a position is obtained by the shift operation.
Scenario 1: If there is no element at the location, store the element directly in that position
Case 2: If there is already an element in the location, compare the object's Equals method with an object that already exists in the position, and if equals returns True Then it represents a duplicate object, it does not exist, and if the return is false, the object and the original object exist together
To traverse a set collection: 1. Change the collection to an array, 2. Iterate through iterators (unordered: The order of addition is different from the order printed)
Treeset: Sorts elements using the self-heating order of the elements, using a two-fork tree for the underlying
TreeSet Use note points:
1. When adding objects to TreeSet, if the object has this naturally ordered attribute, sort by this natural sort
2. When adding objects to treeset, such as the object itself does not have the nature of the natural ordering, the return error when running, if you need to store objects, then the object's class must implement the CompareTo interface defines the specification of the element comparison in the CompareTo method
3. When adding objects to TreeSet, if the object itself does not have a natural ordering feature and does not implement the comparable interface, then this time it is necessary to create a treeset to pass in a comparator
How comparators are defined:
Class name Inplements compartor{
}
4. If the comparable interface is implemented in the class, and a comparator is passed in when the TreeSet is created, the comparator is the standard.
Collections in Java