简体   繁体   English

如何从带有对象的hashmap中执行GET方法?

[英]How to do the GET method from a hashmap with objects?

Im trying to get the id of a posto from an hashmap to compare the value in another class: 我试图从一个hashmap获取一个posto的id来比较另一个类中的值:

My class Posto: 我的班级Posto:

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;
    }

My MAIN class where i fill the hashmaps like this: 我的MAIN类,我填充这样的哈希映射:

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));

    }

}

And now my CENTRAL class where i have the method "addPosto" to fill the hashmap and i need the method "getPosto" to get the ids to compare in other class but i can't do it, i'm a little bit confused about the hashmaps. 现在我的CENTRAL类,我有方法“addPosto”来填充hashmap,我需要方法“getPosto”来获取其他类中的ID进行比较,但我不能这样做,我有点困惑哈希图。

    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){
        }***

    }

Your Map has only one value type. 您的地图只有一种值类型。

private final Map<Integer, Posto> postos = new HashMap<>();

And you only add this type. 而且你只添加这种类型。

public void addPosto( Posto p ){
    postos.put(p.getPostoId(), p);
}

so it makes sense to expect this type. 所以期待这种类型是有道理的。

 public Posto getPosto(int id) {
     return postos.get(id);
 }

If you want to leave the Map as it is (which is a bad idea IMHO you can use an explicit cast) 如果你想保持地图原样(这是一个坏主意恕我直言,你可以使用一个明确的演员)

 public Posto getPosto(int id) {
     return (Posto) postos.get(id);
 }

This is needlessly verbose and error prone. 这是不必要的冗长和容易出错的。 At some point doing this will almost certainly lead to a bug which never needed to happen. 在某些时候,这样做几乎肯定会导致一个永远不会发生的错误。

public Posto getPosto (int id)
{
    return postos.get(id);
}

sice you are adding posto class to the hashmap use generics 你将posto类添加到hashmap使用泛型

 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());
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM