Here is a little hack to let you treat Java Hashtables (mostly) like Tcl Arrays *using TclBlend or Jacl -- Todd Coram
package require java
proc tcljava_hashtable {arrayname javaHashTable} {
upvar $arrayname arr
java::lock $javaHashTable
for {set e [$javaHashTable keys]} {[$e hasMoreElements]} {} {
set key [$e nextElement]
if {[java::instanceof $key String]} {
# Convienence, convert it to a Tcl String...
set key [$key toString]
}
set value [$javaHashTable get $key]
if {[java::instanceof $value String]} {
# Convienence, convert it to a Tcl String...
set value [$value toString]
}
set arr($key) $value
}
trace add variable arr {unset write read} [list traceHashArray $javaHashTable]
}
proc traceHashArray {hash name index op} {
upvar $name arr
switch -- $op {
write {
$hash put $index $arr($index)
}
read {
set value [$hash get $index]
if {[java::instanceof $value String]} {
set value [$value toString]
}
set arr($index) $value
}
unset {
java::unlock $hash
}
}
}You can use it on newly created hashtables or existing ones. It also works with any subclass of Hashtable:
# Associate the array 'props' with the Java System Properties. # tcljava_hashtable props [java::call System getProperties] # Show all of the Java system property keys. puts [array names props] # Add a property (in a Tclish manner) # set props(Hacker) "Todd Coram" puts $props(Hacker) # Make sure Java sees it. # puts [java::call System getProperty Hacker]