List set of Java sets and list of java Sets
1. What is an Lsit collection?
A List set is an ordered and repeatable set of elements. Each element in the SET has its own element index. The List set allows repeated elements. You can use indexes to access elements at a specified position.
2. Java 8 Improved List interface and ListIterator Interface
List is a sub-interface of the Collection set. You can use all methods of the Collection interface. In addition, because List is an ordered set, some methods are added to manipulate the List set based on the index.
Void add (int index, Object element ):Add the element to the index of the List set.
Boolean addAll (int index, Collection c ):Insert all elements in the Collection to the index of the List set.
Object get (int index ):Returns the element at the index.
Int indexOf (Object o ):Returns the index of the position where element o appears for the first time in the list set.
Int lastIndexOf (Object O ):Returns the position index of the last occurrence of element o in the List set.
Object set (int index, Object element ):Replace the element whose location index is index with element.
List subList (int fromIndex, int toIndex ):Returns all elements from the index fromIndex to the index toIndex in the List set.
Java 8 adds the following two default methods to the List set:
Void replaceAll (UnaryOperator operator ):Reset all elements in the List set according to the calculation rules specified by operator.
Void sort (Comparator c ):Sort the elements of the List set based on the Comparator parameter.
Public class ListTest {public static void main (String [] args) {List s = new ArrayList (); s. add (new String ("Spring"); s. add (new String ("Autumn"); s. add (new String ("Winter"); // output: [Spring, autumn, winter] System. out. println (s); s. add (1, new String ("Summer"); s. remove (2); // output: [spring, summer, winter] System. out. println (s); // output: 1 System. out. println (s. indexOf (new String (""); s. set (1, new String ("Autumn"); // output: [Spring, autumn, winter] System. out. println (s); // output: [Autumn] // includes fromIndex itself, but does not include the toFrom System. out. println (s. subList (1, 2 ));}}
Public class ListTest0 {public static void main (String [] args) {List books = new ArrayList (); books. add (new String ("lightweight java EE Enterprise Application Development"); books. add (new String ("Crazy java handout"); books. add (new String ("crazy Android handout"); books. add (new String ("Crazy ios handout"); // sort books on the List set using a Lambda expression with the target type Comparator. sort (o1, o2)-> (String) o1 ). length ()-(String) o2 ). length (); // output: [Crazy ios handouts, crazy java handouts, crazy Android handouts, lightweight java EE Enterprise Application Development] System. out. println (books); books. replaceAll (ele-> (String) ele ). length (); // output: [7, 9, 11, 17] System. out. println (books );}}
3. ArrayList and Vector implementation classes
The ArrayList and Vector classes are both List classes based on arrays. Therefore, the ArrayList and Vector classes encapsulate a dynamic array that is allowed in the allocated Object [] array. ArrayList and Vector objects use the initialCapacity parameter to set the length of the array. When the elements added to the Arraylist or Vector exceed the length of the array, their initialCapacity will automatically increase..
ArrayList and Vector provide two methods to re-allocate the Object [] array:
Void ensureCapacity (int minCapacity ):Increase the length of the Object [] array in the ArrayList or Vector set to a value greater than or equal to the minCapacity value.
Void trimToSize ():Adjust the length of the Objec [] array in the ArrayList or Vector set to the number of current elements. This method can be called to reduce the storage space occupied by ArrayList or Vector set objects.
ArrayList and Vecto are almost identical in usage, but because Object [] is an old collection (JDK1.0 is available ), therefore, Vector provided many methods with long names earlier. Methods with short Chinese names of Vector were subsequently added.
A significant difference between ArrayList and Vector is that ArrayList is thread-safe. When multiple threads access the same ArrayList set, if more than one thread modifies the ArrayList set, the program must manually ensure the synchronization of the set. However, the Vector is thread-safe and does not need to ensure the synchronization of the set. Because the Vector is thread-safe, the performance of the Vector is lower than that of the ArrayList set. In fact, even if the thread security of the List set needs to be ensured, Vector is not recommended.
4. fixed-length List
Arrays is a tool class for manipulating Arrays. This tool class provides asList (Object... a) method. This method can convert an array or a specified number of objects into a List set. This List set is neither an ArrayList implementation class instance nor a Vector instance class, it is an ArrayList instance of arrays's internal class.
Arrays. ArrayList is a fixed-length List set. The program can only traverse and access the elements in the Set, and cannot add or delete the elements in the set.
Public class FixedsizeList {public static void main (String [] args) {List fixedList = Arrays. asList ("Crazy java handout", "Crazy ios handout"); // gets the implementation class of fixedList // output: class java. util. arrays $ ArrayList System. out. println (fixedList. getClass (); // output: Crazy java handout // crazy ios handout fixedList. forEach (System. out: println); // an UnsupportedOperationException occurs when an element is deleted. // fixedList. add ("crazy Android handout ");}}