This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collection problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Collection problem" Watch "Collection problem" New topic
Author

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);
System.out.println(smap3);
}
}


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

map=[1234]
smap1=[234]
smap2=[234]
smap3=[]

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: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Collection problem
 
Similar Threads
HashMap remove multiple elements
1:1 Map class?
Query on enhanced FOR loop
How can I Iterate a map
Map