Monday, 15 September 2014

JAVA COLLECTIONS FRAMEWORK

Introductions to Collections

Collections like array holds stuff , holds references to other objects  but unlike array its dynamic


Populat sets - 


Collection - 

The root interface in the collection hierarchy. A collection represents a group of objects, known as its elements.
 Some collections allow duplicate elements and others do not. 
 Some are ordered and others unordered. The JDK does not provide any direct implementations of this interface: 
 it provides implementations of more specific subinterfaces like Set and List.
 This interface is typically used to pass collections around and manipulate them where maximum generality is desired. 



List -  

An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element 
is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.
Unlike sets, lists typically allow duplicate elements


Iterator -


 An iterator over a collection. Iterator takes the place of Enumeration in the Java Collections Framework. Iterators differ from enumerations like this that iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics. 

example
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;



public class Main {

public static void main (String args[]){

//initialise string for list collection
String i [] = {"cats" , "dogs" , "mouse" , "hamsters"};
String j[] = {"cats" ,  "dogs"};


// develope list interface for strings defined
List<String> list1 =  new ArrayList<>();
List<String> list2 =  new ArrayList<>();


//add the string objects in list collections
for(String a : i)
list1.add(a);
for(String a : j)
list2.add(a);

System.out.println("Before");
for (int k = 0  ; k < list1.size() ; k++)
System.out.printf("%s " , list1.get(k));

//edit the list1 , by deleting those entries which are present in list two
editlist(list1 , list2);

System.out.println();
System.out.println("After editing");
for (int k = 0  ; k < list1.size() ; k++)
System.out.printf("%s " , list1.get(k));



}


public static void editlist(Collection <String> l1 , Collection <String> l2){
Iterator <String> it =  l1.iterator();
while(it.hasNext()){
if(l2.contains(it.next()))
it.remove();

}

}

}



Collections -
Collections class consists exclusively of static methods that operate on or return collections. 
It contains polymorphic algorithms that operate on collections, "wrappers", which return a new 
collection backed by a specified collection, and a few other odds and ends. 
The methods of this class all throw a NullPointerException if the collections or class objects provided to them are null

Example - List<String> ll =  Arrays.asList(stringArrayName);
Collections.sort(ll); // aplpahbatically in ascending order
//polymorphic now
Collections.sort(ll, Collection.reverseOrder());

Other methods are Collections.reverse(); 
               Collections.copy( dest ,  src ) , Collections.fill()
Collections.addAll() -
Adds all of the specified elements to the specified collection. "Elements to be added                              may be
specified individually or as an array. "
The behavior of this
convenience method is identical to that of c.addAll(Arrays.asList(elements)),
but this method is likely to run significantly faster under most implementations

System.out.println(Collections.frequency(list,"digg"));
Returns the number of elements in the specified collection
                        equal to the specified object. More formally, returns the number of 
                         elements e in the collection
such that (o == null ? e == null : o.equals(e)).

Collections.disjoint(list1,list2);

Returns true if the two specified collections have no elements in common. 






No comments:

Post a Comment