Results 1 to 4 of 4
  1. #1
    JavaTheHut is offline Member
    Join Date
    Dec 2010
    Rep Power

    Talking Help with TreeSet and finding longest string

    Well due to sickness I missed a week of college which means I missed out on a lecture Im using something called TreeSet which seems to group things. Anywho I need to get the longest name in a list of names. Here is my code:
    import java.util.*;
    /** demonstrates Set and SortedSet via a list of names
    public class NameSet {
    public static void main (String args[]){
    Set<String> names = new HashSet<String>();
    // transfer all the names to a (sorted) TreeSet
    TreeSet<String> sortedNames = new TreeSet<String>(names);
    System.out.println("First person is " +sortedNames.first());
    System.out.println("longest name is ")+sortedNames.
    Code end.
    Ps Im not being lazy I just want a straightforward answer as I dont get it

  2. #2
    YolanC is offline Member
    Join Date
    Feb 2011
    Rep Power

    Default treeset

    hey, for what i understood a treeset does the organising of the elements you add to it automatic, using a comparator,
    this is a certain value to what you compare every imputed element. this comparator can be static or resizeable.
    witch you can define your self or use a default one.

    knowing that the longest inputted string will always be the last or first of the set (not sure witch it is). i haven't tested this tho.

    I do suggest however you readup upon the subject cause its quite complex matter imo.

    for more on the tree set
    TreeSet (Java 2 Platform SE v1.4.2)
    and for more on the comparator
    Comparator (Java 2 Platform SE v1.4.2)
    Last edited by YolanC; 05-04-2011 at 10:01 PM. Reason: wall of text

  3. #3
    JavaTheHut is offline Member
    Join Date
    Dec 2010
    Rep Power


    Thanks its something at least. I already checked the api but thanks anyway

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Rep Power


    As mentioned, see the API - a TreeSet will order entries based upon their natural ordering or a provided Comparator. This means for String's (without a defined comparator) they will be alphabetized (see String.compareTo in the API docs). To retrieve the elements of the Set, retrieve the Iterator of that set and do the Iteration.
    The Set Interface (The Java™ Tutorials > Collections > Interfaces)

Similar Threads

  1. Longest common sequence
    By fam2315 in forum New To Java
    Replies: 8
    Last Post: 04-29-2011, 02:00 AM
  2. longest word present in a string
    By somnath6088 in forum New To Java
    Replies: 4
    Last Post: 11-09-2010, 05:43 PM
  3. Longest word in a program...
    By hustlas4ever in forum New To Java
    Replies: 5
    Last Post: 08-20-2010, 01:34 PM
  4. Finding and storing a word from a string builder
    By ao241 in forum Advanced Java
    Replies: 3
    Last Post: 06-28-2010, 12:46 PM
  5. Replies: 2
    Last Post: 03-25-2008, 05:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts