0

I have data that structured like this (See the code).

public ArrayList<HashMap<String, String>> GetUsers(String Query) {
    ArrayList<HashMap<String, String>> userList = new ArrayList();
    try {
        ResultSet rs = stmt.executeQuery(Query);
        while (rs.next()) {
            HashMap<String, String> user = new HashMap();
            user.put("hotelid", rs.getString(COLUMN_HotelID));
            user.put("countery", rs.getString(COLUMN_Countery));
            user.put("city", rs.getString(COLUMN_CITY));
            user.put("hotelname", rs.getString(COLUMN_HOTELNAME));
            user.put("startdate", rs.getString(COLUMN_STARTDATE));
            user.put("enddate", rs.getString(COLUMN_ENDDATE));
            userList.add(user);
        }
    } catch (SQLException ex) {
        Logger.getLogger(SQLiteJDBC.class.getName()).log(Level.SEVERE, null, 
    }
    return userList;
}

Every time I iterate over the arraylist I get values like this:

for (int i = 0; i < userList.size(); i++) {
    System.out.println(userList.get(i));
}

{enddate=6/6/2019, city=karak, hotelid=11, startdate=4/22/2019, countery=jordan, hotelname=Dalallah}

I want to print the values in a different way like selecting the value that I want to print.

3
  • If you want to model a user in Java, using a HashMap to store lose key value pairs will not be the object oriented way... Create a class User that holds the user values... Commented Apr 23, 2019 at 7:51
  • Yes, its not OOP oriented , but its worked nicely !! Commented Apr 23, 2019 at 9:19
  • Your decision... If it works as desired, then fine ;-) Commented Apr 23, 2019 at 9:22

1 Answer 1

3

Once you get an element from the list, you can execute any method on it, e.g., Map#get. So, e.g., if you want to print all the hotel names:

for (int i = 0; i < userList.size(); i++) {
     System.out.println(userList.get(i).get("hotelname");
     // Here --------------------------^
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.