Channi Studies

[Java] Generic Types of List 본문

Data Science/Data Structure & Algorithm

[Java] Generic Types of List

Chan Lee 2025. 8. 31. 04:46

To create an ArrayList (or LinkedList) in Java, you can do as following:

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List myList = new ArrayList();

        myList.add(10);
        myList.add("Go");
        myList.add(false);

        System.out.println(myList); // [10, Go, false]
    }
}

If we do not specify the type of the elements in the List, we can put different types in the List like we did in Python. 

 

Since we can put whatever we want, possible runtime errors can occur dealing with types. 

However, there are multiple reasons why we use generics specified within angle brackets (or diamond operator, <>)

 

One problem that might arise in such situation is incompatible type error when assigning an element to 

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List myList = new ArrayList();

        myList.add(10);
        myList.add("Go");
        myList.add(false);

        int firstNum = myList.get(0); // Compile Error
        // java: incompatible types: java.lang.Object cannot be converted to int
    }
}

This error occurs same for "Go" and false. Why does this happen? 

 

 

It happens because we didn't specified generic type of the List, resulting EVERY elements getting returned to have Object (java.util.Object) type, which is the superclass of every class in Java. 

In other words, ANY classes that you define automatically inherits from java.util.Object class. 

Since the generic type hasn't provided, Java returns every object in Object type.

 

Although specifying type is safer, we can still use the code if we add type casting. 

Object type will be converted to our desired types with appropriate type casting. 

For example:

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List myList = new ArrayList();

        myList.add(10);
        myList.add("Go");
        myList.add(false);

        int firstNum = (Integer) myList.get(0);
        String secondStr = (String) myList.get(1);
        boolean thirdBool = (boolean) myList.get(2);

        System.out.println(firstNum + ", " + secondStr + ", " + thirdBool);
    }
}

 

Now, there is another weird problem when you try to make a List of integers. (with generics)

My compiler shows "Type argument cannot be of a primitive typeon integer. 

 

Like other programming languages, Java have some primitive types–byte, short, int, long, float, double, boolean, and char.

Those primitive types are NOT objects, while generic system requires objects. So you must only put objects inside the diamond operator (<>).

 

Then, what should we do in such situation? There are something called wrapper class corresponding to each data type. 

Ex) int → Integer, double → Double, boolean → Boolean, etc

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Integer> myList = new ArrayList<>();
        List<Double> secondList = new LinkedList<>();

        myList.add(1);
        myList.add(2);
        myList.add(3);
        System.out.println(myList);

        secondList.add(2.0);
        secondList.add(2.5);
        secondList.add(-3.141592);
        System.out.println(secondList);
    }
}

Result