简体   繁体   中英

Sort List < HashMap < String, Object >> by value of key

I have a list of this type List < HashMap < String, Object >> ResultSet

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

and I want to sort the list values by the value of the "PROBABILITY" key. When I print the keys and their values I get this:

l_commitdate=1996-02-12
l_partkey=155190
PROBABILITY=56.63499683535207
l_commitdate=1996-02-28
l_partkey=67310
PROBABILITY=49.93142590485798
l_commitdate=1996-03-05
l_partkey=63700
PROBABILITY=56.88804620364059
l_commitdate=1996-03-30
l_partkey=2132
PROBABILITY=55.511179358539486

And I wanted to get this:

l_commitdate=1996-03-05
l_partkey=63700
PROBABILITY=56.88804620364059
l_commitdate=1996-02-12
l_partkey=155190
PROBABILITY=56.63499683535207
l_commitdate=1996-03-30
l_partkey=2132
PROBABILITY=55.511179358539486
l_commitdate=1996-02-28
l_partkey=67310
PROBABILITY=49.93142590485798

假设概率值是double类型:

list.sort((p1,p2)-> Double.compare( (double)p2.get("PROBABILITY"),(double) p1.get("PROBABILITY")));

It looks like you know the structure of each row in your ResultSet , so things would get a lot easier for you if you create an object to represent each row instead of using a Map<String, Object> . I've called it Part since that's what the key seems to refer to, but call it whatever makes sense.

public class Part {
    private LocalDate commitDate;
    private long partKey;
    private BigDecimal probability;

    //getters and setters
}

With this you can alter your method to return a list of Part s (as an aside: you shouldn't use an untyped list here. Even if you don't follow the rest of my advice, your method signature should always have a type for the List that's being returned. Java method names should also start with a lowercase character - by convention only class es have names that start with upper case).

public List<Part> resultSetToParts(ResultSet rs) throws SQLException {
    List<Part> list = new ArrayList<>(); //No need to specify the generic type again, we can use the diamond operator for Java 7 and later

    while (rs.next()) {
        Part part = new Part();

        part.setCommitDate(rs.getDate("l_commitdate").toLocalDate()); //Note: this is vulnerable to NullPointerExceptions if the date can be null
        part.setPartKey(rs.getString("l_partkey"));
        part.setProbability(BigDecimal.valueOf(rs.getDouble("PROBABILITY")));

        list.add(part);
    }
    return list;
}

Now that we've done the small amount of work to set that up, you can sort it really easily by any field you want:

List<Part> parts = resultSetToParts(rs);
parts.sort(Comparator.comparing(Part::getProbability);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM