Duvida MAp ler seu conteudo....java.util.ConcurrentModificationException

2 respostas
P

Folks,

estou tentando ler o conteudo map através da chave do map
mas está dando erro.

Alguém pode me ajudar…

abs

public  static void main (String[] args){
	    	 int max = 5;
	    	 Integer codigo=0;
	    	 Map m = new Cache(max);
	    	 
	    	 m.put(new Integer(98),  new String("B98"));
	    	 m.put(new Integer(100), new String("P"));
	    	 m.put(new Integer(101), new String("P1"));
	    	 m.put(new Integer(1),   new String("A1"));
	    	 m.put(new Integer(2),   new String("B1"));
	    	 m.put(new Integer(3),   new String("C1"));
	    	 m.put(new Integer(99),  new String("B100"));
	    	 m.put(new Integer(98),  new String("B101"));
	    	 m.put(new Integer(98),  new String("B98"));
	    	 
	    	 Iterator i = m.keySet().iterator();
			 while (i.hasNext()) {
				 codigo = (Integer) i.next();
				 System.out.println("codigo = "+codigo);
				 String conteudo = (String)m.get(codigo);
				 System.out.println("valor="+conteudo.toString());
				
			 }	 
	     }
} 

resultado:

codigo = 100
valor=P
Exception in thread "main" java.util.ConcurrentModificationException
	at java.util.LinkedHashMap$LinkedHashIterator.nextEntry(Unknown Source)
	at java.util.LinkedHashMap$KeyIterator.next(Unknown Source)
	at com.cert.LinkedHashMap.Cache.main(Cache.java:40)

2 Respostas

P

ufa consegui o que queria mas um aduvida tem como eu saber qual

o codigo da chave do map que estou lendo do values…

Isto é quando mostro o conteudo na linha

String conteudo = (String)i.next();

queria saber qual a chave desse conteudo para o conteudo B98 trazer 
a chave 98 ...


se alguém puder me ajudar agradeceria....



     public  static void main (String[] args){
	    	 int max = 5;
	    	 Integer codigo=0;
	    	 Map m = new Cache(max);
	    	 
	    	 m.put(new Integer(98),  new String("B98"));
	    	 m.put(new Integer(100), new String("P"));
	    	 m.put(new Integer(101), new String("P1"));
	    	 m.put(new Integer(1),   new String("A1"));
	    	 m.put(new Integer(2),   new String("B1"));
	    	 m.put(new Integer(3),   new String("C1"));
	    	 m.put(new Integer(99),  new String("B100"));
	    	 m.put(new Integer(98),  new String("B101"));
	    	 m.put(new Integer(98),  new String("B98"));
	    	 
	    	 Iterator i = m.values().iterator();
	    	 
	    	 while(i.hasNext()){
	    		 String conteudo = (String)i.next();
	    		 System.out.println("conteudo="+conteudo);
	    		 
	    		 
	    	 }

	     }
	     
	     resultado :
	     
	     conteudo=P
	     conteudo=P1
	     conteudo=A1
	     conteudo=B1
	     conteudo=C1
	     conteudo=B100
	     conteudo=B98
P

Folks,

porque dessa forma da erro

e não consigo ler o conteudo do MAP acessando atraves

da chave…

package com.cert.LinkedHashMap;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

public class Cache extends java.util.LinkedHashMap {
	     // poderia ser final:
	     protected int maxsize;
	 
	     public Cache(int maxsize) {
	         super(maxsize, 0.75f, true);
	         this.maxsize = maxsize;
	     }
	 
	     /**
	      * Faz com que o elemento mais velho
	      * seja removido no caso do tamanho ser atingido
	      */
	     protected boolean removeEldestEntry() {
	         return (this.size() > maxsize);
	     }
	     
	     public  static void main (String[] args){
	    	 int max = 5;
	    	 Integer codigo=0;
	    	 Map m = new Cache(max);
	    	 
	    	 m.put(new Integer(98),  new String("B98"));
	    	 m.put(new Integer(100), new String("P"));
	    	 m.put(new Integer(101), new String("P1"));
	    	 m.put(new Integer(1),   new String("A1"));
	    	 m.put(new Integer(2),   new String("B1"));
	    	 m.put(new Integer(3),   new String("C1"));
	    	 m.put(new Integer(99),  new String("B100"));
	    	 m.put(new Integer(98),  new String("B101"));
	    	 m.put(new Integer(98),  new String("B98"));
	    	 
	    	 Iterator i = m.keySet().iterator();
	    	 while(i.hasNext()){
	    		 
	    		 Integer chave = (Integer)i.next();
	    		 System.out.println("chave="+chave);
	    		 
	    		 String conteudo= (String) m.get(chave) ;
	    		
	    		 System.out.print("    conteudo="+conteudo);
	    	 }

	     }
} 

resultado :

chave=100
    conteudo=PException in thread "main" java.util.ConcurrentModificationException
	at java.util.LinkedHashMap$LinkedHashIterator.nextEntry(Unknown Source)
	at java.util.LinkedHashMap$KeyIterator.next(Unknown Source)
	at com.cert.LinkedHashMap.Cache.main(Cache.java:50)
Criado 15 de agosto de 2008
Ultima resposta 15 de ago. de 2008
Respostas 2
Participantes 1