Results 1 to 5 of 5
  1. #1
    pi4r0n is offline Member
    Join Date
    Jan 2011
    Posts
    19
    Rep Power
    0

    Default HashSet with array

    Hello ALL

    I have got 2 method:

    1st - Returns Set <Integer> which contains an array
    2nd - Takes returned value from 1st method as HashSet argument.

    Then I need to find consecutive numbers like 1,2 in order to find the numbers I need to first loop through the HashSet, but there is no length, so how do I do it ??

    Cheers in advance for all your support

    pi4r0n
    Last edited by pi4r0n; 03-10-2011 at 11:52 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by pi4r0n View Post
    1st - Returns Set <Integer> which contains an array
    This confuses me. How can a Set<Integer> contain an array?

    Then I need to find consecutive numbers like 1,2 in order to find the numbers I need to first loop through the HashSet, but there is no length, so how do I do it ??
    HashSet has a size() method. It also implements the Iterable interface and you can iterate through it with this or the foreach loop.

  3. #3
    kwgivler is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    See: HashSet (Java Platform SE 6)

    Is this what you are trying to do?

    Java Code:
    Set<Integer> set = hashSet.keySet();
    		Iterator<Integer> setIt = hashSet.iterator();
    		while(setIt.hasNext())
    		{
                         int num = setIt.next()
                         <Do stuff>
    		}

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Another problem: HashSet makes no guarantee of the order of the components. If you are looking to see if values are in order then a HashSet is the wrong choice. TreeSet or extracting values into another Collection and then sorting them would be better.

  5. #5
    pi4r0n is offline Member
    Join Date
    Jan 2011
    Posts
    19
    Rep Power
    0

    Default

    I just found a post on this forum that might relates to what I am trying to do.

    I'll take it as a example and will see how it goes

Similar Threads

  1. Getting value for key in HashSet
    By Venny in forum New To Java
    Replies: 12
    Last Post: 02-02-2011, 09:42 AM
  2. Adding to a HashSet
    By Neivaed in forum New To Java
    Replies: 6
    Last Post: 12-05-2010, 07:16 PM
  3. :( anyone here plz help on HashSet
    By waklo99 in forum New To Java
    Replies: 8
    Last Post: 09-20-2010, 04:02 AM
  4. HashSet anomaly
    By jon80 in forum New To Java
    Replies: 1
    Last Post: 06-21-2009, 09:22 PM
  5. Iterating through a HashSet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-21-2008, 05: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
  •