Ciaon andbin,
Grazie della risposta. Che LinkedHashmap estende Hasmap mi torna. Ti posto un esempio:
Codice PHP:import java.util.HashMap;import java.util.LinkedHashMap;import java.util.Set;
public class Test2 { public static void main(String[]args){ HashMap<String, String> maps = new HashMap<String, String>(); for(int i = 0; i < 10; i++){ maps.put("name: "+i, "value: " +i); } Test3 t = new Test3(); t.setMaps(maps); Set<String> b = t.getMaps().keySet(); for(String s : b){ System.out.println("value: " +t.getMaps().get(s)); } LinkedHashMap<String, String> linked = new LinkedHashMap<String, String>(); for(int i = 0; i < 10; i++){ linked.put("name: "+i, "value: " +i); } Test3 t2 = new Test3(); t2.setMaps(linked); Set<String> b2 = t2.getMaps().keySet(); for(String s : b2){ System.out.println("value 2: " +t2.getMaps().get(s)); } } }
class Test3{ private HashMap<String, String> maps;
public HashMap<String, String> getMaps() { return maps; }
public void setMaps(HashMap<String, String> maps) { this.maps = maps; }}
Allora nel primo for stampa in maniera randomica, nel secondo for stampa in maniera ordinata (in base all'inserimento che faccio io).
Nel secondo caso ho creato un LinkedHasMap passandolo nel set dell'oggetto Test3. Quando chiamo il get maps dell'instanza Test3, non vado a usare comunque un'HashMap?


Rispondi quotando