De duplication of generics, ArrayList, LinkedList and LinkedList

catalogue

generic paradigm

Generic mechanism

Characteristics of generics

Generic class

generic interface

ArrayList

ArrayList stores strings and iterates through them

ArrayList stores custom objects and traverses them

Unique functions of Vector

Implementation of the unique function of Vector

Unique features of LinkedList

Implementation of the unique function of LinkedList

How to remove duplicate string elements in ArrayList

Idea 1: create a new collection method

Idea 2: operate in the original set without creating a new set

Idea 3: use LinkedHashSet set

generic paradigm

Generic mechanism

Generic mechanism is a mechanism that postpones the explicit work of data types until the creation of objects or the invocation of methods.

For example, when creating a collection object, if you ignore generics, write Object obj = new ArrayList(); At this time, the data type of the set is still not specified, so the created set can store elements of any data type (of course, only objects can be stored in the set). If a string object "hello" is stored at this time, we must carry out downward transformation if we want to use the unique method of string type, as follows:

 import java.util.ArrayList;
 ​
 public class Test4 {
     public static void main(String[] args) {
         ArrayList list = new ArrayList();
         list.add(new java.lang.String("hello"));
         Object o = list.get(0);
         String s= (String) o;
         int i = s.compareTo("hellow");
         System.out.println(i);
     }
 }

However, if we specify the data type directly when creating the collection, the data type of the elements of the collection is the data type you specify. There will be less trouble. as follows

 import java.util.ArrayList;
 ​
 public class Test5 {
     public static void main(String[] args) {
         ArrayList<String> list = new ArrayList<>();
         list.add(new String("hello"));
         int i = list.get(0).compareTo("hellow");
         System.out.println(i);
     }
 }

Characteristics of generics

1) Valid only at compile time and erased at run time;

2) The expansibility of the code is improved;

3) Generics are in jdk1 Introduced after 5.

4) Avoid forced type conversion;

5) The program design is optimized and the yellow warning line is solved.

Generic class

Generics are defined on classes, which become generic classes, such as

 public class MyDemo<X> {
     public void myDemo(){}
 }

generic interface

Generics can also be defined on interfaces, which are called generic interfaces

 public interface MyInterface<X, Y> {
     void show(X x, Y y);
 }

When an implementation class implements this interface, the specific type of generic type can be specified.

 public class Test implements Inter<String,Integer>{
 ​
     @Override
     public void show(String s, Integer integer) {
         ;
     }
 }

Of course, you can also implement the interface without specifying the specific data type

 public class Test2<X, Y> implements Inter<X,Y> {
 ​
     @Override
     public void show(X x, Y y) {
         
     }
 }

ArrayList

ArrayList stores strings and iterates through them

 import java.util.ArrayList;
 import java.util.Iterator;
 ​
 public class Test4 {
     public static void main(String[] args) {
         ArrayList<String> list = new ArrayList<>();
         list.add("Alice");
         list.add("Helen");
         list.add("Jack");
         //Iterator traversal
         Iterator<String> iterator = list.iterator();
         while (iterator.hasNext()) {
             String s = iterator.next();
             System.out.println(s);
         }
         //Ordinary for traversal
         for (int i = 0; i < list.size(); i++) {
             System.out.println(list.get(i));
         }
     }
 }

ArrayList stores custom objects and traverses them

import java.util.ArrayList;
 import java.util.Iterator;
 ​
 public class Test3 {
     public static void main(String[] args) {
         ArrayList<Student> list = new ArrayList<>();
         list.add(new Student("Alice",18));
         list.add(new Student("Jack",19));
         list.add(new Student("Helen",18));
         //Iterator traversal
         Iterator<Student> iterator = list.iterator();
         while (iterator.hasNext()) {
             Student next = iterator.next();
             System.out.println(next);
         }
         //Ordinary for traversal
         for (int i = 0; i < list.size(); i++) {
             System.out.println(list.get(i));
         }
 ​
     }
 }

Unique functions of Vector

 public void addElement(E obj)
/ / add the specified element to the end of this vector and increase its size by 1. If the size of the vector is larger than its capacity, increase its capacity.
     
 public E elementAt(int index)
/ / returns the element at the specified index.
 ​
 public Enumeration elements()
/ / returns the Enumeration of the components of this vector. The Enumeration object returned will generate all items in this vector. The first item generated is the item at index 0, then the item at index 1, and so on. 
     
 E firstElement()
/ / returns the first component of this vector (the item at index 0)
     
 E lastElement()
