Sei sulla pagina 1di 3

import java.util.

ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
public class collections {
public static void main(String[] args) {
//set wont allow dup keys and values.
//hashtable and map uses "put" to add elements.
//set and list uses "add" to add elements.
HashSet s=new HashSet();
s.add("w");
s.add("w");
s.add("f");
s.add("3");
s.add("null");
s.add(null);
s.add("g");
//s.add(avd); s.add(2);is not recomended.
//System.out.println(s.remove("3"));
System.out.println(s);
System.out.println(s.retainAll(s));
System.out.println(s);
System.out.println(s.size());

LinkedHashSet lhs=new LinkedHashSet();


lhs.add("2");
lhs.add("4");
lhs.add("null");
lhs.add(null);
lhs.add("k");
lhs.add("a");
System.out.println(lhs);
System.out.println(lhs.size());

TreeSet ss=new TreeSet();


ss.add("e");//here 'e' is object.
ss.add("a");
ss.add("2g");
ss.add("1g");
System.out.println(ss);
System.out.println(ss.first());
ss.clear();
System.out.println(ss);
ss.size();
Iterator i=ss.iterator();

List ll=new LinkedList();


ll.add(0,"f");//'0' is index and 'f' is element.
ll.add(1,"a");
ll.add(2,"h");
ll.add(3,"f");
ll.add("f");//here 'f' is object.
ll.add(null);
ll.add(6,"null");
System.out.println(ll);
System.out.println(ll.lastIndexOf("f"));
System.out.println(ll.indexOf("f"));

//iterator
/*Iterator jk=ll.iterator();
jk.hasNext();
jk.next();*/

//enumerator
/*Enumeration enum=(Enumeration) ll.iterator();
System.out.println("more:"+enum.hasMoreElements());
System.out.println(enum.nextElement());*/

//Enumeration j=jk.

ArrayList arr=new ArrayList();


arr.add(0,"e");
arr.add(1,"s");
arr.add("j");
arr.add(3,"j");
arr.add("null");
arr.add(null);
System.out.println(arr);
System.out.println(arr.lastIndexOf("j"));
System.out.println("sublist:"+arr.subList(0,2));

Hashtable ht=new Hashtable();//wont allow dup keys.


ht.put("1","a");
//ht.put(0,"abc");
ht.put("1","hjk");
ht.put("1","hkjh");
System.out.println(ht.size());
System.out.println(ht);

Vector vect=new Vector();


vect.add(0,"g");// here 0 is index and g is element.
vect.add(1,"a");
vect.add("g");
vect.add(3,"null");
//vect.setSize(1);
//System.out.println("vector size after setting:"+vect.size());
System.out.println(vect.capacity());
System.out.println(vect);
System.out.println(vect.indexOf("g"));
System.out.println(vect.lastIndexOf("g"));
vect.ensureCapacity(19);//if ensure capacity is less than 20 and
greater than 10 then capacity output is 20.
//if ec is 10 then capacity is 10.
//if ec is more than 20 then capacity is same as val passed
inside ec.
//System.out.println(vect.ensureCapacity(7));
System.out.println("capacity after ensure:"+vect.capacity());
System.out.println(vect.size());

//map wont allow dup keys and allow only dup values.
HashMap m=new HashMap();//hashmap wont allow dup keys and allow
dup values.
m.put(null,"dsabc");//it is not considered.
m.put(null,null);
m.put("fgh","null");
m.put("sdabc",null);
m.put("abc","abcd");
m.put("hg","abcd");//abc is key and abcd is value
System.out.println(m);

TreeMap sm=new TreeMap();//treemap wont allow dup keys and allows


dup values.
//SortedMap sm1=new SortedMap();
sm.put("abc","def");
sm.put("abc","hjk");
sm.put("jkl","def");
sm.put("abc","def");
System.out.println(sm);
System.out.println(sm.size());
System.out.println("firstkey is:"+sm.firstKey());
System.out.println("last key is"+sm.lastKey());

LinkedHashMap lhm=new LinkedHashMap();//linked hash map wont


allow dup keys and allow only dup values.
lhm.put("abc","def");
lhm.put("abc","hjk");
lhm.put("jkl","def");
lhm.put("abc","def");
System.out.println(lhm);
System.out.println(lhm.size());

}
}

Potrebbero piacerti anche