Results 1 to 3 of 3

Thread: Hash set

  1. #1
    Join Date
    Jun 2011
    Posts
    24
    Rep Power
    0

    Default Hash set

    I have an array 'arr' with integers. I need to display only the unique values. I did this using hash array set and While executing I found an error like this ,

    Exception in thread "main" java.lang.ArrayStoreException: [I
    at java.util.AbstractCollection.toArray(AbstractColle ction.java:171)
    at test.main(test.java:57)

    Here is my logic I used to get unique values,

    Set hashSet = new HashSet(Arrays.asList(arr));
    String[] unique = (String[]) hashSet.toArray(new String[hashSet.size()]);

    Can someone help me on this??

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    6

    Default

    The toArray() method of the collection classes works somewhat unintuitively. The correct syntax is:

    Java Code:
    String[] unique = new String[hashSet.size()];
    hashSet.toArray(unique);

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,299
    Blog Entries
    7
    Rep Power
    24

    Default

    Quote Originally Posted by Iron Lion View Post
    The toArray() method of the collection classes works somewhat unintuitively. The correct syntax is:

    Java Code:
    String[] unique = new String[hashSet.size()];
    hashSet.toArray(unique);
    That's not it: the OP wants to store Integers in a String array (see the [I part in the Exception message).

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. hash table
    By pinkfahtema in forum New To Java
    Replies: 1
    Last Post: 03-28-2011, 08:25 AM
  2. hash map program
    By katturv in forum New To Java
    Replies: 1
    Last Post: 12-04-2010, 06:01 PM
  3. Hash
    By sandy1028 in forum New To Java
    Replies: 4
    Last Post: 04-17-2009, 10:36 AM
  4. Hash Map
    By rekha in forum New To Java
    Replies: 1
    Last Post: 03-21-2009, 02:00 PM
  5. Hash Table help
    By rhm54 in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 02:25 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
  •