Collection-Dynamic data structure to store and manipulating group of elements.

Collections Framework– It’s a architecture for representing and manipulating collections.

In java, collection framework includes 3 things-

-(a)interfaces
-(b)implemented classes
-(c)algorithms

Collections Hierarchy –

colfrmwrk

Ordered: When a collection is ordered, it means you can iterate through the collection in a specific (not-random) order

Sorted:  A sorted collection means that the order of objects in the collection is determined according to some rule or rules, known as the sorting order.

code samples are here

1- Arraylist : creation and iteration using Iterator

package CollectionFramework;

import java.util.*;
public class ExArrList1 {

public static void main(String[] args) {
List<Integer> collDemo=new ArrayList<Integer>();

collDemo.add(5);
collDemo.add(15);
collDemo.add(15);
collDemo.add(12);

Iterator<Integer> iterator=collDemo.iterator();
while(iterator.hasNext()){
System.out.println("using iterator :only next: "+iterator.next());
}
}
}
2- Arraylist : creation and iteration using ListIterator
package CollectionFramework;

import java.util.*;
public class ExArrList2 {

public static void main(String[] args) {
 List<Integer> collDemo=new ArrayList<Integer>();

collDemo.add(5);
collDemo.add(15);
collDemo.add(15);
collDemo.add(12);

ListIterator<Integer> listItr=collDemo.listIterator(); 

 while(listIterator.hasNext()){
System.out.println("using listinterator : next: "+listIterator.next());
}

while(listIterator.hasPrevious()){
System.out.println("using listinterator : previous: "+listIterator.previous());
}
}
}

3- Arraylist : creation and iteration using ForEach

package CollectionFramework;

import java.util.*;
public class ExArrList3 {

public static void main(String[] args) {
 List<Integer> collDemo=new ArrayList<Integer>(); 

collDemo.add(5);
collDemo.add(15);
collDemo.add(15);
collDemo.add(12);
for (Integer i : collDemo) {
System.out.println(i);
}
}
} 

4- LinkedList : creation and iteration using Iterator

package CollectionFramework;

import java.util.*;
public class ExLinkList1 {

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

collDemo.add(5);
collDemo.add(15);
collDemo.add(15);
collDemo.add(12);
Iterator<Integer> itr=collDemo.iterator(); 

while(iterator.hasNext()){
System.out.println("using iterator :only next: "+iterator.next());
}
}
} 

5- LinkedList : creation and iteration using  ListIterator

package CollectionFramework;

import java.util.*;
public class ExLinkList1 {

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

collDemo.add(5);
collDemo.add(15);
collDemo.add(15);
collDemo.add(12);
ListIterator<Integer> listItr=collDemo.listIterator(); 

 while(listIterator.hasNext()){
System.out.println("using listinterator : next: "+listIterator.next());
}

while(listIterator.hasPrevious()){
System.out.println("using listinterator : previous: "+listIterator.previous());
}
}
} 

6- LinkedList : creation and iteration using  ForEach

package CollectionFramework;

import java.util.*;
public class ExLinkList1 {

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

collDemo.add(5);
collDemo.add(15);
collDemo.add(15);
collDemo.add(12);
for (Integer i : collDemo) {
System.out.println(i);
}
}
 } 
7-Vector : creation and iteration using Iterator
package CollectionFramework;

import java.util.*;
public class ExVector1 {

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

collDemo.add(5);
collDemo.add(15);
collDemo.add(15);
collDemo.add(12);

Iterator<Integer> itr=collDemo.iterator(); 

while(iterator.hasNext()){
System.out.println("using iterator :only next: "+iterator.next());
}
}
} 

8-Vector : creation and iteration using ListIterator

package CollectionFramework;

import java.util.*;
public class ExVector2 {

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

collDemo.add(5);
collDemo.add(15);
collDemo.add(15);
collDemo.add(12);
ListIterator<Integer> listItr=collDemo.listIterator(); 

 while(listIterator.hasNext()){
System.out.println("using listinterator : next: "+listIterator.next());
}

while(listIterator.hasPrevious()){
System.out.println("using listinterator : previous: "+listIterator.previous());
}
}
} 
9- Vector : creation and iteration using ForEach
package CollectionFramework;

import java.util.*;
public class ExVector3 {

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

collDemo.add(5);
collDemo.add(15);
collDemo.add(15);
collDemo.add(12);
for (Integer i : collDemo) {
System.out.println(i);
}
}
} 
1-HashSet: Creation and Iteration using Iterator
package CollectionFramework;
import java.util.*;
public class ExHashset1 {

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

collDemo.add(5);
collDemo.add(15);
collDemo.add(15);
collDemo.add(12);
Iterator<integer> itr=collDemo.iterator(); 

while(iterator.hasNext()){
System.out.println("using iterator :only next: "+iterator.next());
}
}
} 
 2-LinkedHashSet: Creation and Iteration using Iterator
package CollectionFramework;
import java.util.*;
public class ExLinkHashset1 {

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

collDemo.add(5);
collDemo.add(15);
collDemo.add(15);
collDemo.add(12);
Iterator<integer> itr=collDemo.iterator(); 

while(iterator.hasNext()){
System.out.println("using iterator :only next: "+iterator.next());
}
}
} 

3- TreeSet: Creation and Iteration using Iterator

package CollectionFramework;
import java.util.*;
public class ExLinkHashset1 {

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

collDemo.add(5);
collDemo.add(15);
collDemo.add(15);
collDemo.add(12);
Iterator<integer> itr=collDemo.iterator(); 

while(iterator.hasNext()){
System.out.println("using iterator :only next: "+iterator.next());
}
}
} 
1- PriorityQueue: creation and iteration using Iterator
package CollectionFramework;

import java.util.*;
public class ExPrQue1 {

public static void main(String[] args) {
 Queue<integer> collDemo=new PriorityQueue<integer>();
collDemo.add(5);
collDemo.add(15);
collDemo.add(15);
collDemo.add(12);
Iterator<integer> itr=collDemo.iterator(); 

while(iterator.hasNext()){
System.out.println("using iterator :only next: "+iterator.next());
}
}
} 
2- PriorityQueue: creation and iteration using ForEach
package CollectionFramework;

import java.util.*;
public class ExPrQue2 {

public static void main(String[] args) {
Queue<integer> collDemo=new PriorityQueue<integer>();
collDemo.add(5);
collDemo.add(15);
collDemo.add(15);
collDemo.add(12);

for (Integer i : collDemo) {
System.out.println(i);
}
}
} 
1- ArrayDeque : creation and iteration using Iterator
package CollectionFramework;

import java.util.*;
public class ExArrDQue1 {

public static void main(String[] args) {
Queue<integer> collDemo=new ArrayDeque<integer>();
collDemo.add(5);
collDemo.add(15);
collDemo.add(15);
collDemo.add(12);
Iterator<integer> itr=collDemo.iterator(); 

while(iterator.hasNext()){
System.out.println("using iterator :only next: "+iterator.next());
}
}
} 

2- ArrayDeque : creation and iteration using ForEach
package CollectionFramework;

import java.util.*;
public class ExArrDQue2 {

public static void main(String[] args) {
Queue<integer> collDemo=new ArrayDeque<integer>();
collDemo.add(5);
collDemo.add(15);
collDemo.add(15);
collDemo.add(12);
for (Integer i : collDemo) {
System.out.println(i);
}
}
} 

1- Hashmap : creation and iteration using Iterator

package CollectionFramework;

import java.util.*;

public class ExHashmap {

public static void main(String[] args) {
Map<String, String> colldemoMap=new HashMap<String, String>();
colldemoMap.put("Key1","Value1");
colldemoMap.put("key2","Value2");
colldemoMap.put("key3","Value3");
Iterator iterator=colldemoMap.keySet().iterator();
while(iterator.hasNext()){
String key=iterator.next().toString();
String value=colldemoMap.get(key);
System.out.println(key);
System.out.println(value);
}
}
} 
2- Hashmap : creation and iteration using ForEach
package CollectionFramework;

import java.util.*;

public class ExHashmap {

public static void main(String[] args) {

Map<String, String> colldemoMap=new HashMap<String, String>();
colldemoMap.put("Key1","Value1");
colldemoMap.put("key2","Value2");
colldemoMap.put("key3","Value3");

for(Object key : colldemoMap.keySet()){
String value=colldemoMap.get(key);
System.out.println(key);
System.out.println(value);
}
}
} 
1- LinkedHashmap : creation and iteration using Iterator
package CollectionFramework;

import java.util.*;

public class ExLinkHashmap1 {

public static void main(String[] args) {
Map<String, String> colldemoMap=new LinkedHashMap<String, String>();
colldemoMap.put("Key1","Value1");
colldemoMap.put("key2","Value2");
colldemoMap.put("key3","Value3");
Iterator iterator=colldemoMap.keySet().iterator();
while(iterator.hasNext()){
String key=iterator.next().toString();
String value=colldemoMap.get(key);
System.out.println(key);
System.out.println(value);
}
}
} 
2- LinkedHashmap : creation and iteration using ForEach
package CollectionFramework;

import java.util.*;

public class ExLinkHashmap2 {

public static void main(String[] args) {

Map<String, String> colldemoMap=new LinkedHashMap<String, String>();
colldemoMap.put("Key1","Value1");
colldemoMap.put("key2","Value2");
colldemoMap.put("key3","Value3");

for(Object key : colldemoMap.keySet()){
String value=colldemoMap.get(key);
System.out.println(key);
System.out.println(value);
}
}
} 
1- Treemap : creation and iteration using Iterator
package CollectionFramework;

import java.util.*;

public class ExTreeHashmap1 {

public static void main(String[] args) {
Map<String, String> colldemoMap=new TreeMap<String, String>();
colldemoMap.put("Key1","Value1");
colldemoMap.put("key2","Value2");
colldemoMap.put("key3","Value3");
Iterator iterator=colldemoMap.keySet().iterator();
while(iterator.hasNext()){
String key=iterator.next().toString();
String value=colldemoMap.get(key);
System.out.println(key);
System.out.println(value);
}
}
} 
2- Treemap : creation and iteration using ForEach
package CollectionFramework;

import java.util.*;

public class ExTreeHashmap2 {

public static void main(String[] args) {

Map<String, String> colldemoMap=new TreeMap<String, String>();
colldemoMap.put("Key1","Value1");
colldemoMap.put("key2","Value2");
colldemoMap.put("key3","Value3");

for(Object key : colldemoMap.keySet()){
String value=colldemoMap.get(key);
System.out.println(key);
System.out.println(value);
}
}
} 
1- Sorting a collection
package CollectionFramework;

import java.util.*;
public class ExsortArrList1 {

public static void main(String[] args) {
List<Integer> collDemo=new ArrayList<Integer>();

collDemo.add(5);
collDemo.add(15);
collDemo.add(15);
collDemo.add(12);

Collections.sort(collDemo);

Iterator<Integer> iterator=collDemo.iterator();
while(iterator.hasNext()){
System.out.println("using iterator :only next: "+iterator.next());
}
}
}
 2- Searching in collection
package CollectionFramework;

import java.util.*;
public class ExsearchingArrList1 {

public static void main(String[] args) {
List<Integer> collDemo=new ArrayList<Integer>();

collDemo.add(5);
collDemo.add(15);
collDemo.add(15);
collDemo.add(12);</pre>
<pre>Collections.sort(collDemo);
</pre>
<pre>int i = Collections.binarySearch(collDemo,12); 

System.out.println("position of element is ");
System.out.println(i);
}
} 
3- Shuffling a collection
package CollectionFramework;

import java.util.*;
public class ExshuffleArrList1 {

public static void main(String[] args) {
List<Integer> collDemo=new ArrayList<Integer>();

collDemo.add(5);
collDemo.add(15);
collDemo.add(15);
collDemo.add(12);

Collections.shuffle(collDemo);

Iterator<Integer> iterator=collDemo.iterator();
while(iterator.hasNext()){
System.out.println("using iterator :only next: "+iterator.next());
}
}
}

4– Reversing a collection

package CollectionFramework;

import java.util.*;
public class ExreverseArrList1 {

public static void main(String[] args) {
List<Integer> collDemo=new ArrayList<Integer>();

collDemo.add(5);
collDemo.add(15);
collDemo.add(15);
collDemo.add(12);

Collections.reverse(collDemo);

Iterator<Integer> iterator=collDemo.iterator();
while(iterator.hasNext()){
System.out.println("using iterator :only next: "+iterator.next());
}
}
}
Advertisements