blah blah

hash, hashmap 비교

사내 게시판에 Ruby script 샘플 소개하면서 이해 돕기위해 hash/HashMap 비교한거.

// <= Java 1.4
HashMap map = new HashMap();
map.put(“apple”, “red”); 
map.put(“banana”, “yellow”);
map.put(“grape”, “purple”);

Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
    Entry entry = (Entry) iter.next();
    System.out.println(“key : ” + entry.getKey() + ” / value : ” + entry.getValue());
}

// >= Java 1.5
Map<String, String> mp = new HashMap<String, String>();
mp.put(“apple”, “red”);
mp.put(“banana”, “yellow”); 
mp.put(“grape”, “purple”);

for (Map.Entry<String, String> entry: mp.entrySet()) {
    System.out.println(“key : ” + entry.getKey() + ” / value :  + entry.getValue());
}

# Ruby
hs = { “apple” => “red”, “banana” => “yellow”, “grape” => “purple” }
hs.each { |key, value| puts “key : #{key} / value : #{value} }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s