List Interface
The list interface is a sub-interface of the collection interface, the abstract method in the list interface, and a part of the method and his parent interface collection is the same, the list interface's own unique method, with the function of indexing .
It is a collection of elements that are accessed in an orderly manner. For example, the order in which elements arestored isone, a, and a. In the collection, the storage of the elements is donein the order of one, one, and all.
It is a collection of indexes that can be used to manipulate the elements in the collection precisely (as with the index of an array).
The collection can have duplicate elements, through the equals method of the element, to compare whether it is a repeating element.
Common subclasses of the List interface are:
- ArrayList Collection: The structure of the data store is the array structure . Elements are slow to delete, Find Fast, because the most used features in daily development for querying data, traversing data, so ArrayList is the most common collection.
- LinkedList Collection: The structure of a data store is a linked list structure . A collection of convenient elements to add and delete. The addition and deletion of a collection element in real-world development often involves end-to-end operations, while LinkedList provides a large number of methods for end-to-end operations.
the structure of the list collection to store data
There are a number of collections under the list interface, and the structure in which they are stored is different, resulting in these sets having their own characteristics that can be used in different contexts. The common structures of data storage are: Stacks, queues, arrays, linked lists.
Stack, using the structure of the collection, the access to the element is like the following characteristics:
Advanced-out (that is, the element to be stored in, to be removed after the element after it in turn to remove the element). For example, a bullet is pressed into a magazine, a bullet that is pressed into it, a bullet in the back, a shot in the top, and a bullet that pops up before it pops up.
The entry and exit of the stack is the top position of the stack.
Stack: The element is stored. That is, the element is stored at the top of the stack, and the elements in the stack move one position to the bottom of the stack in turn.
Stack: Just take the element. That is, the top position of the stack element is taken out, the stack of elements in turn to the top of the stack to move a position.
Queue, using the structure of the collection, the access to the element is like the following characteristics:
FIFO (that is, the element that is stored in, to be removed after the element in front of it, before the element can be taken out). For example, security. In a row, each person checks in turn, only the front of the people to complete the inspection, before the current people to check.
Each side of the queue's entrance and exit. For example, the left side of the entry is the entrance and the right is the exit.
Array, using the set of the structure, the access to the element has the following characteristics:
Linked list, using the structure of the collection, the access to the element is like the following characteristics:
Between multiple nodes, connect through an address. For example, many people hold hands, everyone uses their right hand to pull down a person's left hand, and so on, so many people are connected together.
Find elements slow: To find an element, you need to go through the connected node, looking back to the specified element
-
Add element: Operation as shown on the left, just modify the address of the next element to be connected.
Delete element: The action, as shown on the right, simply modifies the address of the next element to be connected.
a collection of class ArrayList1. Add elements to the specified index
/* * Add (int index, E) * Inserts an element into the specified index of the list * indexed operations to prevent cross-border issues java.lang.IndexOutOfBoundsException */public static void function () {list<string> List = new arraylist<string> (); List.add ("ABC1"); List.add ("ABC2"); List.add ("ABC3"); List.add ("ABC4"); SYSTEM.OUT.PRINTLN (list); list. Add (1, "x5456"); SYSTEM.OUT.PRINTLN (list);}
2. Remove elements from the specified index
/* * E Remove (int index) * removes the element on the specified index * returns the element that was deleted */public static void Function_1 () {list<double> List = new arraylist<double> (); List.add (1.1); List.add (1.2); List.add (1.3); List.add (1.4); list. Remove (1.1); Removes the specified element with no return value double D = list.remove (0); Removes the value of the specified index, with the return value (that is, the value removed) System.out.println (d); SYSTEM.OUT.PRINTLN (list);}
3. Replace the value on the specified index
public static void Function_2 () {list<integer> List = new arraylist<integer> (); List.add (1); List.add (2); List.add (3); List.add (4); Integer i = list. Set (0, 5);//Replace the value of 0 index with 5, the return value is the previous value System.out.println (i); SYSTEM.OUT.PRINTLN (list);}
4. The elements in the collection cannot be increased during the iteration
import java.util.arraylist;import java.util.iterator;import java.util.List;/ * * Iterator Concurrency Modification Exception java.util.ConcurrentModificationException * is in the process of traversal, using the collection method to modify the length of the collection, not allowed */public class ListDemo1 {Pub Lic static void Main (string[] args) {list<string> List = new arraylist<string> () List.add ("ABC1"); List.add ( "ABC2"); List.add ("ABC3"); List.add ("ABC4");//The collection uses iterators to get the time to determine if there is a "ABC3" object in the collection//If so, add an element "ABC3" iterator< String> it = List.iterator (), while (It.hasnext ()) {String s = it.next ();//to the obtained element s, to determine whether there is "ABC3" if (S.equals ("ABC3") ") {//string is not a basic data type, so you cannot use = = to judge List.add (" ABC3 ");} System.out.println (s);}}
the collection of class LinkedList
Its own characteristics: from the bottom of the list of the implementation, query slow, and delete quickly, because it is a unique function of subclasses, can not be called polymorphic
1. Add elements to the beginning and end of the list
/* * AddFirst (E) added to the beginning of the list * AddLast (E) added to the end of the list */public static void function () {linkedlist<string> link = n EW linkedlist<string> (); Link.addlast ("Xinge"); Link.add ("abc"); Link.add ("BCD"); Link.addfirst ("x5456"); System.out.println (link);}
2. Get the start and end values
/* * e GetFirst () Gets the beginning of the list * e GetLast () Gets the end of the list */public static void function_2 () {linkedlist<string> link = new link Edlist<string> (), Link.add ("1"), Link.add ("2"), Link.add ("3"), Link.add ("4"); Link.isempty ()) {//Determine if the collection of linked lists is empty, equivalent to Link.size ()!=0string first = Link.getfirst (); String last = Link.getlast (); SYSTEM.OUT.PRINTLN (first); System.out.println (last);}}
3. Remove the start and end values
/* * e Removefirst () Remove and return to the beginning of the list * e removelast () Remove and return the end of the list */public static void Function_3 () {linkedlist< string> link = new linkedlist<string> (), Link.add ("1"), Link.add ("2"), Link.add ("3"), Link.add ("4"); String first = Link.removefirst (); String last = Link.removelast (); SYSTEM.OUT.PRINTLN (first); System.out.println (last); System.out.println (link. Pop ()); Eject the element at the top of the collection stack}
set Interface
Collection can hold repeating elements, or do not hold duplicate elements, we know that the list can be stored in duplicate elements. So where do you store the elements without repeating them? That is the set interface, the set, the stored elements are not duplicated.
First, HashSet
The HashSet collection, which stores the data in a hash table structure , guarantees that the element uniqueness depends on: thehashcode () and the equals () method, The storage and removal are faster, the thread is unsafe, and the running speed is fast.
What is a hash table?
The bottom of the hash table is also the array mechanism, the array also holds the object, and these objects are stored in the array when the location of the special, when the need to put these objects into the array, then according to the specific data of these objects combined with the corresponding algorithm, the object in the array to calculate the position , The object is then stored in the array. And such an array is called a hash array, that is, a hash table.
When storing elements in a hash table, it is necessary to combine the corresponding algorithm according to the unique data of the element, which is actually the hashcode method in the Object class. Because any object is a subclass of the object class, any object has this method. That is, when the object is stored in the hash table, the object's hashcode method is called, and the location of the object in the table is calculated, and it is important to note that if the two object hashcode method Calculates the result, the phenomenon is called a hash conflict. The object's equals method is called, comparing whether the two objects are the same object, and if the equals method returns True, then the second object is not stored in the hash table, if the returned False, the value is stored in the hash table.
Summary: The only thing that guarantees the HashSet collection element is that it is determined by the hashcode of the object and the equals method. If we store a custom object in the collection, then it is guaranteed to be unique, and the hashcode and equals methods must be replicated to establish a comparison of the current object.
Example: Treat the name, age, and same data in the person object as the same object
Import Java.util.hashset;import cn.itcast.demo3.person;/* * HashSet set of features: * underlying data structure, hash table * storage, take out all relatively fast * thread insecure, fast */public class HashSetDemo1 {public static void main (string[] args) {//Judgment object is duplicated, dependent on the object's own method Hashcode, equalshashset<person> Setperson = new hashset<person> () Setperson.add (New person ("a", one)); Setperson.add (New person ("B", "ten)"), Setperson.add ("B"), Setperson.add (New person ("C", +)), Setperson.add (New person ( ("D", +)); Setperson.add (new Person ("E", 17)); System.out.println (Setperson);}}
Person.java
To meet the requirements, you need to rewrite the hashcode and equals methods of the person class
public class Person {private String name;private int age;/* * does not do the overriding parent class, each run result is a different integer * if the subclass overrides the method of the parent class, the hash value, the custom * The basis for storing the HashSet collection is */public int hashcode () {return name.hashcode () +age*55;} Method equals overrides the parent class, guaranteeing the same as the parent class//public Boolean equals (Object obj) {}public Boolean equals (Object obj) {if (this = = obj) return true if (obj = = null) return false;if (obj instanceof person) {person P = (person) Obj;return name.equals (p.name) && age== P.age;} return false;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public person (String name, int.) {super (); this.name = Name;this.age = age;} Public person () {}public String toString () {return name+ "..." +age;}}
Second, Linkedhashset
HashSet stores are unordered, linkedhashset which are stored in an orderly
public class Linkedhashsetdemo {public static void main (string[] args) {set<string> Set = new Linkedhashset<strin G> (), Set.add ("BBB"), Set.add ("AAA"), Set.add ("abc"); Set.add ("BBC") ; Iterator it = Set.iterator (); while (It.hasnext ()) {System.out.println (It.next ());}}}
determine The unique elements in the collection
ArrayList
The contains method of ArrayList uses the call method when the equals method of the passed-in element is compared to the old element in the collection, which determines whether there are duplicate elements based on the returned Boolean value. At this point, when ArrayList stores a custom type, because the custom type is not overriding the equals method, it is determined whether the duplicate is based on the address value, so if you want to determine whether it is a repeating element based on the content, you need to override the element's The Equals method.
HashSet
The set collection cannot hold duplicate elements, and the Add method will determine whether there are duplicate elements when added, which are not added or duplicated.
HashSet The collection is unordered, its judgment is only based on the hashcode of the element type and the return result of the equals method. The rules are as follows:
Determine the hashcode value of the new element and the old element already in the collection
If different, the description is different from the element that is added to the collection.
If the same, then the equals comparison result is judged. Returns true for the same element, or false to add a different element to the collection.
Therefore, using HashSet to store custom types, if you do not override the class's hashcode with the equals method, then when judging duplicates, you are using the address value, if you want to compare elements by content is the same, You need to override the hashcode of the element class with the equals method.
Java--link interface (arraylist,linklist) and set interface (HashSet)