File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collection problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collection problem" Watch "Collection problem" New topic

Collection problem

Rohit Ramachandran
Ranch Hand

Joined: Oct 05, 2010
Posts: 102
What is the result of compiling and running the following code?
import java.util.SortedMap;
import java.util.TreeMap;

public class Test {

public static void main(String[] args) {
TreeMap<Integer,String> map = new TreeMap<Integer,String>();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
map.put(4, "four");
SortedMap<Integer, String> smap1 = map.tailMap(2);
SortedMap<Integer, String> smap2 = smap1.headMap(4);
SortedMap<Integer, String> smap3 = smap2.subMap(2, 3);

1 {2=two, 3=three, 4=four}
2 {2=two, 3=three}
3 {2=two}
4 no output is printed


Tell me if I'm wrong and how.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Hi Rohit and welcome to the JavaRanch.

Please UseCodeTags when posting code. It will highlight your code and make it much easier to read. It probably will increase the number of people helping you.

Why don't you try to run it? Then you can compare it with what you thought and tell us why you think it should be your answer.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
I agree. Here's the link:
subject: Collection problem
It's not a secret anymore!