• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

preparing for scjp 5.0

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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. :-)
 
The City calls upon her steadfast protectors. Now for a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic