How to convert an EntryString element, Integer to String?

2

I'm using:

List<Map.Entry<String,Integer>> lista = new ArrayList<Map.Entry<String,Integer>>(fila);

And I do some operations after that part .. After I do these operations, I need to put each "object" of the list in a vector position. For example in the first position of the list has: c=30 and need to put in a position of the vector. But I do not know what kind of vector should be or how to change, since lista.get(0) returns me a Entry<String,Integer> .

The variable queue is declared as follows:

PriorityQueue<Map.Entry<String,Integer>> fila = 
    new PriorityQueue<Map.Entry<String,Integer>>(4, comp);

Assuming the list looks like this: [c = 30, e = 25, b = 20] my intention is to have a string "c = 30", another string "e = 25" ... and so on.

    
asked by anonymous 26.09.2014 / 01:22

2 answers

2

Like every object in Java, those that implement Map.Entry have a toString method, which in this case (the Entry of a HashMap ) returns something like:

this.getKey().toString() + "=" + this.getValue().toString()

If you want to convert this value to a different string - you can not move the implementation of Entry - the solution is to call the methods getKey and getValue of it and then format as you want. p>

If your Entry is parameterized for the String and Integer classes, this means that calling these methods will return objects of these types (the value may suffer autounboxing for int ) :

Map.Entry<String,Integer> meuEntry = lista.get(0);

String palavra = meuEntry.getKey();
Integer contagem = meuEntry.getValue();
int alternativa = meuEntry.getValue();

That's just a matter of formatting this data however you want! Examples:

System.out.printf("A palavra %s apareceu %d vezes.", palavra, contagem);

String s = String.format("[%d] %s", contagem, palavra);
    
26.09.2014 / 03:04
0

You can use it to add using the following commands, for example:

//no seu caso o objeto é do tipo Map.Entry<String,Integer> 
Map.Entry<String, Integer> objeto = new Map.Entry<String, Integer>();
objeto.setValue(valorObjeto);
//no seu caso listadeObjeto pode ser a variável fila
lista.add(objeto); 
lista.add(posicao,objeto);
lista.addAll(listadeObjetos); //lista.addAll(fila);

You can change using the following commands, for example:

lista.set(posicao,objeto);   

You can remove using the following commands, for example:

lista.remove(objeto);  
lista.remove(posicao,objeto);
lista.removeAll(listadeObjetos);                                   
    
26.09.2014 / 02:34