This is my first time to write a blog, learning the path of Java by writing a blog record down, if the description has errors, please guide!
1.1 Evolution of the collection
There were only 3 collection classes in early Java, namely, the Vector class, the Hashtable class, and the Stack class. These 3 classes provide basic collection functionality, but are not entirely satisfactory. The solution does not have the flexibility to allow us to implement multiple scenarios, all of which are synchronous methods to ensure thread safety, but this also results in a performance penalty.
But there was another major change after Java5, and the new features even changed the use syntax of the collection class.
before Java5, the base type value cannot be added directly in the collection, for example, when you add an int value, you must first encapsulate it in an instance of the corresponding integer wrapper. But. Since Java5 introduced the feature called Automatic unboxing/boxing, we do not need to manually encapsulate the instance of the corresponding integer wrapper, which is done automatically by the IDE for us, and we do not see the process.
Map map=new HashMap ();//java1.5, adding an int value must first be encapsulated in an instance of the corresponding Integer wrapper map.put ("One", new Integer (1));//auto-boxing for us map.put (" ", 1);
1.2 Integrated classes and interfaces
objects added to the collection are called elements (element), and some collection classes allow duplicate elements, and some things are not allowed. The repetition here refers to comparing 2 elements with the Equals () method, or repeating if the return value is true, or vice versa.
1.21 Collection
The top of the collection class hierarchy is the collection interface, and most of the collection classes implement methods that are often used in that interface.
If you want to access a collection element, you can access it through the iterator () method and indicate that the object returned by the method implements the iterator interface, that is, the iterator () method returns a iterator object, using the method provided by the object to read the collection in the object in turn.
List<student> list=new arraylist<student> (); Student student1=new Student () student1.setstuname ("Xiao Han"); Student1.setstuage (20); Student student2=new Student () student2.setstuname ("Xiao Han"); Student2.setstuage (List.add); Student1, List.add ( STUDENT2);//Gets the current iterator object iterator<student> it=list.iterator ();//Indicates whether the iterator has returned a reference to all objects in the collection while (It.hasnext ()) {//Returns an application pointing to the next object in the collection Student stu=it.next (); System.out.println (Stu.getstuname () + "" +stu.getstuage ());}
1.22 List
One of the characteristics of a collection class is a meaningful ordering of the elements in the collection, the list interface defines the sort, that is, if the list is used to implement and retrieve references to elements, the order in which the elements are returned can be ordered, The order is determined by the position of the element in the collection, and the element position can be displayed or implicitly specified when the element is added. Below I use the following code to demonstrate the following
List<student>stulist=new arraylist<student> (); Stulist.add (New Student (123, "Xiao Han", 22)); Stulist.add (New Student (456, "Little Dew", 22));
The object in the code whose name is Xiao Han occupies the first position of the list, that is, index 0, and the small Dew object occupies the second position, that is, element is inserted at the end if the specified element's position is not displayed. Conversely, if we specify a specific location, you can try it by writing demo
List<student> list=new arraylist<student> (); Student student1=new Student () student1.setstuname ("Xiao Han"); Student1.setstuage (20); Student student2=new Student () student2.setstuname ("Xiao Lu"); Student2.setstuage (20); Student student3=new Student () student3.setstuname ("Small Miss"); Student3.setstuage (list.add); student1 STUDENT2); List.add (1,STUDENT3);//Get current Iterator Object iterator<student> it=list.iterator ();// Indicates whether the iterator has returned a reference to all objects in the collection while (It.hasnext ()) {//Returns an application that points to the next object in the collection Student Stu=it.next (); System.out.println (Stu.getstuname () + "" +stu.getstuage ());}
The output is:
Xiao Han 20
Little Miss 20
Little Dew 20
List.add (1,STUDENT3);
The Add () overload call, the first parameter indicates that the student object is inserted into the list of the corresponding index 1 position, the position was originally occupied by the small dew object, after the execution of this code, the new object will be inserted into the collection of 2 existing objects, so the index of the small dew object becomes 2.
There is another method, the set () method, similar to add, but the set () method is used to replace the current object at the specified position.
The list interface defines not only the add () method with the unknown index, but also the remove () method of removing an object by index, as in the following code
Stulist.remove (0);
This removes the object at index position 0, which is the object of Little Han, which removes the first element of the collection, and the other object's index shifts downward as a result of the shift operation. The internal principle of the Remove () method should be the same, call the Remove () method to look up the entire list and compare each element in the list to the object that passed in the method, if the list contains few elements, or if the element that needs to be removed is positioned in front of it, the process is fast, however, If the list is large or relatively lean, you need to traverse and compare multiple times to find the corresponding object and remove it. Not just the Remove () method, such as the contains () method and the IndexOf () method defined in the collection interface, have this limitation.
1.23 ArrayList
Some of the traits of ArrayList are as follows
(1) can contain duplicate elements in ArrayList
(2) can add null value in ArrayList
(3) The ArrayList class is not inherently thread-safe, so when creating multithreaded use ArrayList, it is your responsibility to synchronize changes to ArrayList.
This is my first blog to learn Java, the wrong place please advice!
Java Collection 1