• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collection problem

 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic