Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2015
    Posts
    12
    Rep Power
    0

    Default Set with get(int i) ???

    hello,

    I am a big fan of arraylists, but I try to use a Set cos I need exclusive listing (no duplicate)
    but using a HashSet, i realize there is no get() accessor,
    witch is silly cos although it is a map, I still need to acess its content, an not iterator-wise, but index-wise
    so I ended up doing it myself

    Java Code:
    public class MySet<T>  
    {
        private Set<T> data=new HashSet<T>();
        
        public void add(T d)
        {
            data.add(d);
        }
        
        public T get(int i)
        {
            if(i>=data.size())
                return null;
            int _i=0;
            for(T d:data)
            {
                if(_i==i)
                    return d;
                _i++;
            }
            return null;
        }
    }
    why is this a map if it is only one dimentional, why not an arraylist that filter duplicates?
    so maybe there is another way/collection-type for doing this ?

    thanks

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Set with get(int i) ???

    Maybe you want to use your beloveth List and its contains( ... ) method before you add a (potential) duplicate element? Note that a Set also has this method, so if set.contains(element) is true, you already have this element (you passed it as a parameter).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Join Date
    Feb 2015
    Posts
    12
    Rep Power
    0

    Default Re: Set with get(int i) ???

    yes, overriding arraylist
    but the question was more about why the f... does that HashSet not have a bloody get(int i) accessor

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Set with get(int i) ???

    A Set isn't ordered according to a 'position' of elements, so it doesn't handle indexes (it isn't needed, if you want Lists, you know where to find them and you can even copy a Set to a List if you want to).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

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

    Default Re: Set with get(int i) ???

    Quote Originally Posted by rompelstilchen View Post
    yes, overriding arraylist
    Who said you had to override ArrayList? Just check the list before you add a new value. You can use a helper method referencing an ArrayList object.

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

  6. #6
    Join Date
    Feb 2015
    Posts
    12
    Rep Power
    0

    Default Re: Set with get(int i) ???

    I know I can use the helper, but i want an hybrid of arraylist and set, and I thought such a thing existed
    why checking contains in all my classes while it can be done by the collection itself, that's the point

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Set with get(int i) ???

    Quote Originally Posted by rompelstilchen View Post
    I know I can use the helper, but i want an hybrid of arraylist and set, and I thought such a thing existed
    why checking contains in all my classes while it can be done by the collection itself, that's the point
    Well, such a thing doesn't exist because you want indexed functionality combined with Set functionality; it can easily be created though with the aid of the building blocks of the Collections framework; it's up to you how to build it; Jim already gave you a suggestion. You might even have a look at the LinkedHashSet but you have to iterate over the elements; there is no free lunch. Remember: a set is not an array.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Posting Permissions

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