Results 1 to 2 of 2
  1. #1
    HoChiMinh is offline Member
    Join Date
    Jan 2009
    Posts
    1
    Rep Power
    0

    Default SCPJ6 Study Guide - Question about exercise

    I'm trying an exercise on the "Backed collections" section on page 589, but i'm stuck after implementing the "Exam watch" of page 591.

    Has anybody an idea how i can do these pollXxxKey() operations on a subMap of a TreeMap?

    A similar test on the subSet of a TreeSet is working fine.
    But there the SortedSet object returned can be casted to a TreeSet.
    The sorted TreeMap returned by subMap() cannot be casted to a TreeMap so i'm stuck...

    Java Code:
    import java.util.*;
    
    public class Test {
    
      public static void main(String[] args) {
        //TreeMap en subMap test    
        TreeMap<String,String> names = new TreeMap<String,String>(new NameComp() );
        
        //sorting test
        names.put("0", "name 1");
        names.put("4", "name 2");
        names.put("1", "name 3");    
        System.out.println(names);
        
        SortedMap<String,String> subnames = names.subMap("2", true, "0", true);
        System.out.println(names);
        System.out.println(subnames);
        
        [COLOR="Red"]subnames.pollLastEntry();   [/COLOR] 
        System.out.println("---\nAfter:");
        System.out.println(names);
        System.out.println(subnames);
      }
    
    }
    
    class NameComp implements Comparator<String> {
    
      public int compare(String one, String two) {
        //descending order
        return two.compareTo(one);
      }
    }

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    pollLastEntry() is defined in NavigableMap (of which SortedMap is a subinterface): it just removes and returns the last entry in the map, or null if the map is empty. (I'm guessing that's why it's called "poll" rather than "remove".)

    Is this not in the javadoc?

Similar Threads

  1. Replies: 2
    Last Post: 10-23-2008, 05:43 PM
  2. Alkitab Bible Study 0.8
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 05-13-2008, 05:22 PM
  3. I/O exercise
    By Feldom in forum New To Java
    Replies: 1
    Last Post: 10-28-2007, 04:48 PM
  4. easy way to study the java springs concept
    By kumar84 in forum New To Java
    Replies: 1
    Last Post: 07-17-2007, 03:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •