Analysis of Java Set and LIst Interface

  • 2021-07-06 11:05:24
  • OfStack

1. The concept of sets

1. Overview:

Before learning the collection, recall one characteristic of array under 1-array has a fixed length, and define an array: int [] array = new int []; In view of the variable data length, a set is generated. java set is generated to cope with the dynamic growth of data, and the specific data amount cannot be known at compile time.
Collection classes are also called container classes.

2. Differences between collections and arrays

They are all containers, arrays are fixed in length, and collections are variable; The data stored in arrays are all basic data types (4 categories and 8 types), and the data stored in collections are all reference data types (String, Integer, custom data types) Bit reference data types are converted for basic data types in the collection and then stored.

3. The collection contains content, the framework of the collection

Interfaces: Collection, Map, Set, List, etc. (where Set and List inherit Collection) Abstract classes: AbstractCollection, AbstractList, etc. (some methods are implemented) Implementation classes: ArrayList, LinkedList, HashMap, etc Iterator: Iterator (access iteration of collection, iterator that returns elements in collection)

2. List Set

1. Overview

The List set is an ordered and repeatable set, and every element in the set has a corresponding sequential index.

List allows duplicates to be added to elements that should be indexed to the specified location.

By default, the List collection increases the index of elements in the order in which they are added.

2.ArrayList

1 > Overview

ArrayList is an array-based List class that implements all optional list operations and allows all elements to include null

2 > Initialization

ArrayList arrayList = new ArrayList(); = > List collection with initial capacity of 10

ArrayList < E > arrayList = new ArrayList < E > (); = > The data type is E, and the initial capacity is 10

3 > Main methods

boolean add(E e) -- > Appends the specified element to the end of this list.

void add(int index, E element) -- > Inserts the specified element at the specified position in this list.

boolean addAll(Collection < ? extends E > c) -- > Appends all elements in the specified collection to the end of this list in the order returned by Iterator for the specified collection.

boolean addAll(int index, Collection < ? extends E > c) -- > Inserts all elements from the specified collection into this list, starting at the specified position.

boolean contains(Object o) -- > If this list contains the specified elements, true is returned.

E get(int index) -- > Returns the element at the specified position in this list.

E remove(int index) -- > Deletes the element at the specified position in the list.

E set(int index, E element) -- > Replaces the element at the specified position in this list with the specified element.

Object[] toArray() -- > Returns an array containing all the elements in this list in the correct order (from the first element to the last one).


/**
 * @ author: PrincessHug
 * @ date: 2019/2/10, 0:18
 * @ Blog: https://www.cnblogs.com/HelloBigTable/
 */
public class ArrayListDemo01 {
 public static void main(String[] args) {
 ArrayList<String> arr = new ArrayList<String>();
 arr.add("123");
 System.out.println(arr);
 ArrayList<Person> person = new ArrayList<Person>();
 Person p1 = new Person("Wyh",18);
 Person p2 = new Person("Hunter", 40);
 person.add(p1);
 person.add(p2);
 for (int i=0;i<person.size();i++) {
 System.out.println(person.get(i));
 }
 System.out.println(person.contains(p2));
 person.remove(1);
 person.set(0,p2);
 Person[] persons = new Person[2];
 person.toArray(persons);
 System.out.println(persons[0]);
 System.out.println(persons[1]);
 }
}
 
public class Person {
 private String name;
 private int age;
 
 public Person(){}
 
 public Person(String name, int age) {
 this.name = name;
 this.age = age;
 }
 
 public String getName() {
 return name;
 }
 
 public void setName(String name) {
 this.name = name;
 }
 
 public int getAge() {
 return age;
 }
 
 public void setAge(int age) {
 this.age = age;
 }
 
 @Override
 public String toString() {
 return "["+this.name+","+this.age+"]";
 }
}

3. Four methods of traversal of 3. List set

The List. size () method is used as a condition for the for loop, which is the same as the array traversal Through iterator Iterator Iterator < Integer > it = arrayList. iterator (); while loops, hasNext is used as a judgment condition, and next () obtains the set elements and outputs them. Enhanced for Loop jdk1.8 New Feature foreach

/**
 * @ author: PrincessHug
 * @ date: 2019/2/12, 2:43
 * @ Blog: https://www.cnblogs.com/HelloBigTable/
 */
public class PrintArrayList {
  public static void main(String[] args) {
    ArrayList<Student> students = new ArrayList<>();
    Student s1 = new Student("001", "Wyh", ' Male ');
    Student s2 = new Student("002", "Fyh", ' Male ');
    Student s3 = new Student("003", "Zxy", ' Male ');
    students.add(s1);
    students.add(s2);
    students.add(s3);
 
    System.out.println(" Pass size() Method acts as a for Loop conditional traversal: ");
    for (int i=0;i<students.size();i++){
      System.out.println(students.get(i));
    }
 
    System.out.println(" Traversing the collection through iterators: ");
    Iterator<Student> iterator = students.iterator();
    while (iterator.hasNext()){
      System.out.print(iterator.next() + "\t");
    }
 
    System.out.println(" By enhancing for Loop through the collection: ");
    for (Student stu:students){
      System.out.println(stu);
    }
    System.out.println(" Pass jdk1.8 New features forEach Traversing the collection: ");
    students.forEach(student -> System.out.println(student));
  }
}

4.LinkedList

1. Overview: LinkedList refers to the data structure of the linked list class

2. Differences between LinkedList and ArrayList:

a) Linked list elements can be arbitrarily added and deleted, but the query efficiency is not as good as list
b) Linked lists store objects in separate spaces, and each stewardess saves an index of the next link
Construction method LinkedList < E > linkedList = new LinkedList < E > ();

3. Main methods

void addFirst(E e) -- > Inserts the specified element at the beginning of the list.
void addLast(E e) -- > Appends the specified element to the end of this list.
E peekFirst() -- > Retrieves but does not delete the first element of this list, and returns null if this list is empty.
E peekLast() -- > Retrieves but does not delete the last 1 elements of this list, and returns null if this list is empty.
E pollFirst() -- > Retrieves and deletes the first element of this list, and returns null if this list is empty.
E pop() -- > The first element pops up from the stack represented by this list. Similar to removeFirst ()
void push(E e) -- > Pushes elements onto the stack represented by this list. Similar to addFirst ()


/**
 * @ author: PrincessHug
 * @ date: 2019/2/10, 2:12
 * @ Blog: https://www.cnblogs.com/HelloBigTable/
 */
public class LinkedListDemo {
 public static void main(String[] args) {
 LinkedList<String> linkedList = new LinkedList<>();
 linkedList.addFirst("is");
 linkedList.addFirst("Wyh");
 linkedList.addLast("cool");
 System.out.println(linkedList);
 System.out.println(linkedList.peekFirst());
 System.out.println(linkedList.pollFirst());
 System.out.println(linkedList);
 System.out.println(linkedList.pop());
 System.out.println(linkedList);
 linkedList.push("Wyh is");
 System.out.println(linkedList);
 }
}

Related articles: