Java self study - collection framework Generic

Using generics on ArrayList

Step 1: Generic

Do not specify a generic container, you can store any type of element
A container with a generic type specified can only hold elements of the specified type and their subclasses

package property;
 
public class Item {
    String name;
    int price;
     
    public Item(){
         
    }
     
    //Provide a construction method to initialize name
    public Item(String name){
        this.name = name;
    }
     
    public void effect(){
        System.out.println("Items can have effect after use");
    }
     
}
package collection;
   
import java.util.ArrayList;
import java.util.List;
  
import property.Item;
import charactor.APHero;
import charactor.Hero;
   
public class TestCollection {
  
    public static void main(String[] args) {
          
        //For containers without generics, you can put heroes or items in them
        List heros = new ArrayList();
          
        heros.add(new Hero("Galen"));
          
        //The container used to store heroes can also store items now
        heros.add(new Item("Ice stick"));
          
        //Problems in object transformation
        Hero h1=  (Hero) heros.get(0);
        //Especially when there are too many objects in the container, it's hard to remember which type of objects are placed in which position
        Hero h2=  (Hero) heros.get(1);
          
        //Introducing Generic
        //When declaring a container, this kind of container is specified. Only Hero can be placed, and other containers will make mistakes
        List<Hero> genericheros = new ArrayList<Hero>();
        genericheros.add(new Hero("Galen"));
        //If it's not a Hero, it's not going to fit in
        //genericheros.add(new Item("ice stick"));
          
        //In addition, it can also store the subclasses of Hero
        genericheros.add(new APHero());
         
        //And when fetching the data, there is no need for transformation, because it must be the Hero or its subclass
        Hero h = genericheros.get(0);
         
    }
       
}

Step 2: shorthand for generics

To avoid warnings from the compiler, you need to use generics before and after, like this:

List<Hero> genericheros = new ArrayList<Hero>();

However, JDK7 provides a generic shorthand that can slightly reduce the amount of code

List<Hero> genericheros2 = new ArrayList<>();

Later generics can be replaced with < >

package collection;
   
import java.util.ArrayList;
import java.util.List;
 
import charactor.Hero;
   
public class TestCollection {
  
    public static void main(String[] args) {
        List<Hero> genericheros = new ArrayList<Hero>();
        List<Hero> genericheros2 = new ArrayList<>();
      
    }
       
}

Practice: Support for generic ArrayList

With the help of generics and the object-oriented knowledge learned earlier, an ArrayList is designed so that both Hero and Item can be placed in the ArrayList, but no other object can be placed except these two objects

Answer:

First, create an interface LOL without declaring any methods in it
Then let Hero and Item implement the LOL interface
Finally, declare a variable lolList of ArrayList, whose generic type is

List<LOL> lolList = new ArrayList<>();

In this way, the Hero object and the Item object are placed in the lolList.

Keywords: Java

Added by xAtlas on Fri, 08 Nov 2019 16:09:10 +0200