Skip to content

Commit

Permalink
Map and Set Demos
Browse files Browse the repository at this point in the history
  • Loading branch information
amit4alljava committed Sep 13, 2015
1 parent 63fcde9 commit a5ebba8
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Collections/MapDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import java.util.HashMap;
import java.util.TreeSet;


public class MapDemo {

public static void main(String[] args) {
HashMap<String,Object> map = new HashMap<>();
TreeSet<Integer> amitPhones = new TreeSet<>();
amitPhones.add(2222);
amitPhones.add(2222);
amitPhones.add(3333);
map.put("amit", amitPhones);
//map.put("amit",3333);
map.put("ram", 3333);
System.out.println(map.get("amit"));
System.out.println(map);


}

}
47 changes: 47 additions & 0 deletions Collections/SetDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import java.util.HashSet;

class Customer
{
int id;
String name;
Customer(int id , String name){
this.id = id ;
this.name = name;
}
public String toString(){
return "Id "+id +" Name "+name;
}

public boolean equals(Object o){
Customer c = (Customer) o;
if(this.id==c.id && this.name.equals(c.name)){
return true;
}
else
{
return false;
}
}

@Override
public int hashCode(){
return name.length();
}
}
public class SetDemo {

public static void main(String[] args) {
Customer ram = new Customer(1001,"Ram");
HashSet<Customer> hashSet = new HashSet<>();
hashSet.add(ram);
Customer ram2 = new Customer(1001,"Ram");
hashSet.add(ram2);
System.out.println("Ram HashCode "+ram.hashCode());
System.out.println("Ram2 HashCode "+ram2.hashCode());
System.out.println(hashSet);



}

}
15 changes: 15 additions & 0 deletions Collections/SimpleSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import java.util.HashSet;


public class SimpleSet {

public static void main(String[] args) {
HashSet<String> hs = new HashSet<String>();
hs.add("mike");
hs.add("tim");
hs.add("mike");
hs.add("tim");
System.out.println(hs);
}

}

0 comments on commit a5ebba8

Please sign in to comment.