CSC/ECE 517 Summer 2008/wiki1 6 jm: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
 
(43 intermediate revisions by one other user not shown)
Line 1: Line 1:
== Introduction ==
== Introduction ==
Both Ruby and Java support arrays and hash tables
Both the Ruby and Java support Arrays as part of the language. Ruby supports Hashtables as part of the language but Java does not. Hashtables are supported in Java using the java.util library. In this article I will compare the use of Arrays and hashtables in Ruby and Java.


== Hashes in Ruby ==
== Arrays and Hashtables ==


== Arrays in Ruby ==
Arrays and Hashtables are basic data structures that are present in some form in almost all programming languages.
 
Arrays provide a way to store a list of objects and access the object based on it's position in the list. Access to the array elements is very fast if the programmer knows the value of the index where the item is located. Access to the array elements may be slow if the index is not known. This may require the programmer to search a large list of items resulting in poor performance.


=== Creating Arrays in Ruby ===
Hashtables are similar to arrays except the index value is typically a string or other object. Hashtable look up performance is generally  very good. The contents of a hashtable are usually not ordered.


Creating an array in Ruby is as simple as
== Array Comparison ==


  a = []
=== Basic Array Operation Comparison ===


The following table summarizes some of the basic array operations in Ruby and Java


=== Initializing an Array in Ruby ===
{| border="1"
 
|-valign="top"
  a = ["one",45,"hello"]
|width="10%"|'''Array Function  '''
|width="45%"|'''Ruby'''
|width="45%"|'''Java'''
|-
|'''Create'''
|<code>h = []</code> or
<code>h = Array.new</code>
|<code>String[] h = new String[3];</code>
|-
|'''Initialize'''
|<code>h = ["one",1,"two"]</code>
|<code>String[] h = {"one","two"};</code>
|-
|'''Element Access'''
|<code>h[0]</code>
|<code>h[0]</code>
|-
{|
 
<br>
<br>
 
When comparing Ruby and Java one of the biggest differences in the syntax and use of the languages is due to the 'Typing' of the languages.
 
The Java language is '''Strong Typed''' while Ruby is '''Dynamically Typed'''. This is apparent when comparing the Array declaration syntax between the two languages.
 
<br><br>'''Declaring and populating an Array in Ruby:'''
<pre>a = Array.new
a[0] = "one"
a[1] = "two"
</pre>
<br>
'''Declaring the same array in Java'''
<pre>String[] a = new String[2];
a[0] = "one";
a[1] = "two";
</pre>
 
Note that when we create the array in Java we have to define the type of elements in the array as well as the size.
 
Ruby will allow us to store any object in the array as well as different types in the same array.
<br>
'''We can create an array of different types using Ruby'''
<pre>a = [1,"one",1.99]</pre>
<br>
If we want to increase the size of the array in the previous example we just use the following:
<pre>a[20] = "new item"</pre>
Items a[2] through a[19] are set to nil
 
There are a large number of methods defined for the Ruby Array class. These methods make it very easy to use the Array class as the base for more specific data structures like stacks, lists and queues.
 
'''Implementing a Stack in Ruby'''
<pre>stack = []
stack.push("item1")  // add item to the stack
stack.pop            // pop the item from the stack
</pre>
 
To implement the Stack in Java we will have to import the java.util.Stack class


===Summary===
The array implementations of Ruby and Java are similar. The main differences are due to the typing differences in the languages. The Ruby Array class is more flexible than the Java classes because the programmer does not have to concern herself with allocating space and restricting the data types stored in the array.


== Hashes in Java ==
== Hashtable Comparison ==


== Arrays in Java ==
=== Basic Hashtable Operation Comparison ===


Hasttables are related to arrays with the difference being that keys used in hashtables are not restricted to integers.<br>
The following table compares some of the basic hashtable functions between Ruby and Java


{| border="1" cellpadding="4"
{| border="1" cellpadding="4"
Line 36: Line 100:
|'''Initialize'''
|'''Initialize'''
|<code>h = {"key1" => "value1", "key2" => "value2" }</code>
|<code>h = {"key1" => "value1", "key2" => "value2" }</code>
| TO DO
| Not Available
|-
|-
|'''Add Key to Existing Hashtable'''
|'''Add Key to Existing Hashtable'''
|<code>h["key3"] = "value3"</code>
|<code>h["key3"] = "value3"</code>
| To Do
|<code>h.put("key3",new String("value3"));</code>
|-
|-
|'''Remove a Key - Value pair'''
|'''Remove a Key - Value pair'''
|<code>h.delete("key1")</code>
|<code>h.delete("key1")</code>
|to do
|<code>h.remove</code>
|-
|-
|'''Check if Key Exists'''
|'''Check if Key Exists'''
|<code>h.key?("key1")</code>
|<code>h.key?("key1")</code>
|to do
|<code>h.containsKey("key1")</code>
|-
|-
|'''Check if Value Exists'''
|'''Check if Value Exists'''
|<code>h.value?("value1")</code>
|<code>h.value?("value1")</code>
|to do
|<code>h.contains(new String("value1"));</code>
|-
|-
|'''Retrieve a Value Given a Key'''
|'''Retrieve a Value Given a Key'''
|<code>h["key1"]</code>
|<code>h["key1"]</code>
|to do
|<code>h.get("key1")</code>
|-
|-
{|
{|
<br>
One of the biggest differences that arises between hashtable usage in Java and Ruby is that the the value data stored in the hashtable in Java is type of object. The data in the Ruby hashtable is also an object but since Ruby is dynamically typed we don't have to worry about casting the Ruby object to get a useful data type.
<br>
When we add a value to a hashtable using Java we have to wrap it in it's specific datatype like this:
<pre>h = new Hashtable();
h.put("key3",new String("value3"));</pre>
When we retrieve the value from a Java hashtable we have to down cast the value as well like this:
<pre>String val = (String).get("key1")</pre>
When doing the same in Ruby we don't have to be concerned with the data types so the syntax is much more readable:
<pre>h = {"key3" => "value3"}
h["key3"]</pre>
===Summary===
From the programmers perspective, using hashtables is simpler in Ruby than in Java. Again, this is primarily due to the differences between a dynamic and static typed language. When programming with hashtables in Ruby the programmer does not have to put as much thought into the mechanics of writing the code, she can concentrate on the use of the data structure instead of the implementation.


==External Links ==
==External Links ==
[http://www.javadeveloper.co.in/java-example/java-hashtable-example.html Java Hastable Examples]
[http://www.javadeveloper.co.in/java-example/java-hashtable-example.html Java Hastable Examples]
<br>
[http://java.sun.com/j2se/1.4.2/docs/api/java/util/Hashtable.html Java Hashtable Documentation]
<br>
[http://tech.rufy.com/2006/06/ruby-vs-java-matter-of-taste_09.html Ruby vs. Java A Matter of Taste]
<br>
[http://en.wikipedia.org/wiki/Associative_array More About Associative Arrays]
<br>
[http://www.ruby-doc.org/core/ Ruby Documentation]

Latest revision as of 17:46, 20 September 2009

Introduction

Both the Ruby and Java support Arrays as part of the language. Ruby supports Hashtables as part of the language but Java does not. Hashtables are supported in Java using the java.util library. In this article I will compare the use of Arrays and hashtables in Ruby and Java.

Arrays and Hashtables

Arrays and Hashtables are basic data structures that are present in some form in almost all programming languages.

Arrays provide a way to store a list of objects and access the object based on it's position in the list. Access to the array elements is very fast if the programmer knows the value of the index where the item is located. Access to the array elements may be slow if the index is not known. This may require the programmer to search a large list of items resulting in poor performance.

Hashtables are similar to arrays except the index value is typically a string or other object. Hashtable look up performance is generally very good. The contents of a hashtable are usually not ordered.

Array Comparison

Basic Array Operation Comparison

The following table summarizes some of the basic array operations in Ruby and Java

Array Function Ruby Java
Create h = [] or

h = Array.new

String[] h = new String[3];
Initialize h = ["one",1,"two"] String[] h = {"one","two"};
Element Access h[0] h[0]


When comparing Ruby and Java one of the biggest differences in the syntax and use of the languages is due to the 'Typing' of the languages. The Java language is Strong Typed while Ruby is Dynamically Typed. This is apparent when comparing the Array declaration syntax between the two languages.

Declaring and populating an Array in Ruby:
a = Array.new
a[0] = "one"
a[1] = "two"


Declaring the same array in Java

String[] a = new String[2];
a[0] = "one";
a[1] = "two";

Note that when we create the array in Java we have to define the type of elements in the array as well as the size.

Ruby will allow us to store any object in the array as well as different types in the same array.
We can create an array of different types using Ruby

a = [1,"one",1.99]


If we want to increase the size of the array in the previous example we just use the following:

a[20] = "new item"

Items a[2] through a[19] are set to nil

There are a large number of methods defined for the Ruby Array class. These methods make it very easy to use the Array class as the base for more specific data structures like stacks, lists and queues.

Implementing a Stack in Ruby

stack = []
stack.push("item1")  // add item to the stack
stack.pop            // pop the item from the stack

To implement the Stack in Java we will have to import the java.util.Stack class

Summary

The array implementations of Ruby and Java are similar. The main differences are due to the typing differences in the languages. The Ruby Array class is more flexible than the Java classes because the programmer does not have to concern herself with allocating space and restricting the data types stored in the array.

Hashtable Comparison

Basic Hashtable Operation Comparison

Hasttables are related to arrays with the difference being that keys used in hashtables are not restricted to integers.
The following table compares some of the basic hashtable functions between Ruby and Java

Hashtable Function Ruby Java
Create h = {} or

h = Hash.new

h = new Hashtable();
Initialize h = {"key1" => "value1", "key2" => "value2" } Not Available
Add Key to Existing Hashtable h["key3"] = "value3" h.put("key3",new String("value3"));
Remove a Key - Value pair h.delete("key1") h.remove
Check if Key Exists h.key?("key1") h.containsKey("key1")
Check if Value Exists h.value?("value1") h.contains(new String("value1"));
Retrieve a Value Given a Key h["key1"] h.get("key1")

One of the biggest differences that arises between hashtable usage in Java and Ruby is that the the value data stored in the hashtable in Java is type of object. The data in the Ruby hashtable is also an object but since Ruby is dynamically typed we don't have to worry about casting the Ruby object to get a useful data type.
When we add a value to a hashtable using Java we have to wrap it in it's specific datatype like this:
h = new Hashtable();
h.put("key3",new String("value3"));

When we retrieve the value from a Java hashtable we have to down cast the value as well like this:

String val = (String).get("key1")

When doing the same in Ruby we don't have to be concerned with the data types so the syntax is much more readable:

h = {"key3" => "value3"}
h["key3"]

Summary

From the programmers perspective, using hashtables is simpler in Ruby than in Java. Again, this is primarily due to the differences between a dynamic and static typed language. When programming with hashtables in Ruby the programmer does not have to put as much thought into the mechanics of writing the code, she can concentrate on the use of the data structure instead of the implementation.

External Links

Java Hastable Examples
Java Hashtable Documentation
Ruby vs. Java A Matter of Taste
More About Associative Arrays
Ruby Documentation