Results 1 to 11 of 11
  1. #1
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Collections that are 'ordered' ?

    Collections that are 'ordered' ?-jvm.jpg

    Hello,
    I am using this image as a reference to help learn the Collection interface better and it seems I must be getting confused with the 'ordered/unordered' types. I thought ordered would mean ordering the objects automatically like a TreeSet using the comparable interface so it could be numerically or alphabetically ordered etc....but it seems I am wrong on this. Could someone lead me in the right direction?
    Thanks


    Java Code:
        public static void main(String[] args) {
            Set hashSet = new HashSet();
            hashSet.add(1);
            hashSet.add(3);
            hashSet.add(2);
            for (Object o : hashSet){
                System.out.print(o + " "); // Prints: 1,2,3 (Unordered but puts the numbers in order....)
            }
            System.out.println();
            Set linkedHashSet = new LinkedHashSet();
            linkedHashSet.add(1);
            linkedHashSet.add(3);
            linkedHashSet.add(2);
            for (Object o : linkedHashSet){
                System.out.print(o + " "); // Prints: 1,3,2 (Ordered but doesn't put the numbers in order....)
            }
            System.out.println();
            Set sortedSet = new TreeSet();
            sortedSet.add(1);
            sortedSet.add(3);
            sortedSet.add(2);
            for (Object o : sortedSet){
                System.out.print(o + " "); // Prints: 1,2,3
            }
            System.out.println();
            List arrayList = new ArrayList();
            arrayList.add(1);
            arrayList.add(3);
            arrayList.add(2);
            for (Object o : arrayList){
                System.out.print(o + " "); // Prints 1,3,2
            }
            System.out.println();
            List vector = new Vector();
            vector.add(1);
            vector.add(3);
            vector.add(2);
            for (Object o : vector){
                System.out.print(o + " "); // Prints 1,3,2
            }
        }
    Java Code:
    run:
    1 2 3 
    1 3 2 
    1 2 3 
    1 3 2 
    1 3 2 
    BUILD SUCCESSFUL (total time: 0 seconds)

  2. #2
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: Collections that are 'ordered' ?

    After some more research, i think I have the answer...
    Ordered means that it will stay in the same order as you add them, and Sorted means it will be sorted using the comparable interface...but why then is HashSet sorted?

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    929
    Rep Power
    2

    Default Re: Collections that are 'ordered' ?

    What makes you think HashSet is sorted?
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,876
    Rep Power
    5

    Default Re: Collections that are 'ordered' ?

    Especially if you take a look at the picture in the original post :/ It states quite clearly: unordered.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    929
    Rep Power
    2

    Default Re: Collections that are 'ordered' ?

    Perhaps your brain melted when you read the text written in Comic Sans...
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: Collections that are 'ordered' ?

    I thought it's sorted because it gets changed from 1,3,2 to 1,2,3?

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: Collections that are 'ordered' ?

    Quote Originally Posted by Teedo View Post
    I thought it's sorted because it gets changed from 1,3,2 to 1,2,3?
    There are only six permutations of the numbers 1, 3 and 2; the HashSet picked 1, 2, 3; try other (larger) sets and see for yourself ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: Collections that are 'ordered' ?

    I did try 1,3,2,10,5,12...and it stilll printed in order
    But I added a lot more and now it doesn't, thanks :)

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    929
    Rep Power
    2

    Default Re: Collections that are 'ordered' ?

    I tried this as well, and observed this behaviour:
    Java Code:
    package forums;
    
    import java.util.HashSet;
    import java.util.Set;
    
    public class HashSetTest {
        public static void main(String[] args) {
            Set<Integer> test = new HashSet<Integer>();
    
            test.add(1);
            test.add(7);
            test.add(10);
            test.add(3);
            test.add(6);
            test.add(4);
            test.add(9);
            test.add(2);
            test.add(5);
            test.add(8);
    
            for (Integer integer : test) {
                System.out.println("integer = " + integer);
            }
    
            Set<Character> test2 = new HashSet<Character>();
    
            test2.add('1');
            test2.add('7');
            test2.add('0');
            test2.add('3');
            test2.add('6');
            test2.add('4');
            test2.add('9');
            test2.add('2');
            test2.add('5');
            test2.add('8');
    
            for (Character character : test2) {
                System.out.println("character = " + character);
            }
    
        }
    }
    Output:
    Java Code:
    integer = 1
    integer = 2
    integer = 3
    integer = 4
    integer = 5
    integer = 6
    integer = 7
    integer = 8
    integer = 9
    integer = 10
    character = 3
    character = 2
    character = 1
    character = 0
    character = 7
    character = 6
    character = 5
    character = 4
    character = 9
    character = 8
    In a large test up to 100 Integers, I see that from the 16th Integer, the numbers tend to vary. It's definately not sorted. The first 15 are just the lucky ones I guess :)
    Java Code:
    public class HashSetTest {
        public static void main(String[] args) {
            Set<Integer> test = new HashSet<Integer>(100);
    
            for (int i = 0; i < 100; i++) {
                test.add(i);
            }
    
    
            for (Integer integer : test) {
                System.out.println("integer = " + integer);
            }
    
    
        }
    }
    Output:
    Java Code:
    integer = 0
    integer = 1
    integer = 2
    integer = 3
    integer = 4
    integer = 5
    integer = 6
    integer = 7
    integer = 8
    integer = 9
    integer = 10
    integer = 11
    integer = 12
    integer = 13
    integer = 14
    integer = 15
    integer = 17
    integer = 16
    integer = 19
    integer = 18
    integer = 21
    integer = 20
    ...
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  10. #10
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: Collections that are 'ordered' ?

    I tried up to 50 and got the same result as you. String and characters aren't sorted at all and integers are up until about 16....interesting :)
    Though of course I wouldn't use that information as fact that it would always happen.

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,876
    Rep Power
    5

    Default Re: Collections that are 'ordered' ?

    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. how to design totally ordered multicast
    By someUser in forum New To Java
    Replies: 2
    Last Post: 10-03-2013, 02:45 PM
  2. Ordered Vector Issue
    By mmonish91 in forum New To Java
    Replies: 8
    Last Post: 03-07-2012, 07:44 AM
  3. Ordered Array plz help....due tomorrow!!
    By canyonist in forum New To Java
    Replies: 6
    Last Post: 05-23-2010, 09:57 AM
  4. viewing records ordered by date
    By nitish.dhar in forum New To Java
    Replies: 3
    Last Post: 07-31-2008, 07:14 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
  •