An Introduction to Collection

List

import java.io.*;
import java.util.*;

class test {
public static void main(String[] args)
{

List<Integer> list = new ArrayList<Integer>();

list.add(1);
list.add(2);
list.add(6);
list.add(4);

System.out.println(list);

list.remove(3);

System.out.println(list);

for (int i = 0; i < list.size(); i++)
System.out.print(list.get(i) + " ");
}
}
import java.io.*;
import java.util.*;

class test {
public static void main(String[] args)
{

List<Integer> ll = new LinkedList<Integer>();
ll.add(1);
ll.add(2);
ll.add(7);
ll.add(6);
ll.add(1);


System.out.println(ll);
ll.remove(3);
System.out.println(ll);

for (int i = 0; i < ll.size(); i++)
System.out.print(ll.get(i) + " ");
}
}
import java.io.*;
import java.util.*;

class test {
public static void main(String[] args)
{
List<Integer> v = new Vector<Integer>();

v.add(1);
v.add(2);
v.add(5);
v.add(19);
v.add(1);

System.out.println(v);

v.remove(3);

System.out.println(v);

for (int i = 0; i < v.size(); i++)
System.out.print(v.get(i) + " ");
}
}
import java.io.*;
import java.util.*;

class test {
public static void main(String[] args)
{
List<Integer> s = new Stack<Integer>();
s.add(1);
s.add(2);
s.add(3);
s.add(5);
System.out.println(s);
s.remove(3);

System.out.println(s);

for (int i = 0; i < s.size(); i++)
System.out.print(s.get(i) + " ");
}
}

Queue

import java.util.LinkedList;
import java.util.Queue;

public class test {

public static void main(String[] args)
{
Queue<Integer> q = new LinkedList<>();

for (int i = 0; i < 5; i++)
q.add(i);

System.out.println( q);
int removedele = q.remove();
System.out.println(removedele);

System.out.println(q);

int head = q.peek();
System.out.println(head);

int size = q.size();
System.out.println( size);
}
}
import java.util.*;
public class test {
public static void main(String[] args)
{
Deque<Integer> de= new ArrayDeque<Integer>();

de.add(10);
de.add(20);
de.add(30);
de.add(40);
de.add(50);

System.out.println(de);
de.clear();
de.addFirst(291);
de.addLast(24);
de.addLast(14);

System.out.println(de);
}
}

Set

import java.util.*;

class test{

public static void main(String[] args)
{
Set<String> h = new HashSet<String>();

h.add("India");
h.add("Australia");
h.add("South Africa");
h.add("India");

System.out.println(h);

h.remove("Australia");
System.out.println(h);
Iterator<String> i = h.iterator();
while (i.hasNext())
System.out.println(i.next());
}
}
import java.util.*;

class test {

public static void main(String[] args)
{
Set<String> h = new LinkedHashSet<String>();

h.add("India");
h.add("Australia");
h.add("South Africa");
h.add("India");

System.out.println(h);

h.remove("Australia");
System.out.println(h);

Iterator<String> i = h.iterator();
while (i.hasNext())
System.out.println(i.next());
}
}
import java.util.*;

class test {
public static void main(String[] args)
{
Set<String> t= new TreeSet<String>();
t.add("India");
t.add("Australia");
t.add("South Africa");
t.add("India");

System.out.println(t);
t.remove("Australia");
System.out.println(t);

Iterator<String> i = t.iterator();
while (i.hasNext())
System.out.println(i.next());
}
}

Map

import java.util.*;

public class test {

public static void main(String[] args)
{
Map<String, Integer> map = new HashMap<>();

map.put("vishal", 10);
map.put("sachin", 30);
map.put("abhi", 20);

for (Map.Entry<String, Integer> e : map.entrySet())
System.out.println(e.getKey() + " "
+ e.getValue());
}
}
import java.util.*;

public class test{

public static void main(String[] args)
{
Map<String, Integer> map = new LinkedHashMap<>();

map.put("vishal", 10);
map.put("shashank", 30);
map.put("abhi", 20);

for (Map.Entry<String, Integer> e : map.entrySet())
System.out.println(e.getKey() + " "
+ e.getValue());
}
}
import java.util.*;

public class test {

public static void main(String[] args)
{
Map<String, Integer> map = new TreeMap<>();

map.put("vishal", 10);
map.put("samarth", 30);
map.put("vaibhav", 50);

for (Map.Entry<String, Integer> e : map.entrySet())
System.out.println(e.getKey() + " " + e.getValue());
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store