将我的对象返回到HashSet

时间:2017-12-19 17:54:50

标签: java collections

我几乎完全失明了,我正在努力学习Java。我告诉你这是因为我很难理解概念,因为我的残疾。

我的问题: 在这个特定的课程中,我将返回一个我希望存储在某个地方的对象,以便稍后调用。我不熟悉Collections,但我相信(我认为?)HashSetSet的类型,我想在这里实施。我是否能够这样做,并在创建后从另一个类调用这个对象集合?

我对Java非常陌生,所以我不太确定我是否有效地解释了我的问题。请不要犹豫,要求澄清!

重要提示:我不允许airCode的任何重复,HashSet会以某种方式处理吗?

public class A8AirlineAircraftData {


private String airName; //these are all showing a warning saying 'Field ____ can be final'
private String airCode;
private String airCraft;
private int firstClass;
private int busiClass;
private int econClass;
//public String toStringAirLine(){return "airname is:" + airName + ", aircode is: "+airCode;}


public A8AirlineAircraftData(String airName, String airCode, String airCraft, int firstClass, int busiClass, int econClass) {
    this.airName = airName;
    this.airCode = airCode;
    this.airCraft = airCraft;
    this.firstClass = firstClass;
    this.busiClass = busiClass;
    this.econClass = econClass;
} 

public static A8AirlineAircraftData AddAirline(Scanner sc) {
    sc.nextLine();
    System.out.println("Please enter the Airline name:");
    String airName = sc.nextLine();

    System.out.println("Please enter the Airline code:");
    String airCode = sc.nextLine();
    System.out.println("Please enter the Delta Aircraft:");
    String airCraft = sc.nextLine();
    System.out.println("Please enter the first class seat capacity:");
    int firstClass = sc.nextInt();
    System.out.println("Please enter the business class seat capacity:");
    int busiClass = sc.nextInt();
    System.out.println("Please enter the economy class seat capacity:");
    int econClass = sc.nextInt();
    System.out.println("Airline name: " + airName);
    System.out.println("Airline code: " + airCode);
    System.out.println("Delta Aircraft: " + airCraft);
    //Splitting the first word from the rest of the string
    String arr[] = airCraft.split(" ", 2);
    String firstWord = arr[0];
    System.out.println(firstWord + " first class seat capacity: " + firstClass);
    System.out.println(firstWord + " business class seat capacity: " + busiClass);
    System.out.println(firstWord + " economy class seat capacity: " + econClass);
    //Airline object
    A8AirlineAircraftData airline = new A8AirlineAircraftData(airName, airCode, airCraft, firstClass, busiClass, econClass);
    System.out.println(airName + " successfully added. Press Enter to continue.");

    sc.nextLine();
    sc.nextLine();//Press Enter to continue 
    return airline;

}   
}

感谢您提供的任何指导。再一次,如果我不清楚,或者我的提交在语法上不正确,我很抱歉。我试着简洁!

我现在将展示这些变化 编辑:

public class A8AirlineAircraftData {

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((airCode == null) ? 0 : airCode.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (!(obj instanceof A8AirlineAircraftData)) {
        return false;
    }
    A8AirlineAircraftData other = (A8AirlineAircraftData) obj;
    return Objects.equals(airCode, other.airCode);
}

private String airName; //these are all showing a warning saying 'Field ____ can be final'
private String airCode;
private String airCraft;
private int firstClass;
private int busiClass;
private int econClass;
//public String toStringAirLine(){return "airname is:" + airName + ", aircode is: "+airCode;}

public A8AirlineAircraftData(String airName, String airCode, String airCraft, int firstClass, int busiClass, int econClass) {
    this.airName = airName;
    this.airCode = airCode;
    this.airCraft = airCraft;
    this.firstClass = firstClass;
    this.busiClass = busiClass;
    this.econClass = econClass;
}

public static A8AirlineAircraftData AddAirline(Scanner sc) {
    sc.nextLine();
    System.out.println("Please enter the Airline name:");
    String airName = sc.nextLine();

    System.out.println("Please enter the Airline code:");
    String airCode = sc.nextLine();
    System.out.println("Please enter the Delta Aircraft:");
    String airCraft = sc.nextLine();
    System.out.println("Please enter the first class seat capacity:");
    int firstClass = sc.nextInt();
    System.out.println("Please enter the business class seat capacity:");
    int busiClass = sc.nextInt();
    System.out.println("Please enter the economy class seat capacity:");
    int econClass = sc.nextInt();
    System.out.println("Airline name: " + airName);
    System.out.println("Airline code: " + airCode);
    System.out.println("Delta Aircraft: " + airCraft);
    //Splitting the first word from the rest of the string
    String arr[] = airCraft.split(" ", 2);
    String firstWord = arr[0];
    System.out.println(firstWord + " first class seat capacity: " + firstClass);
    System.out.println(firstWord + " business class seat capacity: " + busiClass);
    System.out.println(firstWord + " economy class seat capacity: " + econClass);
    //Airline object
    A8AirlineAircraftData airline = new A8AirlineAircraftData(airName, airCode, airCraft, firstClass, busiClass, econClass);
    Set<A8AirlineAircraftData> aircrafts = new HashSet<>();

    /*for (int i = 0; i < 3; i++) {
        aircrafts.add(A8AirlineAircraftData.AddAirline(sc));
    } */

    System.out.println(airName + " successfully added. Press Enter to continue.");

    sc.nextLine();
    sc.nextLine();//Press Enter to continue 
    return airline;

2 个答案:

答案 0 :(得分:2)

您可以调用工厂方法并将创建的对象存储在Set中,例如。
例如,要创建3个元素,您可以编写:

Set<A8AirlineAircraftData> aircrafts = new HashSet<>();
for (int i=0; i<3; i++){
   aircrafts.add(A8AirlineAircraftData.AddAirline());
}
  

我不允许任何重复的airCode,HashSet会处理它   在某种程度上?

Set中元素的唯一性取决于其包含的元素的equals() / hashCode()方法。
您应该在A8AirlineAircraftData中覆盖它们,以依赖于以唯一方式标识实例的类字段:airCode

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((airCode == null) ? 0 : airCode.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (!(obj instanceof A8AirlineAircraftData))
        return false;
    A8AirlineAircraftData other = (A8AirlineAircraftData) obj;
    return Objects.equal(airCode, other.airCode);
}

答案 1 :(得分:1)

你是对的,Set是设置对象实现的接口,它不允许重复。在您的代码中,airCode是一个String,因此不允许重复的字符串。 要访问类外的变量或方法,必须为变量设置正确的访问修饰符。即公众将允许从任何其他类访问此变量。

public static Set<String> setOfAirCodes = new HashSet<>();

关于其他修饰语,请在此处阅读:access modifiers in java