Skip to main content

Posts

Showing posts from July, 2016

Collection(Vector,Stack)

Vector:- Vector is a group of individual objects it is also called legacy class. Vector   implements List Interface. Like   ArrayList   it also maintains insertion order but it is rarely used in non-thread environment as it is synchronized and due to which it gives poor performance in searching, adding, delete and update of its elements. Three ways to create vector class object: Method 1:- Vector v = new Vector(); It creates an empty Vector with the default initial capacity of 10. It means the Vector will be re-sized when the 11th elements needs to be inserted into the Vector. Note: By default vector doubles its size. i.e. In this case the Vector size would remain 10 till 10 insertions and once we try to insert the 11th element It would become 20 (double of default capacity 10). Method 2: Syntax:   Vector object= new Vector(int initialCapacity) Vector v = new Vector(3); It will create a Vector of initial capacity of 3. Method 3: Syntax: Vector object= new vector

Collection(Set,HashSet,LinkedHashSet)

Set:- Set is a child interface of collection, set is used to represent group of individual objects as a single entity where duplicates not allowed and not maintain the insertion order of elements. In case of duplicate insertion add method return false otherwise return true,here HashSet is implemented class on Set interface. Example1:-TestSet.java import java.util.*; class TestSet { public static void main(String[]args) { Set s=new HashSet(); System.out.println(s); s.add(1); s.add(2); s.add(3); s.add(4); System.out.println(s); System.out.println("Set elements by Iterator"); Iterator itr=s.iterator(); while(itr.hasNext()) { System.out.println(itr.next()); } } } Output:- C:\JAVATECH>javac TestSet.java C:\JAVATECH>java TestSet [] [1, 2, 3, 4] Set elements by Iterator 1 2 3 4 Example2:-TestSet.java import java.util.*; class TestSet { public static void main(String[]args) { Set s=new HashSet(); Sy