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 3: Line 3:


'''Map''' : A map 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.
'''Map''' : A map 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.
'''HashMaps in java''' : In java an AbstractMap class may implement the Map Interface, which maps unique keys to values. The HashMap Class extends this AbstractMap and uses a hash table to store the map. Maps in general do not implement the Iterable interface. Hence iterations through a map are not possible using a for loop and also it is not possible to obtain an iterator to a map. To implement the iteration feature , a collection view of the map for example a set needs to be used .
'''Hashes in ruby''' : Similar to java, in ruby hashes also are an unordered collection of key-value pairs , but unlike in java hashes in ruby are implemented the same way as an array. Also since Ruby has the feature of inbuilt iteration, iterating over hashes can be done with the each, each_value, each_key or each_pair method.


'''Similarities between Hashmaps and Hashes'''
'''Similarities between Hashmaps and Hashes'''

Revision as of 02:15, 14 September 2007

HashMaps in Java vs hashes in Ruby

Map : A map 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.

Similarities between Hashmaps and Hashes

In java data structures are treated as objects.However in Java we need to  to import the
util framework and instantiate it using the new method ,While in ruby this is by default an object.

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.

Difference between Hashmaps and Hashes

1) In Ruby there is no difference between primitive data types and objects.Hence any variable or object can be put directly into 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 arrays or hashmaps.

In Java for when

HashMap hm = new HashMap(); hm.put(new Integer(10), new Integer(40)); In the above example we have used the java.lang framework for wrapping primitive type to Integer Objects. In Ruby: h=Hashes.new{10=>"40"}

2)Ruby has the same syntactic sugar for hashes as well as arrays which makes the implementing hashes as simple as implementing an array. Eg : h=Hash.new{"First"=>A,"Second"=>b,"third"=>c}

While in Java since they implement the collections framework so elements have to be inserted using add or put methods.

     hm.put("Betty Matthews", new Integer(40));

3)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 Ruby

h.each_pair{|key,value| puts key,value, " "}

In Java

Iterator itr = hm.keySet().iterator();


h={"Tom Sanders" => 21,"Billy Johnson" => 29, "Dave Patterson" => 18,"Julia Myers" =>25,"Scott"=>29,"Betty"=>40} puts h.values.min


package datastructures; import java.util.*;

public class Hashmaps {

public static void main(String args[]){ HashMap hm = new HashMap();

hm.put("Tom Sanders", new Integer(32)); hm.put("Billy Johnson", new Integer(24)); hm.put("Dave Patterson", new Integer(46)); hm.put("Julia Myers", new Integer(25)); hm.put("Scott Stephenson", new Integer(29)); hm.put("Betty Matthews", new Integer(40));

int temp;

Iterator itr = hm.keySet().iterator();

temp = ((Integer)hm.get(itr.next())).intValue();

while (itr.hasNext()){ if (temp >((Integer)hm.get(itr.next())).intValue()){

temp = ((Integer)hm.get(itr.next())).intValue(); } } System.out.println(" Lowest value is "+ temp);



}

}