Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Why don't I get all the elements in my TreeSet?

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Why don't I get all the elements in my TreeSet?

     
     public void createPersons()
        {
            Random rand = new Random();
            for(int index = 0; index < persons.length; index++) {
                Person person = new Person(rand.nextInt(maxAge), persons[index]);
                setOfPersons.add(person);
            }
        }
     
    public void printPersons()
        {
            for(Iterator<Person> r = setOfPersons.iterator(); r.hasNext();) {
                System.out.println(r.next().getName() + " is " + r.next().getAge() + " years old");
            }
            System.out.println(setOfPersons.size());
        }

    setOfPersons is a TreeSet, persons[] is an array of strings that contains 10 names. Why printPersons() prints only the half names of setOfPersons?
    Note. I do get the size of the TreeSet as 10...


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,744
    My Mood
    Mellow
    Thanks
    233
    Thanked 710 Times in 696 Posts

    Default Re: Why don't I get all the elements in my TreeSet?

    Typically, a while() loop is used with an iterator:

    Iterator<Person> r = setOfPersons.iterator();
    while ( r.hasNext() ) { // code here }

    Why the for() loop?

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    718
    Thanks
    0
    Thanked 132 Times in 115 Posts

    Default Re: Why don't I get all the elements in my TreeSet?

    Because you call "next()" twice in your loop body.
    Everytime you call "next()" of an iterator you will get one additional element. If you have 10 elements you can call this method exactly 10 times. If you call it twice for each iteration of your loop you can only have 5 iterations.
    If you would put 11 elements into your set you would get an exception thrown.

    Instead, do a for-each loop:
    for (Person p : setOfPersons) {...}

  4. The Following 2 Users Say Thank You to Cornix For This Useful Post:

    bihlas (August 1st, 2014), GregBrannon (August 1st, 2014)

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,744
    My Mood
    Mellow
    Thanks
    233
    Thanked 710 Times in 696 Posts

    Default Re: Why don't I get all the elements in my TreeSet?

    Good catch, Cornix. I didn't look past the for() loop.

  6. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Why don't I get all the elements in my TreeSet?

    Thank you Cornix.... Greg I used the for loop because I saw it in an example in my book and I just liked the fact you can declare the Iterator inside the condition...I have seen that is more common to use a while loop for that, and until I see the example I did use only while loop. But i realized one more time that the best way to Iterate in case you dont remove elements from a list is the for each...

    Something else I am struggling with: do you have any idea how how my setOfPersons will sort the Person objects by age?
    from younger to older?

  7. #6
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    718
    Thanks
    0
    Thanked 132 Times in 115 Posts

    Default Re: Why don't I get all the elements in my TreeSet?

    A set is usually not sorted (because thats not part of its definition) but perhaps there are some sorted set variations that I dont know of.
    The best solution would be to use a List if you need order. You could create your own List which is also a Set if you specifically need the uniqueness of elements.

Similar Threads

  1. Replies: 9
    Last Post: October 14th, 2013, 06:17 AM
  2. Arraylist elements in Treeset
    By toyamigo in forum Collections and Generics
    Replies: 3
    Last Post: January 24th, 2013, 09:49 AM
  3. [SOLVED] ArrayList object's elements confusing??? doesnt replace the elements? set() method?
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 21st, 2011, 01:20 PM
  4. Problem with TreeSet.
    By GORGEORWELL in forum Collections and Generics
    Replies: 1
    Last Post: February 23rd, 2011, 06:23 PM
  5. [SOLVED] TreeSet to TreeMap Problem
    By MoniD in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 25th, 2011, 12:27 PM