/ / return the last component of this vector. 
     
 void insertElementAt(E obj, int index)
/ / insert the specified object into the specified index as a component of this vector. 

Implementation of the unique function of Vector

 import java.util.Vector;
 ​
 public class Test5 {
     public static void main(String[] args) {
         Vector<String> vector = new Vector<>();
         vector.add("Alice");
         vector.add("Helen");
         //Adds an element to the end of the collection
         vector.addElement("Jack");
         //Returns the element at the index
         String s = vector.elementAt(0);
         //Returns the first element
         String firstElement = vector.firstElement();
         //Returns the last element
         String lastElement = vector.lastElement();
         //Inserts an element at the specified index
         vector.insertElementAt("Insert",1);
     }
 }

Unique features of LinkedList

/ / insert the specified element at the beginning of this list
 public void addFirst(E e)
 ​
/ / add the specified element to the end of this list.
 public void addLast(E e)
 ​
/ / return the first element of this list.
 public E getFirst()
 ​
/ / return the last element of this list. 
 public E getLast()
 ​
/ / remove and return the first element of this list. 
 public E removeFirst()
 ​
/ / remove and return the last element of this list. 
 public E removeLast()

Implementation of the unique function of LinkedList

 import java.util.LinkedList;
 ​
 public class Test6 {
     public static void main(String[] args) {
         LinkedList<String> list = new LinkedList<>();
         //Inserts the specified element at the beginning of this list
         list.addFirst("addFirst");
         //Adds the specified element to the end of this list.
         list.addLast("addLast");
         //Returns the first element of this list.
         String first = list.getFirst();
         //Returns the last element of this list.
         String last = list.getLast();
         //Removes and returns the first element of this list.
         String removeFirst = list.removeFirst();
         //Removes and returns the last element of this list.
         String removeLast = list.removeLast();
     }
 }

How to remove duplicate string elements in ArrayList

Requirement: ArrayList removes the duplicate values of strings in the collection (the contents of strings are the same)

Idea 1: create a new collection method

 import java.util.ArrayList;
 import java.util.Iterator;
 ​
 public class Test7 {
     public static void main(String[] args) {
         //ArrayList removes duplicate values of strings in the collection (the contents of strings are the same)
         //How to create a new collection
         ArrayList<Integer> list = new ArrayList<>();
         list.add(200);
         list.add(400);
         list.add(1);
         list.add(1);
         list.add(900);
         list.add(200);
         list.add(600);
         list.add(2);
         list.add(200);
         list.add(2);
         list.add(50);
 ​
         ArrayList<Integer> newList = new ArrayList<>();
         newList.add(list.get(0));
         Iterator<Integer> iterator = list.iterator();
         while (iterator.hasNext()) {
             Integer next = iterator.next();
             if(!newList.contains(next)){
                 newList.add(next);
             }
         }
         System.out.println(newList);
     }
 }

Idea 2: operate in the original set without creating a new set

import java.util.ArrayList;

public class Test6 {
    public static void main(String[] args) {
        //ArrayList removes duplicate values of strings in the collection (the contents of strings are the same)
        ArrayList<Integer> list = new ArrayList<>();
        list.add(200);
        list.add(400);
        list.add(1);
        list.add(1);
        list.add(900);
        list.add(200);
        list.add(600);
        list.add(2);
        list.add(200);
        list.add(2);
        list.add(50);

        for (int i = 0; i < list.size()-1; i++) {
            for (int j = i+1; j < list.size(); j++) {
                if(list.get(i).equals(list.get(j))){
                    list.remove(j);
                    j--;
                }
            }
        }
        System.out.println(list);
    }
}

Idea 3: use LinkedHashSet set

 import java.util.ArrayList;
 import java.util.LinkedHashSet;
 ​
 public class Test5 {
     public static void main(String[] args) {
         //ArrayList removes duplicate values of strings in the collection (the contents of strings are the same)
         //Using the LinkedHashSet set,
         ArrayList<Integer> list = new ArrayList<>();
         list.add(200);
         list.add(400);
         list.add(1);
         list.add(1);
         list.add(900);
         list.add(200);
         list.add(600);
         list.add(2);
         list.add(200);
         list.add(2);
         list.add(50);
         //Use a parameterized construct to create a LinkedHashSet object, and pass in list as a parameter
         LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<>(list);
         System.out.println(linkedHashSet);
     }
 }

Keywords: Java Network Protocol linq p2p

Added by stenk on Sun, 20 Feb 2022 06:48:47 +0200