从HashSet集合打印对象[复制]

问题描述 投票:-2回答:2

这个问题已经在这里有一个答案:

enter image description hereI试图打印从HashSet集合的对象。控制台显示仅最后一个对象(一个对象)。当我使用的ArrayList用同样的方法,我能够打印的所有对象。我用一个iterator方法,以打印收集组,见附表test.Example of test result

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
2个回答
0
投票

当你从Coupon循环外初始化while,它不断每次添加相同的对象,因此,在覆盖只有最后的结果结果显示。

你需要做的是从Coupon循环,e.g中实例while

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

0
投票

cupon始终是同一个对象。你让类Cupon的只有一个对象,所以集合中只包含一个对象(添加总是相同的对象)。您必须在while循环每次迭代做出一个新的对象。

© www.soinside.com 2019 - 2024. All rights reserved.