• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

preparing for scjp 5.0  RSS feed

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.*;

public class TestSet
{
public static void main(String[] args)
{
Collection<String> set = new TreeSet<String>();
set.add("java");
set.add("xml");
set.add("Swing");
set.add("jsp");
set.add("java");
for(Object o : set)
{
System.out.println(o + " ");
}
}
}


the o/ p of this programe is jsp java xml swing

how this flows
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

how did you get that output? When i run that code, the output is as follows:


As you can see the output is sorted (capitals before non-capitals) and there are no duplicate elements in the collection.
 
anita dhar
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.*;

public class TestSet
{
public static void main(String[] args)
{
Collection<String> set = new TreeSet<String>();
set.add("Java");
set.add("XML");
set.add("Swing");
set.add("JSP");
set.add("Java");
for(Object o : set)
{
System.out.println(o + " ");
}
}
}

o/p JSP Java Swing XML
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try doing this....

import java.util.*;


public class TestSet
{
public static void main(String[] args)
{
Collection<String> set = new TreeSet<String>();
set.add("Java");
set.add("JSP");
set.add("_Java");
set.add(" Java");
set.add(" JAVA");
set.add(" java");
set.add("_java");
for(Object o : set)
{
System.out.println(o + " ");
}
}
}

The TreeSet uses the natural sorting order for String, which is "empty space first", the "Capital letters", then lowers, then underscore....
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy Ranchers!

Prem posted

Try doing this....
(...)
The TreeSet uses the natural sorting order for String, which is "empty space first", the "Capital letters", then lowers, then underscore....



Prem, you are new to the Ranch, so


Welcome!!!





I only cite your message, because it seems to be the most important part of this thread.

Now the unimportant part:


Mo posted

code:



Perhaps Mo wanted us to remember, that below the entry window for your contributions, there is a button.
When posting code, use it, otherwise you loose the indentations and the code looks less readable.
But, Mo, you really shouldn't use this button




Yours,
Bu.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I executed the following code.
import java.util.*;

public class TestSet
{
public static void main(String[] args)
{
Collection<String> set = new TreeSet<String>();
set.add("Java");
set.add("_Java");
set.add("kava");
set.add(" JAVA");
for(Object o : set)
{
System.out.println(o + " ");
}
}
}

I got the output as

JAVA
Java
_Java
kava

So I think the it is sorted as - empty space,capital letters. underscore then lower letters
Pls tell me if i'm wrong
 
prem arora
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Bu,
Thanks, I shall take care about adding code.. :-)

And yes, sumi, you are correct about the order, i mentioned it in-correctly, thanks for rectifying and thank god I didn't face a question on it in the real exam. :-)
 
You ought to ventilate your mind and let the cobwebs out of it. Use this cup to catch the tiny ads:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!