Showing posts with label Java Collection Programs. Show all posts
Showing posts with label Java Collection Programs. Show all posts

Sunday, 24 July 2016

Java Code to Merge Two Array?

public class MergeTwoArray {

public static void main(String[] args)  {
String a[]={"a","b","c","d","e","f","g"};
String b[]={"h","i","j","k","l","m"};

List al=new ArrayList(Arrays.asList(a));
al.addAll(Arrays.asList(b));

System.out.print(al);
}

}

Thursday, 21 July 2016

Java Code for finding duplicate element from Arraylist

In Method 1 we are using set interface to find duplicate element from list because set only allow unique elements , In Method 2 we are using for loop to find duplicate element . 

public class DuplicateSingleElementFromList {

static int method1(List<Integer> list){
int duplicate = 0;
Set<Integer> set = new HashSet<>();
for(int i=0;i<list.size();i++){
if(!set.add(list.get(i))){
duplicate = list.get(i);
break;
}
}
return duplicate;
}
static int method2(List<Integer> list){
int duplicate = 0;
for(int i=0;i<list.size();i++){
for(int j=1;j<list.size();j++){
if(list.get(i) == list.get(j)){
duplicate = list.get(i);
break;
}
}
}
return duplicate;
}
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
for(int i=0;i<100;i++){
list.add(i);
}
list.add(11);
System.out.println("METHOD 1 OUTPUT : "+method1(list));
System.out.println("METHOD 2 OUTPUT : "+method2(list));
}

}

Saturday, 16 July 2016

Java Code Showing Internal Working of Hashmap

Hash map works on the principle of Hashing ,
Hashing is a technique which convert keys into some integer value known as Hash code .
Default size of hashmap is 16.
This Hashcode is used to store values into Hashmap.
Internally Hashmap uses Table which has array of nodes.
Each Block of table known as bucket.
Each bucket uses LinkedList to store elements and LinkedList store elements in the form node i.e key,hashcode,value and next node address
Example : 
HashMap<String,Integer> map = new HashMap<String, Integer >();
map.put(“ram”,100);
map.put(“shyam”,101);
map.put(“mohan”,102);

How put operation works in Hashmap.
Step 1 > First put operation calculate Hashcode of key . For Example here we calculate Hashcode of key ram  suppose Hashcode of key ram is 1475236 , we cannot create array of this size so we calculate index position from Hashcode (1475236) ,to calculate index position Hashmap divide the Hashcode by Map default size i.e 16 and use remainder (4)as a index to store key in table.







Step 2 > Now we calculate Hashcode of key shyam i.e 64205538 and index is 2 ,hashmap store this key at index 2.









Step 3 > Now we calculate Hashcode of key mohan i.e 4452852 and index is 4 ,hashmap store this key at index 4 .If any element is already available at particular index then bucket internally uses LinkedList to store element .This process continue for all the keys in Hashmap.








How get operation works in Hashmap.
map.get(“ram”);
map.get(“shyam”);
map.get(“mohan”);

Get also do the same operation as Put do , get operation also first calculate Hash code of key suppose Hash code of key ram is 1475236 and then it calculate the index from Hash code i.e.  4
And Now it look up at index 4 and matches the hash code , if Hash code matches then it match the key using equals() method and if key matches then it return the value . This operation continue till it get values of all keys.

public class HashmapInternalWorking {

public static void test() {
   int size = 16; // slot size
   Integer[] keys = { 1, 10, 16, 100, 1000, 10000, 2000000, 100000000 };
   HashMap<Integer, Object> map = new HashMap<Integer, Object>();

   for (Integer key : keys) {
       int hash = hash(key.hashCode());
       int i = indexFor(hash, size);

       map.put(key, "[" + hash + " : " + i + "]");
   }

   for (Integer key : map.keySet()) {
       System.out.printf("key: %9s, [inner hash : slot index]: %s,\n", key, map.get(key));
   }
}

static int hash(int h) {
   h ^= (h >>> 20) ^ (h >>> 12);
   return h ^ (h >>> 7) ^ (h >>> 4);
}

static int indexFor(int h, int length) {
   return h & (length - 1);
}
public static void main(String[] args) {
test();
}

}

Thursday, 14 July 2016

Java Code Showing use of Comparator Interface

class School {
private String name;
private int rollNo;
public School(String name,int rollNo) {
this.name = name;
this.rollNo = rollNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRollNo() {
return rollNo;
}
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
@Override
public String toString() {
return "Name : "+name+"\t"+"Roll No : "+rollNo;
}
}
class SortByRollNo implements Comparator<School>{
@Override
public int compare(School s1, School s2) {
return (s1.getRollNo() < s2.getRollNo() ? -1 : (s1.getRollNo() > s2.getRollNo()) ? 1 : 0);
}
}
class SortByName implements Comparator<School>{
@Override
public int compare(School s1, School s2) {
return s1.getName().compareTo(s2.getName());
}
}
public class ComparatorExample {

public static void main(String[] args) {
School s=new School("Pushkar",104);
School s1=new School("Alok",101);
School s2=new School("Sanjay",102);
School s3=new School("Zimmy",103);
List<School> list = new ArrayList<School>();
list.add(s);
list.add(s1);
list.add(s2);
list.add(s3);
System.out.println("SORTING BY ROLL NO : \n");
Collections.sort(list,new SortByRollNo());
Iterator<School> ir = list.iterator();
School sc = null;
while(ir.hasNext()){
sc = ir.next();
System.out.println(sc);
}
System.out.println("SORTING BY NAME : \n");
Collections.sort(list,new SortByName());
Iterator<School> ir1 = list.iterator();
School sc1 = null;
while(ir1.hasNext()){
sc1 = ir1.next();
System.out.println(sc1);
}
}

}