alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (hash2.scala)

This example Scala source code file (hash2.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

a, array, array, cell, console, console, hashmap, hashmap, none, none, object, some, t

The Scala hash2.scala source code

/* The Computer Language Shootout 
   http://shootout.alioth.debian.org/
   contributed by Isaac Gouy (Scala novice)
*/

import scala.collection.mutable.HashMap;

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

      def printValue[A,B](table: HashMap[A,Cell[B]], key: A) = 
         table get(key) match {
            case Some(c) => Console print(c value);
            case None => Console print(None)
      }

      var n = toPositiveInt(args);
      var nKeys = 10000;

      val table1 = new HashMap[String,Cell[Int]]();
      val table2 = new HashMap[String,Cell[Int]]();

      for (val i <- Iterator.range(0,nKeys)) 
         table1 += (("foo_" + i) -> new Cell(i));


      while (n>0) {
         for (val each <- table1.iterator){
            val key = each._1;
            val c1 = each._2;

            table2 get(key) match {
               case Some(c2) => 
                  c2.value = c2.value + c1.value;
               case None => 
                  table2 += (key -> new Cell(c1.value));
            }
         }
         n = n-1;
      }

      printValue(table1,"foo_1");    Console print(" ");
      printValue(table1,"foo_9999"); Console print(" ");
      printValue(table2,"foo_1");    Console print(" ");
      printValue(table2,"foo_9999"); Console print("\n");

   }


   private def toPositiveInt(s: Array[String]) = {
      val i = 
         try { Integer.parseInt(s(0)); } 
         catch { case _ => 1 }
      if (i>0) i; else 1;
   }
}


private class Cell[T](v: T) extends Object { 
   var value: T = v; 
}


Other Scala examples (source code examples)

Here is a short list of links related to this Scala hash2.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.