Collection Programs
Now, Here we will discuss some basic or simple collection programs of java collection framework like ArrayList, LinkedList, Vector class and from Set interface - there is HashSet, LinkedHashSet, and TreeSet class. We will also discuss all the classes of Map interface. Let's starts...
List Interface
As we know List interface contains 3 classes in java collection which is ArrayList, LinkedList and Vector class.
(1) Java ArrayList Example
import java.util.*;
public class ALExample
{
public static void main(String...s)
{
//creating array list object
ArrayList<String> al = new ArrayList<String>();
al.add("simran");//adding elements
al.add("siya");
al.add("simran");
al.add("sid");
al.add("barkha");
//traverse the elements through Iterator interface
Iterator i = al.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}
output : simran
siya
simran
sid
barkha
Note : ArrayList class contains duplicate elements and maintains insertion order of an elements.
(2) Java LinkedList Example
import java.util.*;
public class LLExample
{
public static void main(String args[])
{
//creating linked list object
LinkedList<String> ll = new LinkedList<String>();
ll.add("apple");
ll.add("mango");
ll.add("orange");
ll.add("apple");
ll.add("banana");
//traverse the elements through Iterator interface
Iterator i = ll.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}
output : apple
mango
orange
apple
banana
Note : LinkedList class contains duplicate elements and maintains insertion order of an elements like array list class.
(3) Java Vector Example
In this vector example, we will use 2 methods for inserting elements in vector and you can use one method also. We will use Enumeration interface to traverse the elements and you can also use Iterator interface to traverse elements but here we will use Enumeration only.
import java.util.*;
public class VectorExample3
{
public static void main(String args[])
{
//creating vector object
Vector<String> v = new Vector<String>();
v.add("rahul");
v.add("varun");
v.addElement("varun");
v.addElement("ranvir");
//traverse the elements by using Enumeration interface
Enumeration e = v.elements();
while(e.hasMoreElements())
{
System.out.println(e.nextElement());
}
}
}
output : rahul
varun
varun
ranvir
Set Interface
As we know, Set interface contains 3 classes in java which is HashSet, LinkedHashSet and TreeSet class.
(1) Java HashSet Example
import java.util.*;
public class HSExample
{
public static void main(String args[])
{
//creating hash set object
HashSet<String> hs = new HashSet<String>();
hs.add("pink");
hs.add("yellow");
hs.add("pink");
hs.add("black");
hs.add("orange");
//traverse the elements by using Iterator interface
Iterator i = hs.iterator();
while(i.hasNext());
{
System.out.println(i.next());
}
}
}
output : pink
black
yellow
orange
Note : Java HashSet class contains only unique elements and it doesn't maintains any insertion order of an elements.
(2) Java LinkedHashSet Example
import java.util.*;
public class LHSExample
{
public static void main(String args[])
{
//creating linked hash set object
LinkedHashSet<String> lhs = new LinkedHashSet<String>();
lhs.add("pink");
lhs.add("yellow");
lhs.add("pink");
lhs.add("black");
lhs.add("orange");
//traverse the elements by using Iterator interface
Iterator i = lhs.iterator();
while(i.hasNext());
{
System.out.println(i.next());
}
}
}
output : pink
yellow
black
orange
Note : LinkedHashSet class contains unique elements and it maintains insertion order of an elements.
(3) TreeSet
import java.util.*;public class TreeSetExample
{
public static void main(String args[])
{
//creating tree set object
TreeSet<String> ts = new TreeSet<String>();
ts.add("zebra");
ts.add("lion");
ts.add("tiger");
ts.add("dog");
ts.add("cat");
//traverse the elements by using Iterator interface
Iterator i = ts.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}
output : cat
dog
lion
tiger
zebraNote : TreeSet class contains only unique elements and print the elements in ascending order.
Map Interface
As we know that Map interface contains 4 classes which is HashMap, LinkedHashMap, Hashtable and TreeMap class.
(1) Java HashMap Example
import java.util.*;public class HMExample
{
public static void main(String args[])
{
// creating hash map object
HashMap<Integer, String> hm = new HashMap<Integer, String>();
hm.put(1,"amir");
hm.put(2,"salman");
hm.put(3,"amitabh");
hm.put(4,"varun");
for(Map.Entry m : hm.entrySet())
{
System.out.println(m.getKey()+" "+m.getValue());
}
}
}
output : 1 amir
2 salman
3 amitabh
4 varunNote : HashMap class take a key and value pairs and it doesn 't allow duplicate elements. it does not maintains insertion order of an elements.
(2) Java LinkedHashMap Example
import java.util.*;public class LHMExample
{
public static void main(String args[])
{
// creating linked hash map object
LinkedHashMap<Integer, String> lhm = new LinkedHashMap<Integer, String>();
lhm.put(1,"amir");
lhm.put(2,"salman");
lhm.put(3,"amitabh");
lhm.put(4,"varun");
for(Map.Entry m : lhm.entrySet())
{
System.out.println(m.getKey()+" "+m.getValue());
}
}
}
output : 1 amir
2 salman
3 amitabh
4 varun
Note : LinkedHashMap class does not allow duplicate elements and it maintains insertion order of an elements.
(3) Java Hashtable Example
import java.util.*;public class HashTableExample
{
public static void main(String args[])
{
// creating hash table object
Hashtable<Integer, String> ht = new Hashtable<Integer, String>();
ht.put(1,"rose");
ht.put(2,"lotus");
ht.put(3,"red rose");
ht.put(4,"red lotus");
for(Map.Entry m : ht.entrySet())
{
System.out.println(m.getKey()+" "+m.getValue());
}
}
}
output : 4 red lotus
3 red rose
2 lotus
1 rose
(4) Java TreeMap Example
import java.util.*;public class TMExample
{
public static void main(String args[])
{
// creating tree map object
TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
tm.put(1,"rose");
tm.put(5,"lotus");
tm.put(3,"red rose");
tm.put(6,"red lotus");
for(Map.Entry m : tm.entrySet())
{
System.out.println(m.getKey()+" "+m.getValue());
}
}
}
output : 1 rose
3 red rose
5 lotus
6 red lotus
Note : TreeMap class takes unique elements and maintains ascending order of an elements.
Thanks for sharing this good blog.This is very important and imformative blog for Java .It's very interesting and useful for students
ReplyDeleteCore Java Online Training Hyderabad
I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts. Python Projects for Students Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account. Project Center in Chennai
ReplyDelete