thingol 20 de jun. de 2007
protected Hashtable xxx = null ;
...
if ( xxx != null ) {
for ( Iterator iter = xxx .entrySet () .iterator () ; iter.hasNext(); ) {
Map .Entry entry = ( Map .Entry ) iter .next () ;
String A = ( String ) entry .getKey () ;
String B = ( String ) entry .getValue () ;
addSomething ( A , B ) ;
}
}
Hum, não entendi porque no seu código original B é um valor constante (você sempre pega de “packageName”).
thingol 20 de jun. de 2007
Um Hashtable é um Map (assim como HashMap), e implementa todos os métodos de Map.
Usar Map.Entry normalmente costuma ser mais rápido que isso que você fez.
peczenyj 20 de jun. de 2007
thingol 20 de jun. de 2007
Só para encher o saco, em Java 5.0 ficaria:
protected Hashtable < String ,String > xxx = null ;
...
if ( xxx != null ) {
for ( Map .Entry < String ,String > entry : xxx .entrySet ()) {
String A = entry .getKey () ;
String B = entry .getValue () ;
addSomething ( A , B ) ;
}
}
Veja que em Java 5.0 o “iterator” sumiu. O Neal Gafter estava propondo que no Java 7 teríamos algo parecido com:
protected Hashtable < String ,String > xxx = null ;
...
if ( xxx != null ) {
for each ( key , value : xxx .entrySet ()) {
String A = key ;
String B = value ;
addSomething ( A , B ) ;
}
}
Mas provavelmente não vai dar tempo para incluir as modificações no Java 7 (Dolphin). Essa parte de “closures” foi tão polêmica que ele mesmo acha que não vai dar tempo.