• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Collection problem

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic