49. Method specific to ArrayList LinkedList

Collection system:
--------------| Collection The root interface of a single-column collection

----------| List If a collection class that implements the List interface, the class has Features are: orderly, repeatable

------|ArrayList ArrayList maintains an Object array at the bottom. Features are: fast query, slow addition and
deletion , then use ArrayList to store this batch of data, such as: University reading library
Note: When using the ArrayList no-parameter constructor, the initial length of Object is 10, and when the length is not enough, it will automatically increase by 0.5 times

------|LinkedList LinkedList bottom layer Implemented using a linked list data structure. The characteristics are: slow query speed, fast addition and deletion.
Usage scenario: If the current data is added and deleted more and the query is less, then use LinkedList to store this batch of data

----------| Set If Set is implemented A few mouthfuls of collection classes, the features of this class are: unordered, non-repeatable ArrayList constructor: ArrayList() Constructs an empty list with an initial capacity of 10. ArrayList(Collection<? extends E> c) Constructs a list containing the elements of the specified collection, in the order in which the collection's iterator returns them. ArrayList(int initialCapacity) Constructs an empty list with the specified initial capacity. The following methods can be understood, because we generally use some methods in the List interface in actual development
 










The unique method in ArrayList (you can understand it):

ensureCapacity(int minCapacity) If necessary, increase the capacity of this ArrayList instance to ensure that it can accommodate at least the number of elements specified by the minimum capacity parameter.
We generally use the ArrayList constructor to specify Initial capacity
trimToSize() Adjusts the capacity of this ArrayList instance to the current size of the list.



A method unique to LinkedList (you can understand it):

    addFirst(E e) Inserts the specified element at the beginning of this list.
    addLast(E e) adds the specified element to the end of this list.
    getFirst() returns the first element of this list.
    getLast() returns the last element of this list.
    removeFirst() removes and returns the first element of this list.
    removeLast() removes and returns the last element of this list.     stack: FIFO     push() adds an element to the beginning of this collection     pop() removes the first element of the collection and returns to the     queue: FIFO     offer adds an element to the end of this element     poll removes the first element of the collection and returns     descendingIterator( ) returns an iterator object in reverse order
    



    



    

 

Here is an example of code to clear duplicate elements in a collection:

 

class Book{
    
    int id;
    String name;
    
    public Book(int id , String name) {
        this.id = id;
        this.name = name;
    }
    @Override
    public boolean equals(Object obj) {
        Book book = (Book)obj;
        return this.id == book.id;
    }
    @Override
    public String toString() {
        
        return "{ 书号:"+this.id+" 书名:"+this.name+" }";
    }
}
public class Demo2 {
    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add(new Book(1001, "java编程思想"));
        list.add(new Book(1002, "java核心技术"));
        list.add(new Book(1003, "javaweb"));
        list.add(new Book(1001, "java编程思想"));
        
        
        System.out.println(clearRepeat(list));
        LinkedList list1 = new LinkedList();
        list1.add("狗娃");
        list1.add("狗剩");

        System.out.println(list1.poll());
        System.out.println(list1);
    }
    
    public static ArrayList clearRepeat(ArrayList list) {
        ArrayList newlist = new ArrayList();
        Iterator it = list.iterator();
        while(it.hasNext()) {
            Book book = (Book)it.next();
            if(!newlist.contains(book)) {
                newlist.add(book);
            }
        }
        return newlist;
    }
}

 

Guess you like

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