Results 1 to 9 of 9
  1. #1
    waklo99 is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

    Default :( anyone here plz help on HashSet

    anyone know how to use this code with integers ???

    I've found this original code and it works fine with string

    Java Code:
    import java.util.HashSet;
    import java.util.Set;
    
    public class FindDups {
      public static void main(String args[]) {
        Set s = new HashSet();
        String[] values = new String[] { "java", "java2", "java2s", "javas",
            "java" };
        for (int i = 0; i < values.length; i++)
          if (!s.add(values[i]))
            System.out.println("Duplicate detected: " + values[i]);
    
        System.out.println(s.size() + " distinct words detected: " + s);
      }
    }

    I tried editing it but its error TT^TT
    Java Code:
    import java.util.HashSet;
    import java.util.Set;
    
    public class FindDups {
      public static void main(String args[]) {
        Set<Integer> s = new HashSet<Integer>();
        int[] values = new int[] {1,2,3,4,5 };
        for (int i = 0; i < values.length; i++)
          if (!s.add(values[i]))
            System.out.println("Duplicate detected: " + values[i]);
    
        System.out.println(s.size() + " distinct words detected: " + s);
      }
    }
    Last edited by waklo99; 09-13-2010 at 09:44 AM.

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

    Default

    It works fine for me; the output of the second example is:

    Java Code:
    Duplicate detected: 3
    4 distinct words detected: [1, 2, 3, 4]
    Are you using Java 1.4 or older?

    kind regards,

    Jos

  3. #3
    waklo99 is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

    Default

    no :( not the first code... the second one

    I don't know how to use for Integer...
    Last edited by waklo99; 09-13-2010 at 09:59 AM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    The second one works as well...
    "5 distinct words detected: [1, 2, 3, 4, 5]"

    If I add an additional 5 in the array I also get:
    "Duplicate detected: 5"

  5. #5
    Thennarasu's Avatar
    Thennarasu is offline Member
    Join Date
    Jan 2009
    Location
    Chennai, Tamilnadu
    Posts
    39
    Rep Power
    0

    Default

    i dont find any problem with the second code..
    One Life!!! Y Serious??? :)

  6. #6
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

    Default

    Both first and second does work fine.

    I tried editing it but its error TT^TT
    Can you show your error / exception!

    -Regards

  7. #7
    waklo99 is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

    Default

    oops sorry guys it was working after all xD

    just like Jos said my Java might be outdated.... I tried running the program in another pc and it worked...

    can someone tell me where I can download an updated version of java?
    Last edited by waklo99; 09-15-2010 at 07:25 AM.

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

    Default

    Quote Originally Posted by waklo99 View Post
    can someone tell me where I can download an updated version of java?
    Too lazy to Google? Here, I did it for you: download link. (11 secs)

    kind regards,

    Jos

  9. #9
    waklo99 is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

    Default

    hehe xD thnx... i was searching it but I dont know what to put on google search.... thnx man

Similar Threads

  1. HashSet Contains problem
    By guywalder in forum Advanced Java
    Replies: 11
    Last Post: 09-01-2009, 02:48 PM
  2. HashSet anomaly
    By jon80 in forum New To Java
    Replies: 1
    Last Post: 06-21-2009, 08:22 PM
  3. HashSet and AbstractSet both Implements Set
    By rkjsohi2 in forum New To Java
    Replies: 1
    Last Post: 08-07-2008, 11:04 AM
  4. Working with HashSet and TreeSet
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:34 PM
  5. Iterating through a HashSet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-21-2008, 04:34 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
  •