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

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
 
mNo edit summary
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
=Compare hashes in Ruby with HashMaps in Java=
=Compare hashes in Ruby with HashMaps in Java=
#<i>Find the best descriptions you can on the Web, and link to them.</i>
Hashes are lookup tables, and are very similar to functions, in a way. You pass something into a Hash, and based on the key passed you receive one value in response. That response never changes unless you either change what you pass into the Hash, or you change the internals of the Hash. In comparison to an array, hashes support any object as a key and hashes provide more flexibility than arrays.
#</i>Then give sample Ruby and Java programs that accomplish the same thing, and analyze which is more elegant and/or shorter.</i>
 
Hashes are lookup tables, and are very similar to functions, in a way. You pass something into a Hash, and you receive one thing from it. That one thing never changes unless you either change what you pass into the Hash, or you change the internals of the Hash.  
 


=Hashes in Ruby=
=Hashes in Ruby=
As you can see below, you declare a Hash with braces. The items to the left of the => are the keys of the Hash, while the items to the right of the => are the values of the Hash. If you pass in one of the keys, the Hash will return the matching value.
As illustrated in the example below, a Hash is declared with braces. The items to the left of the => sign are the keys of the Hash, while the items to the right of the => sign are the values of the Hash. If you pass in one of the keys, the Hash will return the matching value.
 
