CSC/ECE 517 Fall 2007/wiki1 1 aman: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 36: Line 36:
         Set set = h.entrySet();
         Set set = h.entrySet();
         Iterator i = set.iterator();
         Iterator i = set.iterator();
         while (i.hasNext()){ Map.Entry mpentry = (Map.Entry)i.next(); System.out.println(" Name" + mpentry.getKey()); System.out.println(" Age" + mpentry.getValue()); sum += ((Integer)mpentry.getValue()).intValue(); count = count +1;
         while (i.hasNext()){
} int avg = sum/count; System.out.println("Average age is " + avg);
        Map.Entry mpentry = (Map.Entry)i.next();  
System.out.println(" Name" + mpentry.getKey());
                System.out.println(" Age" + mpentry.getValue());
        sum += ((Integer)mpentry.getValue()).intValue();
        count = count +1;
}
        int avg = sum/count;
        System.out.println("Average age is " + avg);
}
}
   }
   }

Revision as of 03:38, 14 September 2007

HashMaps in Java vs hashes in Ruby

Maps in java or hashes in ruby is any unordered object which stores associations between keys and their corresponding values, in which both keys and the values are objects. The key is unique , however the values may not necessarily be unique. The key, is used to retrieve a value from the map at a later stage after having had stored the values.

Comparing HashMaps and Hashes

1. Both in Java and Ruby, data structures are treated as objects. Hence both HashMaps and Hashes are used to store and manipulate objects which can be instantiated using the new method.

  in Java using   : HashMap h = new HashMap; 
  in Ruby using   : h = hash.new

2.In Ruby there is no difference between primitive data types and objects.Hence any variable or object can be put directly into the hashes container and methods can be invoked on them. However in Java primitive objects needs to be type casted to an Object class in order to use in built methods on the created HashMaps.

3.Ruby has the same syntactic sugar for hashes as well as arrays which makes the implementing hashes as simple as implementing an array. In Java however, since HashMaps implement the Collections framework, objects have to be inserted using the method.

  in Java using   : h.put (New Integer(0234), "Peter Drake");
  in Ruby using   : h = {0234 => "Peter Drake"}  

4.Inbuilt iterator feature of ruby enables accessing items one at a time. However in java, Hashmaps cannot implement the Iterator interface. To implement the iteration feature , a collection view of the map for example a Set needs to be used.

  in Java using   : Iterator itr = h.keySet().iterator();
  in Ruby using   : h.each_pair{|key,value| puts key,value, " "}

The following sample code compares simplicity in implementing a HashMap in Java with Hashes in Ruby . The following codes creates a hashmap to associate names of people as keys with their ages as values. The code prints out the keys and values and also finds the average age of the group.

 Java Code : 
 package assgn1;
 import java.util.*;
 public class HashMaps   {
        public static void main(String args[]){
        HashMap h = new HashMap();
        h.put("Tom Sanders", new Integer(32));
        h.put("Dave Patterson", new Integer(46));	
        h.put("Julia Myers", new Integer(14));	
        h.put("Scott Stephenson", new Integer(29));	
        h.put("Betty Matthews", new Integer(40));	
        h.put("Billy Johnson", new Integer(24));   
        int sum =0 , count = 0 ; 
        Set set = h.entrySet();	
        Iterator i = set.iterator();	
        while (i.hasNext()){	
       	Map.Entry mpentry = (Map.Entry)i.next(); 
		System.out.println(" Name" + mpentry.getKey());		
               System.out.println(" Age" + mpentry.getValue()); 	
        	sum += ((Integer)mpentry.getValue()).intValue();	
        	count = count +1;

}

       	int avg = sum/count;	
       	System.out.println("Average age is " + avg);		

}

 }


In version 1.5 of Java ,the generic feature of Java has implemented building type safe collections.In Ruby as well arrays or hashes can be made type safe by modifying ruby classes directly.