How to set the key and value of an object creating an instance and put the values in the map?

Asked

Viewed 1,148 times

0

I only know how to hashmap in a way that is creating an instance in the put method to insert values.

Follow the way I do:

import java.util.Map;

public class Teste {

public static void main(String[] args){

//Exemplo com utilização de hashmap

    Map<Pessoa, Pessoa> example = new HashMap<Pessoa, Pessoa>();



    example.put(new Pessoa(12), new Pessoa("Aline"));
    example.put(new Pessoa(13), new Pessoa("Carla"));

    int key = 2;

    if(example.containsKey(key)){
        System.out.println("Valor é:" + key + " = " + example.get(key));

    }else{
        System.out.println("Não existe!");
    }
  }
}

But I couldn’t do it that way as you do it? example:

import java.util.Map;

public class Teste {

    public static void main(String[] args){



    //Exemplo com utilização de hashmap

        Map<Pessoa, Pessoa> example = new HashMap<Pessoa, Pessoa>();

        Pessoa pessoa;

        pessoa = new Pessoa();
        pessoa.setId(40);
        pessoa.setNome("Aline");

        example.put(pessoa.getId(), pessoa.getNome());

        int key = 2;

        if(example.containsKey(key)){
            System.out.println("Valor é:" + key + " = " + example.get(key));

        }else{
            System.out.println("Não existe!");
        }
    }




Class Pessoa:

package ibm;

public class Pessoa {
private Integer id;
private String nome;

Pessoa(Integer num){
 this.id    = num;
}

Pessoa(String nome){
    this.nome = nome;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

}

  • If name is string and id is int, why didn’t you declare HashMap<String, Integer>() instead of using Person? It’s unclear what you’re trying to do.

  • When you create a new class you are allocating a pointer to the data, maybe you want to make a map<int, Person>. It really wasn’t very clear the question

  • I just want to do the implementation in a way that doesn’t need to create an instance. I want to take values already defined in an object and insert.

1 answer

4

The key and value types of your Map do not match what you are trying to insert into it.

Map is a type of collection that holds keys and values, with the goal of you locating a value through your key. In case you want the key to be an integer (id) and the value to be the name of the person (String). Then your Map should reflect this. In other words...

This line:

Map<Pessoa, Pessoa> example = new HashMap<Pessoa, Pessoa>();

should be exchanged for:

Map<Integer, String> example = new HashMap<Integer, String>();

or better still, by:

Map<Integer, String> example = new HashMap<>();

UPDATING: On second thought, maybe you want to do a little different and locate a Person through your id. In that case you should declare your Map like this:

Map<Integer, Pessoa> example = new HashMap<>();

and change your put() thus:

example.put(pessoa.getId(), pessoa);

Once done, you can see what the name of the person is like:

Pessoa pessoa = example.get(id);
System.out.println("Nome: " + pessoa.getNome());
  • but in Pessoa I am inserting an id.

  • 3

    And what does that have to do with what I said? Yours put(pessoa.getId(), pessoa.getNome()) is correct, what you need to exchange is the declaration of your map of Map<Pessoa, Pessoa> for Map<Integer, String>. Because getId() returns an integer and getNome() returns a String, your map should be compatible with this, you should save integers pointing to Strings.

  • I’ll try that

  • Well, I’ll try to explain, the id is an Integer and the name is a String so what this described there is you will create an Integer (id) key map that will save Strings (name), ie, Map<int, String>()

  • 1

    @user3010128 Correct, the only caveat is that instead of int which is a primitive type has to be the class Integer, but I think you already know that.

  • On second thought, you might want to do a little different and locate a Person through your id. I updated the answer explaining how to do.

Show 1 more comment

Browser other questions tagged

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