user471450
user471450

Reputation:

How to get values more than certain key in TreeMap

I have a TreeMap with key , value pair as SessionIdNode

private class SessionNode 
{
    String sessionid;
    Timestamp time;


    private SessionNode (String sessionid, long  d)
    {
        this.time = new Timestamp(d);
        this.sessionid = sessionid;
    }


    public int hashCode()
    {
        return sessionid.hashCode();

    }

    public boolean equals(SessionNode node)
    {
        return this.sessionid.equals(node.sessionid);
    }


}

This class, is used internally in a TreeMap as below:

private final Comparator< SessionNode> sessionNodeComparator = new Comparator<SessionNode>() {
        @Override public int compare(SessionNode s1, SessionNode s2) 
        {
            return ((SessionNode)s1).time.compareTo(s2.time);   
        }           
    };

private Map <SessionNode , SessionNode> map = new TreeMap <SessionNode , SessionNode>( sessionNodeComparator);

Now, my question is , it sorts the SessionNodes on the basis of custom comparator, which is essentially according to time.

Now say given a time t1, I need to remove all the keys in the treemap which have SessionNode timestamp value > t1.

How do I accomplish this?? Any help would be greatly appreciated.

Thank you.

Upvotes: 0

Views: 990

Answers (1)

Louis Wasserman
Louis Wasserman

Reputation: 198171

map.tailMap(new SessionNode(null, t1), false).clear();

Upvotes: 2

Related Questions