Write a Java program to Create a LinkedList with different types of items


The Different_Types class contains a main method where a LinkedList object named dif_type is created without specifying any type parameter, allowing it to hold items of any type. Five items of different types, including an integer (18), a string ("Java"), a double (88.49), a character ('j'), and a boolean (false), are added to the dif_type list using the add method.

The System.out.println statement is used to print the message "LinkedList with different types of items : " along with the content of the dif_type list using the toString method, which will display the items in the order they were added to the list.

It's worth noting that using a LinkedList or any other generic collection without specifying a type parameter is not recommended in Java, as it can lead to runtime type errors and make the code harder to maintain. It's generally better practice to use generic collections with a specific type parameter that matches the type of items you intend to store in the list. For example, LinkedList<Integer> for integers, LinkedList<String> for strings, etc.

Source Code

import java.util.LinkedList;
public class Different_Types
{
	public static void main(String[] args)
	{
		LinkedList dif_type = new LinkedList();
		dif_type.add(18);
		dif_type.add("Java");
		dif_type.add(88.49);
		dif_type.add('j');
		dif_type.add(false);
 
		System.out.println("LinkedList with different types of items : " + dif_type);
	}
}

Output

LinkedList with different types of items : [18, Java, 88.49, j, false]

Example Programs