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); + } + + } + +} diff --git a/src/main/java/com/demo/program/mapdemo/Employee.java b/src/main/java/com/demo/program/mapdemo/Employee.java new file mode 100644 index 0000000..bcb1d8b --- /dev/null +++ b/src/main/java/com/demo/program/mapdemo/Employee.java @@ -0,0 +1,37 @@ +package com.map; + +public class Employee { + + private int id; + private String name; + private float salary; + public Employee(int id, String name, float salary) { + super(); + this.id = id; + this.name = name; + this.salary = salary; + } + @Override + public String toString() { + return "Employee [id=" + id + ", name=" + name + ", salary=" + salary + + "]"; + } + + public int hashCode() + { + return id; + } + + public boolean equals(Object obj) + { + Employee emp = (Employee)obj; + if(this.name.equals(emp.name)) + { + if(this.salary == emp.salary) + { + return true; + } + } + return false; + } +} diff --git a/src/main/java/com/demo/program/mapdemo/HashMapDemo.java b/src/main/java/com/demo/program/mapdemo/HashMapDemo.java new file mode 100644 index 0000000..d9b275b --- /dev/null +++ b/src/main/java/com/demo/program/mapdemo/HashMapDemo.java @@ -0,0 +1,54 @@ +package com.map; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Set; + +public class HashMapDemo { + + public static void main(String[] args) { + + + HashMap map = new HashMap(); + + map.put(new Integer(10), "Sakshi"); + map.put(new Integer(20), "Palak"); + map.put(40, "prachi"); + map.put(40,"Ritesh"); + map.put(50, "prachi"); + map.put(null, "Hi"); + map.put(null, "Hello"); + map.put(60, null); + map.put(70,null); + + + //String s = map.get(10); + Set keys = map.keySet(); + + for(Integer k : keys) + { + System.out.println("Key :"+k+" Value: "+map.get(k)); + } + + + HashMap empmap = new HashMap(); + + Employee emp1=new Employee(101,"sakshi",1290); + Employee emp2=new Employee(101,"sakshi",1290); + Employee emp3=new Employee(103,"palak",34324); + + empmap.put(emp1, "Hi"); + empmap.put(emp2, "Hello"); + empmap.put(emp3, "Hi"); + + Set empset=empmap.keySet(); + Iterator it = empset.iterator(); + while(it.hasNext()) + { + Employee emp = it.next(); + System.out.println("Key :"+emp+" Value: "+empmap.get(emp)); + } + + } + +} diff --git a/src/main/java/com/demo/program/mapdemo/HashTableDemo.java b/src/main/java/com/demo/program/mapdemo/HashTableDemo.java new file mode 100644 index 0000000..b8d620e --- /dev/null +++ b/src/main/java/com/demo/program/mapdemo/HashTableDemo.java @@ -0,0 +1,27 @@ +package com.map; + +import java.util.Hashtable; +import java.util.Set; + +import com.map.Employee; + +public class HashTableDemo { + + public static void main(String[] args) { + + Hashtable map = new Hashtable(); + + map.put(1,new Employee(101,"Sakshi",1000)); + map.put(2,new Employee(101, "Sakshi", 1000)); + map.put(new Integer(4), new Employee(101, "Ritesh", 90000)); + map.put(new Integer(6), new Employee(103, "Palak", 60000)); + //map.put(5,null); + + Set set = map.keySet(); + for(Integer i : set) + { + System.out.println("Key: "+i+" Value: "+map.get(i)); + } + + } +} diff --git a/src/main/java/com/demo/program/mapdemo/Student.java b/src/main/java/com/demo/program/mapdemo/Student.java new file mode 100644 index 0000000..6aedeb9 --- /dev/null +++ b/src/main/java/com/demo/program/mapdemo/Student.java @@ -0,0 +1,34 @@ +package com.map; + +public class Student implements Comparable{ + + private int rollNo; + private String name; + private float percentage; + public Student(int rollNo, String name, float percentage) { + super(); + this.rollNo = rollNo; + this.name = name; + this.percentage = percentage; + } + @Override + public String toString() { + return "Student [rollNo=" + rollNo + ", name=" + name + ", percentage=" + + percentage + "]"; + } + + public int compareTo(Student s) + { + return this.rollNo-s.rollNo; + } + public int getRollNo() { + return rollNo; + } + public String getName() { + return name; + } + public float getPercentage() { + return percentage; + } + +} diff --git a/src/main/java/com/demo/program/mapdemo/StudentComparator.java b/src/main/java/com/demo/program/mapdemo/StudentComparator.java new file mode 100644 index 0000000..aeecc6f --- /dev/null +++ b/src/main/java/com/demo/program/mapdemo/StudentComparator.java @@ -0,0 +1,15 @@ +package com.map; + +import java.util.Comparator; + +public class StudentComparator implements Comparator{ + + @Override + public int compare(Student s1, Student s2) { + + return s1.getName().compareTo(s2.getName()); + } + + + +} diff --git a/src/main/java/com/demo/program/mapdemo/TreeMapDemo.java b/src/main/java/com/demo/program/mapdemo/TreeMapDemo.java new file mode 100644 index 0000000..2012c22 --- /dev/null +++ b/src/main/java/com/demo/program/mapdemo/TreeMapDemo.java @@ -0,0 +1,56 @@ +package com.map; + +import java.util.Set; +import java.util.TreeMap; + +public class TreeMapDemo { + + public static void main(String[] args) { + TreeMap< Integer, String> map = new TreeMap(); + + map.put(2,"Hi"); + map.put(1, "Hello"); + map.put(3, "Welcome"); + + Set keys = map.keySet(); + for(Integer i : keys) + { + System.out.println("Key :"+i+" Value: "+map.get(i)); + } + + TreeMap map1 = new TreeMap(); + map1.put("Hi", new Employee(101,"Sakshi",1000)); + map1.put("Bitch", new Employee(102,"Palak",2000)); + + Set keys2 = map1.keySet(); + for(String s : keys2) + { + System.out.println("Key :"+s+" Value: "+map1.get(s)); + } + + + TreeMap map2 = new TreeMap(); + + map2.put(new Student(3,"Sakshi",87.3f), "hi"); + map2.put(new Student(2,"Palak",85.3f), "hello"); + map2.put(new Student(4,"Prachi",75.9f), "today"); + + Set set = map2.keySet(); + for(Student s : set) + { + System.out.println("Key: "+s+" Value: "+map2.get(s)); + } + System.out.println("---------------------------------------------------"); + + TreeMap map3 = new TreeMap(new StudentComparator()); + map3.put(new Student(3,"Sakshi",87.3f), "hi"); + map3.put(new Student(2,"Palak",85.3f), "hello"); + map3.put(new Student(4,"Prachi",75.9f), "today"); + + Set set1 = map3.keySet(); + for(Student s : set1) + { + System.out.println("Key: "+s+" Value: "+map3.get(s)); + } + } +}