diff --git a/src/main/java/com/demo/program/collection/ArrayListDemo.java b/src/main/java/com/demo/program/collection/ArrayListDemo.java new file mode 100644 index 0000000..ddaaaf7 --- /dev/null +++ b/src/main/java/com/demo/program/collection/ArrayListDemo.java @@ -0,0 +1,36 @@ +package com.collection; + +import java.util.ArrayList; + +public class ArrayListDemo { + + public static void main(String[] args) { + + ArrayList list = new ArrayList(); + list.add(new Integer(10)); + list.add(new Float(20.6)); + list.add(new String("ABC")); + list.add("Welcome"); + list.add(30.8); + list.add(1, "HI"); + list.add(30.8); + list.add(new Employee(10,"Sakshi",10.5f)); + list.add(new Employee(12,"Palak",13.5f)); + for(int i=0;i{ + + private int productId; + private String productName; + private float price; + public Product(int productId, String productName, float price) { + super(); + this.productId = productId; + this.productName = productName; + this.price = price; + } + + public int getProductId() { + return productId; + } + + public String getProductName() { + return productName; + } + + public float getPrice() { + return price; + } + + public String toString() { + return "Product [productId=" + productId + ", productName=" + + productName + ", price=" + price + "]"; + } + + /*public int compareTo(Object obj) + { + Product p = (Product)obj; + if(this.productId>(p.productId)){ + return 1; + } + else if(this.productId{ + + /*public int compare(Object obj1, Object obj2) { + + Product p1 = (Product)obj1; + Product p2 = (Product)obj2; + return p1.getProductName().compareTo(p2.getProductName()); + } +*/ + public int compare(Product obj1, Product obj2) { + return obj1.getProductName().compareTo(obj2.getProductName()); +} +} diff --git a/src/main/java/com/demo/program/collection/TreeSetDemo.java b/src/main/java/com/demo/program/collection/TreeSetDemo.java new file mode 100644 index 0000000..c315cf6 --- /dev/null +++ b/src/main/java/com/demo/program/collection/TreeSetDemo.java @@ -0,0 +1,61 @@ +package com.collection; + +import java.util.Iterator; +import java.util.TreeSet; + +public class TreeSetDemo { + + public static void main(String[] args) { + + TreeSet t = new TreeSet(); + + t.add(new Integer(10)); + t.add(20); + t.add(30); + t.add(100); + t.add(100); + t.add(100); + + Iterator it = t.iterator(); + while(it.hasNext()) + { + Object o = it.next(); + System.out.println(o); + } + +TreeSet ts = new TreeSet(); + + ts.add("Hi"); + ts.add("Hello"); + ts.add("Welcome"); + ts.add("Good Morning"); + + System.out.println(ts); + + TreeSet p = new TreeSet(); + p.add(new Product(11, "Shoes", 1000)); + p.add(new Product(13, "Watches",3460)); + p.add(new Product(12, "Bags", 4000)); + + + Iterator it1 = p.iterator(); + while(it1.hasNext()) + { + Product p1 = it1.next(); + System.out.println(p1); + } + System.out.println("----------------------------------------------------"); + TreeSet p1 = new TreeSet(new ProductCompare()); + p1.add(new Product(11, "Shoes", 1000)); + p1.add(new Product(13, "Watches",3460)); + p1.add(new Product(12, "Bags", 4000)); + + + Iterator it2 = p1.iterator(); + while(it2.hasNext()) + { + Product p2 = it2.next(); + System.out.println(p2); + } + } +} diff --git a/src/main/java/com/demo/program/collection/VectorDemo.java b/src/main/java/com/demo/program/collection/VectorDemo.java new file mode 100644 index 0000000..e4687c8 --- /dev/null +++ b/src/main/java/com/demo/program/collection/VectorDemo.java @@ -0,0 +1,29 @@ +package com.collection; + +import java.util.Enumeration; +import java.util.Vector; + +public class VectorDemo { + + public static void main(String[] args) { + + Vector list = new Vector(); + + list.addElement(new Integer(20)); + list.addElement(20.5f); + list.addElement(30); + list.addElement("Hello"); + list.addElement(new String("Hi")); + list.addElement(new String("Hi")); + list.add(new Employee(10,"Sakshi",10.5f)); + list.add(new Employee(12,"Palak",13.5f)); + Enumeration e = list.elements(); + while(e.hasMoreElements()) + { + Object o = e.nextElement(); + System.out.println(o); + } + + } + +}