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 4 of 4

Thread: Getting an error while altering a source code

  1. #1
    Junior Member marksquall's Avatar
    Join Date
    Jan 2009
    Posts
    27
    My Mood
    Fine
    Thanks
    0
    Thanked 1 Time in 1 Post

    Unhappy Getting an error while altering a source code

    Dear javaprogrammingforums administrator and members,


    Hello everyone, I hope everyone is fine upon reading this thread of mine. I'm currently reading "Java How to Program Third Edition by Deitel and Deitel" and exploring about Collections Framework. I tried to program one of their source codes about the Set interface implemented by HashSet. I edited it a little though. By the way, here's the code:

    [FONT="Courier New"]
    import java.util.*;
    public class Sample{
      public static void main(String [] args){
          String colors[]={"green","red","blue","red","blue","white",
                           "orange","black"};
          List list = new ArrayList(Arrays.asList(colors));
          System.out.println(list);
     
          HashSet ref = new HashSet(list);
          Iterator i = ref.iterator();
     
          System.out.println("\n\nElements with no duplicates:");
          while(i.hasNext()){
              System.out.print( i.next()+ " "); //Line 11
           }
      }//main
     
    }//class
     
    [/FONT]


    The program works fine, but I just wonder why the ourput in Line 11 is in this order?

    orange red blue green white black


    I am really expecting this output:

    green red blue white orange black



    I hope someone would give more information about why this came to be. I hope this is not too much to ask.

    Thank you and God bless everyone.


    Respectfully Yours,

    MarkSquall


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: A question about the Set interface. Please help.

    Allow me to quote JavaDocs
    Quote Originally Posted by JavaDoc
    This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time.
    Chris

  3. #3
    Junior Member marksquall's Avatar
    Join Date
    Jan 2009
    Posts
    27
    My Mood
    Fine
    Thanks
    0
    Thanked 1 Time in 1 Post

    Talking It's clear to me now.

    Dear Mr. Chris,


    A pleasant day to you. Wow, that was a quick answer from JavaDocs, hehehe.


    Now I know... thanks a lot Mr. Chris. I tried to experiment with the program, I added more colors (with duplicating entries), and sort them using Arrays.sort(), then when I tried to use TreeSet instead of HashSet, whoa...the oder is still in alphabetical order (with duplicate entries removed).


    Again, thank you Mr. Chris and God bless everyone.


    Respectfully Yours,

    MarkSquall

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: A question about the Set interface. Please help.

    I'm glad it gave you the answer you were after and that you have your solution. If that is everything would you mind marking this thread as solved.

    Thanks,
    Chris

Similar Threads

  1. [SOLVED] How to narrow down the range of input in java?
    By big_c in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 20th, 2009, 11:38 AM
  2. Problem while implementing a basic user interface menu
    By Rastabot in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 3rd, 2009, 04:38 PM
  3. How to show 2D array using combobox and radiobutton?
    By Broken in forum Loops & Control Statements
    Replies: 1
    Last Post: March 10th, 2009, 06:01 AM