通过使用相同的对象创建将具有不同值的新对象添加到哈希图中

时间:2019-01-21 17:16:00

标签: java

Ticket ticketObject = new Ticket(); //I create my object here
HashMap<Integer, Ticket> totalTickets = new HashMap<Integer, Ticket>(); //I create the hasmap
totalTickets.put(counter, ticketObject); // And after i have given values to my object via setters and getters I add i to has map

上面是我的主类,然后,我使用以下命令重新运行main函数:

main(args);

当它尝试将具有不同值的ticketObject添加到hasmap时,它不会这样做。有什么办法吗?

3 个答案:

答案 0 :(得分:0)

哈希表的每个值对象都将映射到一个整数对象。因此,您不能将两个不同的值分配给同一对象。

您必须注意装箱的Integer,因为如果两次创建“ new Integer(1)”,则将得到相同的对象,例如,因为该类具有整数的内部缓存。检查Integer.valueof的方法以更好地了解此行为。

查看本文: https://javapapers.com/java/java-integer-cache/

答案 1 :(得分:0)

public class Ticket {

private String type; // "normal" "reduced"
private String kind; // "online" "printed"
private int duration;
private int trips;
private int serial;

public void constuct( ) {
    type = "";
    kind = "";
    duration = 0;
    trips = 0;
    serial = 0;
}

public String getType() {
    return type;
}

public String getKind() {
    return kind;
}

public int getDuration() {
    return duration;
}

public int getTrips() {
    return trips;
}

public void setType(String newType) {
    type = newType;
}

public void setKind(String newKind) {
    kind = newKind;
}

public void setDuration(int newDuration) {
    duration = newDuration;
}

public void setTrips(int newTrips) {
    trips = newTrips;
}

public int getSerial() {
    return serial;
}

public void setSerial(int serial) {
    this.serial = serial;
}

}

这是我的机票舱位

