JAVA SET MAP
Résolu
Bonjour,
Voila je voulais savoir si quelqu'un connaissait la syntaxe afin de remplir un Set et une Map directement lors de son instanciation (si c'est possible). Donc sans passer dans une méthode de remplissage.
Set<Integer> setAge = new HashSet<Integer>(); ????
Genre : Set<Integer> setAge = new HashSet<Integer>(3){1,2,3};
ET
Map<String, Integer> mapNomAge = new HashMap<String, Integer>(); ????
Genre : Map<String, Integer> mapNomAge = new HashMap<String, Integer>(2){("hello",2),("salut"),10};
Merci d'avance
Salutation
Alex
Voila je voulais savoir si quelqu'un connaissait la syntaxe afin de remplir un Set et une Map directement lors de son instanciation (si c'est possible). Donc sans passer dans une méthode de remplissage.
Set<Integer> setAge = new HashSet<Integer>(); ????
Genre : Set<Integer> setAge = new HashSet<Integer>(3){1,2,3};
ET
Map<String, Integer> mapNomAge = new HashMap<String, Integer>(); ????
Genre : Map<String, Integer> mapNomAge = new HashMap<String, Integer>(2){("hello",2),("salut"),10};
Merci d'avance
Salutation
Alex
2 réponses
-
ok merci,
Mais la par exemple pour le hashset, il y a un constructeur comme ceci :
HashSet(Collection<? extends E> c)
Constructs a new set containing the elements in the specified collection.
Mais alors la niveau syntaxe je ne comprend plus rien et il n y a pas d'exemple
Avez vous deja eu a faire a ca ?-
-
ModérateurEn fait ça veut dire que tu peux faire :
ArrayList<String> liste = new ArrayList<String>(); liste.add("a"); liste.add("b"); ... HashSet<String> set = new HashSet<String>(liste); HashMap<String,Integer> map1 = new HashMap<String,Integer>(); map1.put("a",1); map1.put("b",2); ... HashMap<String,Integer> map2 = new HashMap<String,Integer>(map1); -
-
-
Modérateur