carLookup = {“Volkswagen"=>"Gti", "Nissan"=>"Altima", "Toyota"=>"Camry", "Scion"=>"tc"}


carLookup = { 'Volkswagen' => 'Gti', 'Nissan' => 'Altima', 'Toyota' => 'Camry', 'Scion' => 'tc' }


=Hashmaps in Java=
=Hashmaps in Java=
A list of key/value pairs is called HashMap and or HashTable in Java. They are under Java's java.util.* classes among other general list type of datatypes
A list of key/value pairs is called a HashMap or HashTable in Java. They are under Java's java.util.* classes among other general list type of datatypes. An example is given below:


  carLookup = new HashMap();
  carLookup = new HashMap();
carLookup.put("Volkswagen", "Gti");
carLookup.put("Volkswagen", "Gti");
carLookup.put("Nissan", "Altima");
carLookup.put("Nissan", "Altima");
carLookup.put("Toyota", "Camry");
carLookup.put("Toyota", "Camry");
carLookup.put("Scion", "tc");
carLookup.put("Scion", "tc");




Like Java's HashMaps, a Ruby Hash is an object.
=Similarities/ Differences=
Unlike Java's HashMap, in a Ruby Hash, you use braces instead of brackets, and you use key=>value to define one key-value pair. There’s a lot of syntactical sugar, for to create a map in Java and you can it by comparing the code.
#Like Java's HashMaps, a Ruby Hash is an object.
#Unlike Java's HashMap, in a Ruby Hash, you use braces instead of brackets, and you use key=>value to define one key-value pair.  
#There’s a lot of syntactical sugar in Ruby, as compared to Java and you see can it by comparing the code.
=Other descriptions on web=
#http://www.brainbell.com/tutorials/java/About_Ruby.htm
#http://www.jroller.com/wireframe/entry/ruby_syntax_for_java_maps




=Example Java Implementation=
=Example Java Implementation=
import java.util.*;
  import java.util.*;
 
   public class HashTest  
   public class HashTest {
  {
  static HashMap carLookup;
    static HashMap carLookup;
  public static void main(String[] args) {
    public static void main(String[] args)  
  carLookup = new HashMap();
    {
 
        carLookup = new HashMap();
  carLookup.put("Volkswagen", "Gti");
        carLookup.put("Volkswagen", "Gti");
  carLookup.put("Nissan", "Altima");
        carLookup.put("Nissan", "Altima");
  carLookup.put("Toyota", "Camry");
        carLookup.put("Toyota", "Camry");
  carLookup.put("Scion", "tc");
        carLookup.put("Scion", "tc");
        if(args.length > 0)
        {
          if (carLookup.get(args[0])!=null) {
          System.out.println("The midsize car for" + args[0] + " is " + carLookup.get(args[0]));
        }
        else
        {
          System.out.println("Car is not a midsize"); 
        }
    }
    else
    {
        System.out.println("Please enter a Car company");
    }
  }


  if(args.length > 0) {
=Example Ruby Implementation=
    if (carLookup.get(args[0])!=null) {
      System.out.println("The midsize car for" + args[0] + " is " + carLookup.get(args[0]));
    } else
{
    System.out.println("Car is not a midsize"); 
    }
  } else {
    System.out.println("Please enter a Car company");
  }


  }
carLookup = { 'Volkswagen' => 'Gti', 'Nissan' => 'Altima', 'Toyota' => 'Camry', 'Scion' => 'tc' }
}
if(ARGV.length > 0)
        if(carLookup[ARGV[0]] !=nil)
                puts "The midsize car for #{ARGV[0]} is #{carLookup[ARGV[0]]}"
        else
                puts "Car is not a midsize"
        end
end


=References=
=References=
Ruby by Example: Concepts and Code by Kevin Baird
Ruby by Example: Concepts and Code by Kevin Baird
http://www.brainbell.com/tutorials/java/About_Ruby.htm
http://www.jroller.com/wireframe/entry/ruby_syntax_for_java_maps

Latest revision as of 18:30, 19 September 2007

Compare hashes in Ruby with HashMaps in Java

Hashes are lookup tables, and are very similar to functions, in a way. You pass something into a Hash, and based on the key passed you receive one value in response. That response never changes unless you either change what you pass into the Hash, or you change the internals of the Hash. In comparison to an array, hashes support any object as a key and hashes provide more flexibility than arrays.

Hashes in Ruby

As illustrated in the example below, a Hash is declared with braces. The items to the left of the => sign are the keys of the Hash, while the items to the right of the => sign are the values of the Hash. If you pass in one of the keys, the Hash will return the matching value.

carLookup = { 'Volkswagen' => 'Gti', 'Nissan' => 'Altima', 'Toyota' => 'Camry', 'Scion' => 'tc' }

Hashmaps in Java

A list of key/value pairs is called a HashMap or HashTable in Java. They are under Java's java.util.* classes among other general list type of datatypes. An example is given below:

carLookup = new HashMap();
carLookup.put("Volkswagen", "Gti");
carLookup.put("Nissan", "Altima");
carLookup.put("Toyota", "Camry");
carLookup.put("Scion", "tc");


Similarities/ Differences

  1. Like Java's HashMaps, a Ruby Hash is an object.
  2. Unlike Java's HashMap, in a Ruby Hash, you use braces instead of brackets, and you use key=>value to define one key-value pair.
  3. There’s a lot of syntactical sugar in Ruby, as compared to Java and you see can it by comparing the code.

Other descriptions on web

  1. http://www.brainbell.com/tutorials/java/About_Ruby.htm
  2. http://www.jroller.com/wireframe/entry/ruby_syntax_for_java_maps


Example Java Implementation

 import java.util.*;
 public class HashTest 
 {
    static HashMap carLookup;
    public static void main(String[] args) 
    {
       carLookup = new HashMap();
       carLookup.put("Volkswagen", "Gti");
       carLookup.put("Nissan", "Altima");
       carLookup.put("Toyota", "Camry");
       carLookup.put("Scion", "tc");
       if(args.length > 0) 
       {
          if (carLookup.get(args[0])!=null) {
          System.out.println("The midsize car for" + args[0] + " is " + carLookup.get(args[0]));
       } 
       else 
       {
          System.out.println("Car is not a midsize");  
       }
    } 
    else 
    {
       System.out.println("Please enter a Car company");
    }
 }

Example Ruby Implementation

carLookup = { 'Volkswagen' => 'Gti', 'Nissan' => 'Altima', 'Toyota' => 'Camry', 'Scion' => 'tc' }
if(ARGV.length > 0)
        if(carLookup[ARGV[0]] !=nil)
                puts "The midsize car for #{ARGV[0]} is #{carLookup[ARGV[0]]}"
        else
                puts "Car is not a midsize"
        end
end

References

Ruby by Example: Concepts and Code by Kevin Baird