HashSet<String> user = new HashSet<String>(); String SQL = "SELECT distinct userid FROM sch.stutents order by userid ASC"; ResultSet resultSet = connection.createStatement().executeQuery(SQL); while (resultSet.next()) { user.add(resultSet.getString("userid")); } If you pull data using pgAdmin III , then correctly sorted data is returned. But when using ResultSet data is clearly not sorted by field!
How to get a sorted result from a database using ResultSet ?
userhash table, it does not guarantee the order of the added data. - Pavel Parshin