Results 1 to 7 of 7
  1. #1
    Malv is offline Member
    Join Date
    Jun 2013
    Posts
    62
    Rep Power
    0

    Default toList Method of Setlmpl

    Hi guys,

    Does anyone knows what does toList method do? I've encountered some difficulties understanding the requirement of this question.

    Write the toList method of SetImpl.java. toList returns a List containing the elements of the set. The elements of the list can be in any order but the result of toList must not contain any duplicates.

    Here is this skeleton code:

    public List<T> toList(){
    ArrayList<T> res = null;
    return res;
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: toList Method of Setlmpl

    It is supposed to return a list of the set elements. Since sets cannot hold duplicates, then set implementation must just return a List<T> of the elements in Set<T> for some type T. However, it mus return a List and not a Set (so the return value can hold duplcates or do other things required of lists. At least, that is my interpretation.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Malv is offline Member
    Join Date
    Jun 2013
    Posts
    62
    Rep Power
    0

    Default Re: toList Method of Setlmpl

    Quote Originally Posted by jim829 View Post
    It is supposed to return a list of the set elements. Since sets cannot hold duplicates, then set implementation must just return a List<T> of the elements in Set<T> for some type T. However, it mus return a List and not a Set (so the return value can hold duplcates or do other things required of lists. At least, that is my interpretation.

    Regards,
    Jim
    how can i transfer the set elements over to a list where i do not know how to initialize a set in the tolist method.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: toList Method of Setlmpl

    Well, what are you using in the Set implementation to hold the elements of the set? If it is an array, create an instance of some List implementation (see List API for implementations available) and add them to it and return the List. If you are already using a List internally, then make a copy and return the copy.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Malv is offline Member
    Join Date
    Jun 2013
    Posts
    62
    Rep Power
    0

    Default Re: toList Method of Setlmpl

    Quote Originally Posted by jim829 View Post
    Well, what are you using in the Set implementation to hold the elements of the set? If it is an array, create an instance of some List implementation (see List API for implementations available) and add them to it and return the List. If you are already using a List internally, then make a copy and return the copy.

    Regards,
    Jim
    i've goggled online regarding converting set to list and i've done this

    // converts to a list
    public List<T> toList(){
    Set<String> s = new SetImpl<String>();
    ArrayList<T> res = new ArrayList<T>();
    res.addAll(s);
    return res;
    }

    but it looks wrong and it's definitely wrong.
    can somebody correct my mistake?

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: toList Method of Setlmpl

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: toList Method of Setlmpl

    Malv, please go through the Forum Rules, particularly the second paragraph.

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 7
    Last Post: 04-11-2013, 06:31 AM
  2. Replies: 2
    Last Post: 03-23-2012, 05:53 AM
  3. Replies: 1
    Last Post: 10-17-2011, 02:00 AM
  4. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 08:20 PM
  5. Replies: 29
    Last Post: 09-25-2008, 08:55 PM

Posting Permissions

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