Notes - Advanced Java Foundation to Face Difficulties 1

Collection set

The length of the array is fixed, and the length of the set is variable.

Arrays store elements of the same type, which can store basic data type values. Collections store objects. And the types of objects can be inconsistent.

Set framework

import java.util.Collection;
import java.util.ArrayList;
//Common methods of Collection
public class CollectionDemo{
    public static void main(String[] args){
    //Collection is an interface that cannot create objects and can use polymorphism to use methods
    Collection<String> collection = new ArrayList<>();
    //Add method to add elements to the collection
    collection.add("Li Bai");
    //collection.add(String); you cannot add an unspecified type.
    collection.add("Su Shi");
    collection.add("Du Fu");
    collection.add("Wang Anshi");
    collection.add("Li Qingzhao");
    //remove method, the return value type is boolean, deleting the given object
    //collection.remove("Lin Daiyu");
    //System.out.println(aremove); if not, return false
    collection.remove("Li Qingzhao");
    //Determine whether a given object is included in the current collection and the return value type is boolean
    boolean acontains = collection.contains("Li Qingzhao");
    //size, returns the number of elements in the collection
    int asize = collection.size();
    //isEmpty, to determine whether the collection is empty, the collection is empty, returns true, not empty, returns false
    boolean aisEmpty = collection.isEmpty();
    //toArray, which stores elements in a collection into an array with the return value type Object [] array
    Object[] atoArray = collection.toArray();
    for(int i=0;i<atoArray.length;i++){
    //Clean the array clear


Iterator interface

The Iterator interface has two common methods

boolean hasNext() If there are still elements to iterate over, return true to determine if there is another element in the set.

E next() returns the next element of the iteration

Remove the next element in the collection

Iterator iterator is an interface that can not be used directly. Obtaining the Iterator interface to implement the object of the class can be achieved through the method iterator () in the Collection interface; this method returns the implementation class object of the iterator.

The steps of using the iterator are as follows:

1. Use iterator() in Collection set to get the implementation class object of iterator, and use Iterator interface to receive it.

2. Use the method hasNext in the Iterator interface to determine if there is another element

3. Use the method next in the Iterator interface to extract the next element of the collection

import java.util.*;
public class IteratorDemo{
    public static void main(String[] args){
    //You can use polymorphism to use this method
    Collection<String> collection = new ArrayList<>();
    collection.add("Li Shangyin");
    collection.add("Li Qingzhao");
    collection.add("Wang Anshi");
    collection.add("Su Shi");
    collection.add("Bai Juyi");
    //Use iterators to extract elements from a collection
    //To get an implementation class of Iterator first, you can use the method iterator to get it.
    Iterator<String> aIterator = collection.iterator();
    //Determine whether a collection has elements
    String str =;

Enhanced for cycle

Collection interface inherits Iterable interface, which is a new feature after JDK 1.5.


For (collection variable name: collection name) {}

import java.util.*;
public class ForeachDemo{
    public static void main(String[] args){
    ArrayList<String> arraylist = new ArrayList<>();
    arraylist.add("Clouds think of clothes and flowers. ");
    arraylist.add("Spring breeze blows the sill and luxuriant. ");
    arraylist.add("If I hadn't seen you at the top of the mountain,");
    arraylist.add("I will meet Yaotai next month. ");
    for(String a : arraylist){

generic paradigm

Generics are unknown data types

Define classes with generics

public class GenericDemo<E>{
    private E e;
    public void setName(E e){
        this.e = e;
    public E getName(){
        return e;
public class GenericDemoa{
    public static void main(String[] args){
    GenericDemo<String> gd = new GenericDemo<>();
    gd.setName("Li Bai");
    String str = gd.getName();
//Define methods with generics
public class GenericDemob{
    public <E> void method1(E e){
    public static <E> void method2(E e){
public class GenericDemoc{
    public static void main(String[] args){
    GenericDemo1 gd1 = new GenericDemo1();
    gd1.method1("Clouds think of clothes and flowers");
    GenericDemo1.method2("Spring breeze blows the sill and luxuriant");

Generic wildcards

public static void main(String[] args){
     Collection<Integer> list1 = new ArrayList<Integer>();
     Collection<String> list2 = new ArrayList<String>();
     Collection<Number> list3 = new ArrayList<>(Number);
     Collection<Object> list4 = new ArrayList<Object>();
    Inheritance relationship between classes Integer extends Number extends Object
    String extends Objects
    getElement1(list1);//Compilation passes and is a subclass of Number
    getElement1(list2);//Error reporting, not a subclass of Number
    getElement1(list3);//Compilation passes, and it's Number itself.
    getElement1(list4);//Error reporting, not a subclass of Number
    getElement2(list1);//Report errors
    getElement2(list2);//Report errors
    //The upper limit of a generic: this generic? Must be a subclass of Number type or Number type
public static void getElement1(Collection<? extends Number> coll){}
   //Lower Limit of Generics: This Generic? Must be a parent of Number type or Number type 
public static void getElenment2(Collection<? super Number> coll)

I made a game of fighting against landlords. This case analysis was done by watching videos. After watching the analysis, I wrote the code myself. It was good or bad. I thought about it for a long time. I wrote it for 2 hours, and finally finished it.

package demo12;

import java.util.ArrayList;
//Make a landlord game. There are three players and one card.
public class doudizhu {
    public static void main(String[] args) {
        //Create a set of players, including the bottom cards
        Players aplayers = new Players();
        //This collection is used to hold 54 cards.
        ArrayList<String> pokerArray = new ArrayList<>();
        //This set is used to decorate the color of the card. When it is ready, it should be combined with the number of the card.
        ArrayList<String> color = new ArrayList<String>();
        //This collection is used to hold the number of playing cards.
        ArrayList<String> number = new ArrayList<String>();

        //Generate poker cards
        for (int i = 2; i < 13; i++) {
            int j = i + 1;
            String str = j + "";
        for (int i = 0; i < color.size(); i++) {
            for (int j = 0; j < number.size(); j++) {
                String str1 = color.get(i) + number.get(j);
        //Add the King and King to the poker Array collection, and the cards are generated here.
        pokerArray.add(0, "king");
        pokerArray.add(1, "Xiao Wang");
        //Transitive set;

import java.util.ArrayList;
import java.util.Collections;

public class Players {
    public void play(ArrayList<String> arrayList) {
        //Disorder incoming collections
        //Collection of players
        ArrayList<String> player1 = new ArrayList<String>();
        ArrayList<String> player2 = new ArrayList<String>();
        ArrayList<String> player3 = new ArrayList<String>();
        ArrayList<String> player4 = new ArrayList<String>();
        for(int i=0;i<arrayList.size();i++) {
            //Why subtract three, because three cards are added to the deck, and the cards with index 51, 52, 53 are added to the deck?
                String p1=arrayList.get(i);
            }else if(i%3==1&&i<arrayList.size()-2){
                String p2 =arrayList.get(i);
            }else if(i%3==2&&i<arrayList.size()-2){
                String p3 = arrayList.get(i);
                String p4 = arrayList.get(i);
        //Display board

Data organization

Stack: First in, last out, with only one entrance and exit

Queue: First in, first out, one entrance, one exit

Array: The address in the array is continuous and can be queried directly according to the index value. The query is fast and the increase or deletion is slow.

Link list: slow query: the address in the list is not continuous, and every query must start from scratch.

Increase or delete faster: the structure of the linked list, add or delete an element, the overall structure of the linked list has no impact, so increase or delete faster.

Red and Black Trees: Quick Query

List set


import java.util.*;
//List collection inherits from Collection interface
//List set is an ordered set, which is stored in 1, 2, 3 and taken out in 1, 2, 3.
//List collections can store duplicate elements
//List collection is an indexed collection
public class ListDemo{
    public static void main(String[] args){
    //Take a look at the common methods of List collections
    //List collection is an interface that needs to use polymorphism to invoke its methods
    List<String> aList = new ArrayList<>();
    aList.add("Clouds think of clothes and flowers, ");
    aList.add(" Spring breeze blows the sill and luxuriant.");
    aList.add("If I hadn't seen you at the top of the mountain, ");
    //Change the specified element, add it, and change it to the specified location in the collection
    aList.add(3,"I will meet Yaotai next month. ");
    //Returns the element at the specified location in the collection
    String str1 = aList.get(0);
    //Remove the element in the list at the specified location and return the removed element
    String str2 = aList.remove(1);
    //Replace elements in the set with specified elements at specified locations, returning elements before updating values
    String str3 = aList.set(1,"Spring breeze does not pass through Yumen Gate");
    System.out.println(aList);//[Clouds think of clothes and flowers, spring breeze does not pass through Yumen Gate, will meet Yao Tai on the next moon. ]
    //Ergodic set
    for(int i = 0;i<aList.size();i++){
        String str = aList.get(i);
    //The iterator method of the iterator List collection returns an implementation class of the Iterator interface
    Iterator<String> iterator = aList.iterator();
    //Judge first whether there is the next element.
        String str5 =;
    for(String s : aList){


Subclasses of List Sets

ArrayList Collection: Elements add and delete slowly, find quickly, see for querying data, traverse data

LinkedList collection:

Common Methods of LinkedList Sets

AddFirst (E); inserts the specified element at the beginning of the list

AddLast (E); adds the specified element to the end of the list

push (E); push elements into the stack represented by the sub-list, equal to addFirst

getFirst(); returns the first element of the next list

getLast(); returns the last element of this list

removeFirst(); removes the first element of this list

removeLast (); removes the last element of this list

pop(); equivalent to removeFirst

isEmpty(); Determines whether a collection contains elements, returns false if there is one, and true if there is none.

Keywords: Java Spring P4 JDK

Added by EvilCoatHanger on Tue, 21 May 2019 22:21:43 +0300