In Java, you cannot create a list of primitive int directly because lists only support objects. To work with primitive integers, use their wrapper class Integer. Here’s how:
List
intList.add(10);
intList.add(20);
intList.add(30);
Java automatically converts between int and Integer using autoboxing. If you need better performance and want to avoid boxing, consider using IntStream from the java.util.stream package or TIntList from third-party libraries like Trove. Example with IntStream:
List
This allows you to handle primitive-like integers in lists.