Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maps #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/main/java/com/demo/program/collection/ArrayListDemo.java
Original file line number Diff line number Diff line change
@@ -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<list.size();i++)
{
Object obj = list.get(i);
if(obj instanceof Employee){
System.out.println("Employee Details:");
System.out.println("Id: "+((Employee)obj).getId());
System.out.println("Name: "+((Employee)obj).getName());
System.out.println("Salary: "+((Employee)obj).getSalary());
}
else
{
System.out.println(obj);
}
}

}

}
51 changes: 51 additions & 0 deletions src/main/java/com/demo/program/collection/Employee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.collection;

public class Employee {

private int id;
private String name;
private float salary;

public Employee(int id, String name, float salary) {
this.id = id;
this.name = name;
this.salary = salary;
}

public int getId() {
return id;
}

public String getName() {
return name;
}

public float getSalary() {
return salary;
}

public String toString()
{
return ("ID:"+id+" Name:"+name+" Salary:"+salary);
}

public int hashCode()
{
return id;
}

public boolean equals(Object obj)
{
if(obj instanceof Employee)
{
Employee e = (Employee)obj;
if(this.id==e.id && this.name==e.name && this.salary==e.salary)
{
return true;
}
}
return false;

}

}
30 changes: 30 additions & 0 deletions src/main/java/com/demo/program/collection/HashSetDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.collection;

import java.util.HashSet;
import java.util.Iterator;

public class HashSetDemo {

public static void main(String[] args) {

HashSet h = new HashSet();

h.add(new Integer(100));
h.add(10.9);
h.add(60.5f);
h.add("Hello World!");
h.add(60.5f);
h.add(new Employee(10,"Sakshi",100000));
h.add(new Employee(12,"Palak",100000));
h.add(new Employee(10,"Ritesh",100000));
h.add(new Employee(12,"Prachi",100000));
h.add(10);
Iterator i = h.iterator();

while(i.hasNext())
{
Object o = i.next();
System.out.println(o);
}
}
}
48 changes: 48 additions & 0 deletions src/main/java/com/demo/program/collection/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.collection;

public class Product implements Comparable<Product>{

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<p.productId){
return -1;
}
return 0;*/

public int compareTo(Product p)
{
return p.productId - this.productId;
//return this.productId - p.productId;
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/demo/program/collection/ProductCompare.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.collection;

import java.util.Comparator;

public class ProductCompare implements Comparator<Product>{

/*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());
}
}
61 changes: 61 additions & 0 deletions src/main/java/com/demo/program/collection/TreeSetDemo.java
Original file line number Diff line number Diff line change
@@ -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<Integer> t = new TreeSet<Integer>();

t.add(new Integer(10));
t.add(20);
t.add(30);
t.add(100);
t.add(100);
t.add(100);

Iterator<Integer> it = t.iterator();
while(it.hasNext())
{
Object o = it.next();
System.out.println(o);
}

TreeSet<String> ts = new TreeSet<String>();

ts.add("Hi");
ts.add("Hello");
ts.add("Welcome");
ts.add("Good Morning");

System.out.println(ts);

TreeSet<Product> p = new TreeSet<Product>();
p.add(new Product(11, "Shoes", 1000));
p.add(new Product(13, "Watches",3460));
p.add(new Product(12, "Bags", 4000));


Iterator<Product> it1 = p.iterator();
while(it1.hasNext())
{
Product p1 = it1.next();
System.out.println(p1);
}
System.out.println("----------------------------------------------------");
TreeSet<Product> p1 = new TreeSet<Product>(new ProductCompare());
p1.add(new Product(11, "Shoes", 1000));
p1.add(new Product(13, "Watches",3460));
p1.add(new Product(12, "Bags", 4000));


Iterator<Product> it2 = p1.iterator();
while(it2.hasNext())
{
Product p2 = it2.next();
System.out.println(p2);
}
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/demo/program/collection/VectorDemo.java
Original file line number Diff line number Diff line change
@@ -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);
}

}

}
37 changes: 37 additions & 0 deletions src/main/java/com/demo/program/mapdemo/Employee.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
54 changes: 54 additions & 0 deletions src/main/java/com/demo/program/mapdemo/HashMapDemo.java
Original file line number Diff line number Diff line change
@@ -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<Integer,String> map = new HashMap<Integer, String>();

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<Integer> keys = map.keySet();

for(Integer k : keys)
{
System.out.println("Key :"+k+" Value: "+map.get(k));
}


HashMap<Employee, String> empmap = new HashMap<Employee, String>();

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<Employee> empset=empmap.keySet();
Iterator<Employee> it = empset.iterator();
while(it.hasNext())
{
Employee emp = it.next();
System.out.println("Key :"+emp+" Value: "+empmap.get(emp));
}

}

}
27 changes: 27 additions & 0 deletions src/main/java/com/demo/program/mapdemo/HashTableDemo.java
Original file line number Diff line number Diff line change
@@ -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<Integer,Employee> map = new Hashtable<Integer, Employee>();

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<Integer> set = map.keySet();
for(Integer i : set)
{
System.out.println("Key: "+i+" Value: "+map.get(i));
}

}
}
Loading