【问题标题】:print objects from HashSet collection [duplicate]从 HashSet 集合中打印对象 [重复]
【发布时间】:2019-02-05 17:51:28
【问题描述】:

我试图打印 hashSet 集合中的对象。控制台仅显示最后一个对象(一个对象)。当我用同样的方法使用 ArrayList 时,我可以打印所有的对象。我使用了迭代器方法来打印集合集,请参阅附件测试。

public Set<Coupon> getAllCoupouns() throws Exception {

    Coupon coupon = new Coupon();
    Set<Coupon> coupons = new HashSet<Coupon>();

    // Open a connection
    conn = DriverManager.getConnection(Utils.getDBUrl());
    // Define the Execute query
    java.sql.Statement stmt = null;

    try {
        stmt = conn.createStatement();
        // build The SQL query
        String sql = "SELECT * FROM COUPON";
        // Set the results from the database
        ResultSet resultSet = stmt.executeQuery(sql);
        // constructor the object, retrieve the attributes from the results
        while (resultSet.next()) {

            coupon.setId(resultSet.getLong(1));
            coupon.setTitle(resultSet.getString(2));
            coupon.setStartDate((Date) resultSet.getDate(3));
            coupon.setEndDate((Date) resultSet.getDate(4));
            coupon.setAmount(resultSet.getInt(5));
            CouponType type = CouponType.valueOf(resultSet.getString(6)); // Convert String to Enum
            coupon.setType(type);
            coupon.setMessage(resultSet.getString(7));
            coupon.setPrice(resultSet.getDouble(8));
            coupon.setImage(resultSet.getString(9));

            coupons.add(coupon);

        }

    } catch (SQLException e) {
        throw new Exception("Retriev all the coupons failed");
    } finally {
        // finally block used to close resources
        try {
            if (stmt != null)
                conn.close();
        } catch (SQLException se) {
            // do nothing
        }
        try {
            if (conn != null)
                conn.close();
        } catch (SQLException se) {
            se.printStackTrace();
        }

    }
    return coupons;
}

【问题讨论】:

标签: java hash collections set


【解决方案1】:

当您从 while 循环外部初始化 Coupon 时,它每次都会不断添加相同的对象,因此,覆盖结果只会显示最后一个结果。

您需要做的是从while 循环中实例化Coupon,例如:

public Set<Coupon> getAllCoupouns() throws Exception {
    Set<Coupon> coupons = new HashSet<Coupon>();

    // Open a connection
    conn = DriverManager.getConnection(Utils.getDBUrl());
    // Define the Execute query
    java.sql.Statement stmt = null;

    try {
        stmt = conn.createStatement();
        // build The SQL query
        String sql = "SELECT * FROM COUPON";
        // Set the results from the database
        ResultSet resultSet = stmt.executeQuery(sql);
        // constructor the object, retrieve the attributes from the results
        while (resultSet.next()) {
            Coupon coupon = new Coupon();
            coupon.setId(resultSet.getLong(1));
            coupon.setTitle(resultSet.getString(2));
            coupon.setStartDate((Date) resultSet.getDate(3));
            coupon.setEndDate((Date) resultSet.getDate(4));
            coupon.setAmount(resultSet.getInt(5));
            CouponType type = CouponType.valueOf(resultSet.getString(6)); // Convert String to Enum
            coupon.setType(type);
            coupon.setMessage(resultSet.getString(7));
            coupon.setPrice(resultSet.getDouble(8));
            coupon.setImage(resultSet.getString(9));

            coupons.add(coupon);

        }

    } catch (SQLException e) {
        throw new Exception("Retriev all the coupons failed");
    } finally {
        // finally block used to close resources
        try {
            if (stmt != null)
                conn.close();
        } catch (SQLException se) {
            // do nothing
        }
        try {
            if (conn != null)
                conn.close();
        } catch (SQLException se) {
            se.printStackTrace();
        }

    }
    return coupons;
}

【讨论】:

    【解决方案2】:

    cupon 始终是同一个对象。您只制作了一个 Cupon 类的对象,因此该集合仅包含一个对象(您始终添加相同的对象)。您必须在 while 循环的每次迭代中创建一个新对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      • 2016-12-19
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      相关资源
      最近更新 更多