How to convert an Entry<String, Integer> element to String?

Asked

Viewed 548 times

2

I’m using:

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

and do some operations after that part.. after I do these operations, I need to place each "object" of the list in a vector position. For example in the first position of the list has: c=30 and I need to place it in a vector position. But I don’t know which type of vector should be and how to change it, because the lista.get(0) return me a Entry<String,Integer>.

The row variable is declared like this:

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

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

  • 1

    Could display variable declaration and initialization fila?

  • 1

    Looking at your other question, I saw that your code here is probably a continuation of that topic. I ask you, wouldn’t it just be doing what mgibsonbr’s answer doesn’t suggest to you? So: Map<String,Integer> quantas = new HashMap<String,Integer>(); and quantas.put("c", 30);?

  • 1

    @Math From what I understood of the question, the doubt is in the final phase - after all the previous process (which includes my answer) has been done. And the answer lies in the interface Map.Entry - use the method getKey() to get the word (String) and the method getValue() to get the count (Integer). Then you can use the text (and number) manipulation functions you want...

  • 1

    @mgibsonbr after much thinking I think you interpreted correctly, I however had not yet managed to understand, rs.. feel free to answer

  • No @Gustavocinque a question I want to know how to convert to String and the other how to separate. I could not take one question to ask the other, because it runs away from the other question.

  • 1

    Fine then. But there are some things that led me to think that it was duplicate... In the first question you did not mark answer, as if they had not answered correctly and continued doubt, in a second part there is a part of that question ("But I do not know what type of vector should be and how to change... "), which defines the first doubt you had. But no fuss, I apologize.

Show 2 more comments

2 answers

2


Like every object in Java, those that implement Map.Entry have a method toString, that in the 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 - and you can’t change the implementation of Entry - the solution is to call the methods getKey and getValue of the same and then format as you want.

If your Entry is parameterized for the classes String and Integer, this means that the call of 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();

Then it’s just a matter of you 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);
  • but my problem that Map.Entry is inside an Array. Trying to do as you thought, gave the error: Symbol: method getKey() Location: variable list of type List<Entry<String,>>

  • a dirty example, but just to make it clear what I want: String first = (String)list.get(0); I know it doesn’t work, but it’s just to make it clearer what my intention is.

  • 2

    Are you sure you followed my example correctly? Well look what I first do lista.get(0) to remove the first Entry from the list, then I do meuEntry.getKey() to remove the String entry. Your error suggests that you tried to do lista.getKey() - which in fact is incorrect. Your example, in a single line, would look like this: String primeiro = lista.get(0).getKey();

0

You can use 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);                                   
  • 1

    "I think" that’s not exactly what the AP is in need of, but the text is a little fuzzy, so I may have been mistaken

  • 2

    Map.Entry is an interface and cannot be instantiated directly (with new). However, once you get an instance of it (the AP already has several, in a list) the way to manipulate it is the same that you suggested (via setValue), but it is worth noting that: 1) this method is optional - so that not all implementation of Map.Entry needs to support it; and 2) if it exists, it modifies the Map original, and only if the entry is still in the Map. According to the documentation: "the behavior of this call is undefined if the mapping has already been removed from the map".

  • just ta kinda confused... but from what I understood I put an answer...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.