IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Collection et Stream Java Discussion :

[concurrence] iterateurs et Map


Sujet :

Collection et Stream Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Juin 2006
    Messages : 11
    Par défaut [concurrence] iterateurs et Map
    Bonjour, après quelques recherches, je me retrouve dans une impasse.

    J'ai une HashMap que je parcours grace à un Iterator sur ses clefs. J'aimerai pouvoir ajouter un truc à la Map pendant les itérations.

    En gros j'aimerai pouvoir faire un truc du genre :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Map m = new HashMap();
    (...)
    Iterator iterator = m.keySet().iterator();
    while (iterator.hasNext()){
      String s = (String)iterator.next();
      if (s.equals("uneChaine")){
        m.put("uneClef", uneValeur);
      }
    }
    Au m.put, j'ai une ConcurrentModificationException, ce qui est tout à fait normal.

    Mais j'ai absolument besoin d'ajouter un truc dans cette map dans certains cas.

    Existe-il des implémentations qui le permettent ?

    Merci d'avance.

  2. #2
    Expert éminent
    Avatar de adiGuba
    Homme Profil pro
    Développeur Java/Web
    Inscrit en
    Avril 2002
    Messages
    13 938
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java/Web
    Secteur : Transports

    Informations forums :
    Inscription : Avril 2002
    Messages : 13 938
    Billets dans le blog
    1
    Par défaut
    Salut,

    Citation Envoyé par Aquaphobe
    Existe-il des implémentations qui le permettent ?
    Je ne sais pas... mais je pense que non.

    A mon avis la meilleure solution dans ton cas serait d'utiliser une Map temporaire dans laquelle tu feras tous les ajouts, et que tu ajouteras à la Map initial à la fin de l'itération :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
            Iterator iterator = m.keySet().iterator();
            Map tmp = new HashMap();
            while (iterator.hasNext()){
                String s = (String)iterator.next();
                if (s.equals("uneChaine")){
                    tmp.put("uneClef", uneValeur);
                }
            }
            m.putAll(tmp);
    a++

  3. #3
    Membre émérite Avatar de g_rare
    Profil pro
    Inscrit en
    Novembre 2005
    Messages
    608
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2005
    Messages : 608
    Par défaut
    D'accord avec adiGuba, car après un scan rapide (de la javadoc) : HashMap, Hashtable, TreeMap, IdentityHashMap et WeakHashMap sont toutes des implémentations "fail-fast" !

    The iterators returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

    Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.

Discussions similaires

  1. Réponses: 10
    Dernier message: 17/06/2013, 22h29
  2. Acces concurrent sur une map
    Par boo64 dans le forum Collection et Stream
    Réponses: 4
    Dernier message: 03/03/2011, 11h34
  3. Comment gérer l'accès concurrent de threads à une map STL C++ ?
    Par masterx_goldman dans le forum Windows
    Réponses: 1
    Dernier message: 15/09/2009, 15h55
  4. Mapping Collection et accès concurrent
    Par Royd938 dans le forum Hibernate
    Réponses: 1
    Dernier message: 22/05/2008, 11h04
  5. iterateurs de map
    Par ocean24 dans le forum SL & STL
    Réponses: 2
    Dernier message: 03/05/2007, 19h18

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo