Type of list in Java

Java Generics - List


Advertisements

Previous Page
Next Page

Java has provided generic support in List interface.

Syntax

List list = new ArrayList();

Where

  • list object of List interface.

  • T The generic type parameter passed during list declaration.

Description

The T is a type parameter passed to the generic interface List and its implemenation class ArrayList.

Example

Create the following java program using any editor of your choice.

Live Demo
package com.tutorialspoint; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class GenericsTester { public static void main(String[] args) { List integerList = new ArrayList(); integerList.add(Integer.valueOf(10)); integerList.add(Integer.valueOf(11)); List stringList = new ArrayList(); stringList.add("Hello World"); stringList.add("Hi World"); System.out.printf("Integer Value :%d\n", integerList.get(0)); System.out.printf("String Value :%s\n", stringList.get(0)); for(Integer data: integerList) { System.out.printf("Integer Value :%d\n", data); } Iterator stringIterator = stringList.iterator(); while(stringIterator.hasNext()) { System.out.printf("String Value :%s\n", stringIterator.next()); } } }

This will produce the following result

Output

Integer Value :10 String Value :Hello World Integer Value :10 Integer Value :11 String Value :Hello World String Value :Hi World

Useful Video Courses


Type of list in Java
Video

Java Date and Time Online Training

16 Lectures 2 hours

Malhar Lathkar

More Detail
Type of list in Java
Video

Java Servlet Online Training

19 Lectures 5 hours

Malhar Lathkar

More Detail
Type of list in Java
Video

JavaScript Online Training

25 Lectures 2.5 hours

Anadi Sharma

More Detail
Type of list in Java
Video

Java Online Training

Most Popular

126 Lectures 7 hours

Tushar Kale

More Detail
Type of list in Java
Video

Java Essential Training

119 Lectures 17.5 hours

Monica Mittal

More Detail
Type of list in Java
Video

Java Essentials Online Training

76 Lectures 7 hours

Arnab Chakraborty

More Detail
Previous Page Print Page
Next Page
Advertisements