xxxxxxxxxx
// ArrayList implementation of List
List<String> list1 = new ArrayList<>();
// LinkedList implementation of List
List<String> list2 = new LinkedList<>();
xxxxxxxxxx
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
class scratch{
public static void main(String[] args) {
List<Integer> aList = new ArrayList<>();
List<Integer> lList = new LinkedList<>();
}
}
xxxxxxxxxx
import java.util.*;
public class ListExample1{
public static void main(String args[]){
//Creating a List
List<String> list=new ArrayList<String>();
//Adding elements in the List
list.add("Mango");
list.add("Apple");
list.add("Banana");
list.add("Grapes");
//Iterating the List element using for-each loop
for(String fruit:list)
System.out.println(fruit);
}
}
xxxxxxxxxx
import java.util.List;
public class ListExample {
public static void main(String[] args) {
// Creating a List of Strings
List<String> myList = new ArrayList<>();
// Adding elements to the list
myList.add("Apple");
myList.add("Banana");
myList.add("Orange");
// Accessing elements in the list
String firstElement = myList.get(0);
System.out.println("First element: " + firstElement);
// Updating an element in the list
myList.set(1, "Mango");
// Removing an element from the list
boolean removed = myList.remove("Orange");
System.out.println("Element 'Orange' removed: " + removed);
// Getting the size of the list
int size = myList.size();
System.out.println("Size of the list: " + size);
// Iterating over the elements in the list
for (String element : myList) {
System.out.println(element);
}
}
}
xxxxxxxxxx
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// Create a new list using ArrayList
List<String> myList = new ArrayList<>();
// Add elements to the list
myList.add("First element");
myList.add("Second element");
myList.add("Third element");
// Access and print elements of the list
for (String element : myList) {
System.out.println(element);
}
}
}
xxxxxxxxxx
import java.util.*;
var list = new ArrayList<String>();
list.add("Hello World!");
xxxxxxxxxx
// Java Program to illustrate the
// addition of elements in a List
import java.util.*;
public class GFG {
public static void main(String args[])
{
// Creating a List
List<String> al = new ArrayList<>();
// Adding elements in the List
al.add("mango");
al.add("orange");
al.add("Grapes");
// Iterating the List
// element using for-each loop
for (String fruit : al)
System.out.println(fruit);
}
}
xxxxxxxxxx
import java.util.*;
public class CollectionExample {
public static void main(String[] args) {
// Set Example
Set<String> setExample = new HashSet<>();
setExample.add("Apple");
setExample.add("Banana");
setExample.add("Orange");
// iterating through set
Iterator<String> setIterator = setExample.iterator();
while (setIterator.hasNext()) {
System.out.println(setIterator.next());
}
// Alternative iteration
for (String item : setExample) {
System.out.println(item);
}
// List Example
List<Integer> listExample = new ArrayList<>();
listExample.add(10);
listExample.add(20);
listExample.add(30);
// iterating through List
Iterator<Integer> listIterator = listExample.iterator();
while (listIterator.hasNext()) {
System.out.println(listIterator.next());
}
// Alternative iteration
for (Integer value : listExample) {
System.out.println(value);
}
// Map Example
Map<String, Integer> mapExample = new HashMap<>();
mapExample.put("One", 1);
mapExample.put("Two", 2);
mapExample.put("Three", 3);
// iterating through Map
Iterator<Map.Entry<String, Integer>> mapIterator = mapExample.entrySet().iterator();
while (mapIterator.hasNext()) {
Map.Entry<String, Integer> entry = mapIterator.next();
System.out.println(entry.getKey() + ": " + entry.getValue());
}
// Alternative iteration
for (Map.Entry<String, Integer> entry : mapExample.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
xxxxxxxxxx
// Java code to demonstrate the concept of
// list of lists using iterator
import java.util.*;
class List_of_list {
// Iterate the 2D list using Iterator
// and print each element
public static <K> void
iterateUsingIterator(List<List<K> > listOfLists)
{
// Iterator for the 2D list
Iterator listOfListsIterator
= listOfLists.iterator();
System.out.println("[");
while (listOfListsIterator.hasNext()) {
// Type cast next() method
// to convert from Object to List<K>
List<K> list = new ArrayList<K>();
list = (List<K>)listOfListsIterator.next();
// Iterator for list
Iterator eachListIterator
= list.iterator();
System.out.print(" [");
while (eachListIterator.hasNext()) {
System.out.print(
" "
+ eachListIterator.next()
+ ", ");
}
System.out.println("], ");
}
System.out.println("]");
}
// Driver code
public static void main(String[] args)
{
// List of Lists
ArrayList<List<Integer> > listOfLists
= new ArrayList<List<Integer> >();
// Create N lists one by one
// and append to the list of lists
List<Integer> list1
= new ArrayList<Integer>();
list1.add(5);
list1.add(10);
listOfLists.add(list1);
List<Integer> list2
= new ArrayList<Integer>();
list2.add(1);
listOfLists.add(list2);
List<Integer> list3
= new ArrayList<Integer>();
list3.add(20);
list3.add(30);
list3.add(40);
listOfLists.add(list3);
// Iterate the 2D list
iterateUsingIterator(listOfLists);
}
}