programmer
programmer

Reputation: 65

Convert List<HashMap<String, Object>> to stream

I have a list of this type List<HashMap<String, Object>> ResultSet that contains the result of a query to a database. I wanted call stream() on the List object. How do I do that ? This code create my list:

public class ResultSetToMap {
    int Columns = 0;
    List<Map<String, String>> data = new ArrayList<Map<String, String>>();
    HashMap<String, Object> row = new HashMap<>();

    public List ResultSetToMap(ResultSet rs) throws SQLException {
        ResultSetMetaData md = rs.getMetaData();
        int columns = md.getColumnCount();
        List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();

        while (rs.next()) {
            row = new HashMap<String, Object>(columns);
            for (int i = 1; i <= columns; ++i) {
                row.put(md.getColumnName(i), rs.getObject(i));
            }
            list.add(row);
        }
        return list;

    }
}

I can query the list data in this way:

for (HashMap<String, Object> map : list) {
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        System.out.println(entry.getKey() + " : " + entry.getValue());
    }
}

And I want to make queries about those data later. Of the type:

films.stream()
    .filter(Film.LENGTH.greaterThan(60))
    .sorted(Film.TITLE)
    .forEach(System.out::println);

Upvotes: 1

Views: 1401

Answers (3)

Samuel Philipp
Samuel Philipp

Reputation: 11042

Beside the fact that you should better filter and sort your data in your SQL you should use an Object Film instead of a Map:

public class Film {
    private String title;
    private int length;
    // more attributes if you have ...

    public Film(String title, int length) {
        this.title = title;
        this.length = length;
    }

    public String getTitle() {
        return title;
    }

    public int getLength() {
        return length;
    }

    @Override
    public String toString() {
        return title + " : " + length;
    }
}

Now you can use this while creating the result List:

public List<Film> getFilmsFromResultSet(ResultSet rs) throws SQLException {
    List<Film> result = new ArrayList<>();

    while (rs.next()) {
        result.add(new Film(rs.getString("TITLE"), rs.getInt("LENGTH")));
    }

    return result;
}

Use the column names from your table in the rs.getInt() and rs.getString() methods.

Use your getFilmsFromResultSet() methos like this:

List<Film> films = getFilmsFromResultSet(rs);
films.stream()
        .filter(f -> f.getLength() > 60)
        .sorted(Comparator.comparing(Film::getTitle))
        .forEach(System.out::println);

Upvotes: 2

Andreas
Andreas

Reputation: 159086

You stream a list by calling stream() on the List object.

Example:

list.stream()
    .filter(row -> ((Number) row.get("LENGTH")).intValue() > 60)
    .sorted(Comparator.comparing(row -> (String) row.get("TITLE")))
    .forEach(System.out::println);

Note that it would be better to build a list of objects representing the row data, instead of a list of maps, so the data can be handled in a type-safe manner.

Upvotes: 1

Ruslan
Ruslan

Reputation: 6290

You could just use flatMap operation like:

list.stream()
        .flatMap(map -> map.entrySet().stream())
        .forEach(entry -> System.out.println(entry.getKey() + " : " + entry.getValue()));

Upvotes: 1

Related Questions