Java 如何使用对象从hashmap获取方法?
我正在尝试从hashmap获取posto的id,以比较另一个类中的值: 我的班级邮递员:Java 如何使用对象从hashmap获取方法?,java,Java,我正在尝试从hashmap获取posto的id,以比较另一个类中的值: 我的班级邮递员: public class Posto { private int id; private Point posicao; private int capacidade; private int quantidadeAtual; private int gastoMedio; public Posto(int id, Point posicao, int c
public class Posto {
private int id;
private Point posicao;
private int capacidade;
private int quantidadeAtual;
private int gastoMedio;
public Posto(int id, Point posicao, int capacidade, int quantidadeAtual, int gastoMedio) {
this.id = id;
this.posicao = posicao;
this.capacidade = capacidade;
this.quantidadeAtual = quantidadeAtual;
this.gastoMedio = gastoMedio;
}
public int getPostoId() {
return id;
}
public void setPostoId(int id) {
this.id = id;
}
public Point getPostoPosicao() {
return posicao;
}
public void setPostoPosicao(Point posicao) {
this.posicao = posicao;
}
public int getPostoCapacidade() {
return capacidade;
}
public void setPostoCapacidade(int capacidade) {
this.capacidade = capacidade;
}
public int getPostoQuantidadeAtual() {
return quantidadeAtual;
}
public void setPostoQuantidadeAtual(int quantidadeAtual) {
this.quantidadeAtual = quantidadeAtual;
}
public int getPostoGastoMedio() {
return gastoMedio;
}
public void setPostoGastoMedio(int gastoMedio) {
this.gastoMedio = gastoMedio;
}
在我的主类中,我像这样填充哈希映射:
public class Main {
public static void main(String[] args) {
Central c = new Central( new Point(20, 300) );
setupCentral( c );
MenuCentral mc = new MenuCentral( c );
mc.menuPrincipal();
}
private static void setupCentral(Central c) {
//Posto p1 = new Posto(1,new Point(2,3),24,40,30);
c.addPosto(new Posto(1,new Point(10,10),10,200,180));
c.addPosto(new Posto(2,new Point(700,15),15,300,200));
}
}
现在是我的中心类,其中我有一个方法“addPosto”来填充hashmap,我需要一个方法“getPosto”来获取要在其他类中比较的ID,但我不能这样做,我对hashmap有点困惑
public class Central {
private Point posicao;
private Map<Integer, Object> camioes = new HashMap<Integer,Object>( );
private Map<Integer,Object> postos = new HashMap<Integer,Object>( );
public Central(Point posicao) {
this.posicao = posicao;
}
public Point getPosicao() {
return posicao;
}
public void setPosicao(Point posicao) {
this.posicao = posicao;
}
public void addPosto( Posto p ){
postos.put(p.getPostoId(), p);
}
***public int getPosto (int id){
}***
}
公共类中心{
私人点posicao;
私有映射camioes=newhashmap();
privatemappostos=newhashmap();
公共中心(波西卡角){
this.posicao=posicao;
}
公共点getPosicao(){
返回国际民航组织;
}
公共空间塞波西科(波西科角){
this.posicao=posicao;
}
公开作废附加邮政(邮政p){
postos.put(p.getPostoId(),p);
}
***公共int getPosto(int id){
}***
}
您的地图只有一种值类型
private final Map<Integer, Posto> postos = new HashMap<>();
因此,期待这种类型是有道理的
public void addPosto( Posto p ){
postos.put(p.getPostoId(), p);
}
public Posto getPosto(int id) {
return postos.get(id);
}
如果你想让地图保持原样(在我看来这是个坏主意,你可以使用显式强制转换)
这是不必要的冗长和容易出错的。在某种程度上,这样做几乎肯定会导致一个从未发生过的错误。您的映射只有一种值类型
private final Map<Integer, Posto> postos = new HashMap<>();
public Posto getPosto (int id)
{
return postos.get(id);
}
因此,期待这种类型是有道理的
public void addPosto( Posto p ){
postos.put(p.getPostoId(), p);
}
public Posto getPosto(int id) {
return postos.get(id);
}
如果你想让地图保持原样(在我看来这是个坏主意,你可以使用显式强制转换)
这是不必要的冗长和容易出错的。在某种程度上,这样做几乎肯定会导致一个从未发生过的bug。sice您正在向hashmap添加posto类,请使用泛型
public Posto getPosto (int id)
{
return postos.get(id);
}
private Map<Integer, Posto> camioes = new HashMap<Integer,Posto>( );
private Map<Integer,Posto> postos = new HashMap<Integer,Posto>( );
you can iterate the hashmap and get all the values
for (Integer key : camioes.keySet()) {
Posto p = postos.get(key);
System.out.println(p.getPostoId());
}
}
私有映射camioes=newhashmap();
privatemappostos=newhashmap();
您可以迭代hashmap并获取所有值
for(整数键:camioes.keySet()){
postop=postos.get(键);
System.out.println(p.getPostoId());
}
}
sice您正在向hashmap添加posto类,请使用泛型
private Map<Integer, Posto> camioes = new HashMap<Integer,Posto>( );
private Map<Integer,Posto> postos = new HashMap<Integer,Posto>( );
you can iterate the hashmap and get all the values
for (Integer key : camioes.keySet()) {
Posto p = postos.get(key);
System.out.println(p.getPostoId());
}
}
私有映射camioes=newhashmap();
privatemappostos=newhashmap();
您可以迭代hashmap并获取所有值
for(整数键:camioes.keySet()){
postop=postos.get(键);
System.out.println(p.getPostoId());
}
}
只需在地图上调用get
:return postos.get(id)
是否要获取属于该ID的Posto
实例?还是要验证列表中是否存在ID
?或者您想基于其他内容获取ID?我想验证ID是否存在。要验证映射是否有具有指定ID的条目:postos.containsKey(ID)
只需在映射上调用get
:return postos.get(ID)
是否要获取属于该ID的Posto
实例?还是要验证列表中是否存在ID
?或者您想基于其他内容获取ID?我想验证ID是否存在。要验证映射是否有具有指定ID的条目:postos.containsKey(ID)
我已经尝试过了,但是hashmap postos中的get()方法只返回object。错误:类型不匹配:无法从对象转换为int@JohnOak在这种情况下,我建议你再读一遍我答案的第二行。如果您希望值是Posto
而不是Object
,我建议您改为Posto
。是的,但是有没有一种方法可以在不改变hashmap的情况下实现呢?也许我可以用不同的方式填写?@JohnOak我已经更新了我的答案,虽然你可以这样做,但你不应该。嗯,我明白了,你是对的。谢谢,这两种方法都有效我已经尝试过了,但是hashmap postos的get()方法只返回object。错误:类型不匹配:无法从对象转换为int@JohnOak在这种情况下,我建议你再读一遍我答案的第二行。如果您希望值是Posto
而不是Object
,我建议您改为Posto
。是的,但是有没有一种方法可以在不改变hashmap的情况下实现呢?也许我可以用不同的方式填写?@JohnOak我已经更新了我的答案,虽然你可以这样做,但你不应该。嗯,我明白了,你是对的。谢谢,它双向运行错误:类型不匹配:无法从对象转换为int是的,我在那里写了int,而不是Posto,我已经更改了,请原谅,Mealry也尝试过了,它只是不断给我错误。“无法从Object转换到Porto。”错误:类型不匹配:无法从Object转换到int是的,我在那里写了int,而不是Posto,我已经更改了它,请原谅,Mealry也尝试过了,它只是不断给我错误。“无法从对象转换为端口。”