public class Main {
public static void main(String[] args) {
    int menuChoice;
    int counter = 0;
    Ticket ticketObject = new Ticket();
    SubTicket subTicketObject = new SubTicket();
    Scanner input = new Scanner(System.in);
    Menu menuObject = new Menu();
    HashMap<Integer, Ticket> totalTickets = new HashMap<Integer, Ticket>();
    ticketObject.constuct();
    subTicketObject.constuctt();
    menuObject.printMainMenu();
    menuObject.printChoice();
    menuChoice = input.nextInt();

    switch (menuChoice) {
    case 1:
        menuObject.printChoiceType();
        input.nextLine(); // throw away the /n
        String tmp = input.nextLine();
        if(tmp.equals("normal")) {
            ticketObject.setType("normal");
            menuObject.printChoiceTripsDur();
            String tmp2 = input.nextLine();
            if(tmp2.equals("trips")) {
                menuObject.printTripsAmount();
                int tripsAmount = input.nextInt();
                if(tripsAmount!=1 || tripsAmount != 5 || tripsAmount!=11) {
                    menuObject.printError();
                    break;
                }
                ticketObject.setTrips(tripsAmount);
                System.out.println(ticketObject.getTrips());
            }else if(tmp2.equals("duration")){
                menuObject.printDurationAmount();
                int durationAmount = input.nextInt();
                switch (durationAmount){
                case 1:
                    counter++;
                    ticketObject.setDuration(90);
                    System.out.println("Your ticket serial is " + counter);
                    menuObject.printType();
                    int tmptype = input.nextInt();
                    if(tmptype == 1) {
                        subTicketObject.setType("online");
                    }else if(tmptype == 2){
                        subTicketObject.setType("printed");
                    }else {
                        menuObject.printError();
                    }

                    totalTickets.put(counter, ticketObject);
                    break;
                case 2:
                    counter++;
                    ticketObject.setDuration(1440);
                    System.out.println("Your ticket serial is " + counter);
                    menuObject.printType();
                    int tmptype3 = input.nextInt();
                    if(tmptype3 == 1) {
                        subTicketObject.setType("online");
                    }else if(tmptype3 == 2){
                        subTicketObject.setType("printed");
                    }else {
                        menuObject.printError();
                    }

                    totalTickets.put(counter, ticketObject);
                    break;
                case 3:
                    counter++;
                    ticketObject.setDuration(1080);
                    System.out.println("Your ticket serial is " + counter);
                    menuObject.printType();
                    int tmptype2 = input.nextInt();
                    if(tmptype2 == 1) {
                        subTicketObject.setType("online");
                    }else if(tmptype2 == 2){
                        subTicketObject.setType("printed");
                    }else {
                        menuObject.printError();
                    }

                    totalTickets.put(counter, ticketObject);    
                    break;
                case 4:
                    counter++;
                    subTicketObject.setDuration(43200);
                    menuObject.printAskUserFirstName();
                    input.nextLine(); // throw away the /n
                    String tmpfirstname = input.nextLine();
                    subTicketObject.setFirstname(tmpfirstname);
                    menuObject.printAskUserSecondName();
                    String tmpsecondname = input.nextLine();
                    subTicketObject.setFirstname(tmpsecondname);
                    menuObject.print30DurationPrice();
                    menuObject.printHowToPay();
                    int tmppay = input.nextInt();
                    if(tmppay == 1) {
                        menuObject.printInsertCash();
                    }else if(tmppay == 2) {
                        menuObject.printInsertCard();
                    }else {
                        menuObject.printError();
                    }
                    System.out.println("Your ticket serial is " + counter);
                    menuObject.printType();
                    int tmptype5 = input.nextInt();
                    if(tmptype5 == 1) {
                        subTicketObject.setType("online");
                    }else if(tmptype5 == 2){
                        subTicketObject.setType("printed");
                    }else {
                        menuObject.printError();
                    }
                    totalTickets.put(counter, subTicketObject); 
                    break;
                default:
                    menuObject.printError();
                    break;
                }

            }

        }else if(tmp.equals("reduced")) {
            ticketObject.setType("reduced");
            // gotta ask for "stoixeia" here and save them somewhere


        }else {
            menuObject.printError();
        }

    }
    System.out.println(totalTickets);
    menuObject.printKeepRunning();
    int tmprun = input.nextInt();
    if (tmprun == 1) {
        main(args);
    }

}

}

这是我的主要

public class SubTicket extends Ticket{

private String firstname;
private String secondname;

public void constuctt( ) {
    firstname = "";
    secondname = "";

}

public String getFirstname() {
    return firstname;
}
public void setFirstname(String firstname) {
    this.firstname = firstname;
}
public String getSecondname() {
    return secondname;
}
public void setSecondname(String secondname) {
    this.secondname = secondname;
}

} 这是我的SubTicket,子类

答案 2 :(得分:0)

首先,这个问题不清楚。请确认您是要添加其他计数器作为Key还是要添加相同值的票证对象。 如果您尝试将不同的值映射到相同的键,它将覆盖先前的ticketObject,如下所示。

import java.util.HashMap;

public class TestMap {

    public static void main(String[] args) {

        String[] counterArgs= {"1","2","3"};

        Ticket ticketObject = (new TestMap()).new Ticket();
            // I create my object here
        HashMap<Integer, Ticket> totalTickets = new HashMap<Integer, Ticket>(); 
        for (String counter : counterArgs) {
            ticketObject.setCounter(Integer.valueOf(counter));
            totalTickets.put(Integer.valueOf(counter), ticketObject); 
        }

        totalTickets.entrySet().forEach(e->System.out.println(e.getKey()+"-"+e.getValue()));


    }

    class Ticket {
        public int getCounter() {
            return counter;
        }

        public void setCounter(int num) {
            this.counter = num;
        }

        int counter;
        @Override
        public String toString(){

            return ""+counter;

        }
    }

}

输出:

1-3
2-3
3-3