Skip to content

Map – Java, Scala, Ruby

December 25, 2015

begin
names = Hash.new

startTime = Time.now

(0..200000).each do |i|
#puts “adding #{i} to the list.”
# pushes the i variable on the *end* of the list
names[i] = “Jagan”+i.to_s
end

names.each_pair {|key,value| puts “#{key} = #{value}”}
endTime = Time.now

p startTime
p endTime
end

 

 

package com.jasokan.test;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

public class TestMap {

public static void main(String[] args) {
Map<Integer, String> nameMap = new HashMap<Integer, String>();

Calendar startTime = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(“HH:mm:ss”);

for (int i = 0; i < 200000; i++) {
nameMap.put(new Integer(i), “Jagan” + i);
}

for (Integer name : nameMap.keySet()) {

String key = name.toString();
String value = nameMap.get(name).toString();
System.out.println(key + ” ” + value);

}

Calendar endTime = Calendar.getInstance();
System.out.println(sdf.format(startTime.getTime()));
System.out.println(sdf.format(endTime.getTime()));
}

}

 

 

import java.util.Calendar

object TestMap {
def main(args: Array[String]) {

var names: Map[Int, String] = Map()

val startTime = Calendar.getInstance().getTime()

for (i <- 1 to 200000) {
names += (i -> (“Jagan” + i.toString()));

}
names.keys.foreach { i =>
print(“Key = ” + i)
println(” Value = ” + names(i))
}

val endTime = Calendar.getInstance().getTime()

println(startTime)
println(endTime)

}
}

Run the programs to get the time taken 🙂

But an interesting problem to observe

Please watch the Creation and printing of Map in Scala!

 

 

 

Advertisements
No comments yet

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

%d bloggers like this: