Results 1 to 2 of 2

Thread: Java HashSets

  1. #1
    sameerr_007 is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default Java HashSets

    Hello Everybody,
    I am using HashSets in my program to maintain hash values.
    My problem is: it works fine when i add a value in this HashSet, it works fine but

    HashSet hsh = new HashSet();
    hsh.add("John");
    if((hsh.contains("John") == true)
    {
    System.out.println("Success");
    }

    Here the output is Success...
    BUT when i restart this program and directly say hsh.contains("John");
    It gives me Unsuccessful as output...

    In short i want to make hashSets such that once an element is added should remain persistent... Hope you got my question...

  2. #2
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    Hi Sameer,

    The problem in the code is - you are not persisting the hashset after adding elements to it. So as soon as you stop the application, your whole data vanishes.

    Solution:- Persist the hashset on local file system before application quits. Then read (deserialize) the persisted file and start using it.

    May be you can use shutdown hook for serialization purpose.

Similar Threads

  1. Java HashSets
    By sameerr_007 in forum New To Java
    Replies: 5
    Last Post: 03-09-2011, 11:30 AM
  2. help using hashsets?
    By yasmin k in forum New To Java
    Replies: 1
    Last Post: 04-17-2010, 10:09 PM
  3. HashSets
    By DavidG24 in forum New To Java
    Replies: 5
    Last Post: 04-19-2009, 06:02 PM
  4. how does the remove method work for sets and hashsets
    By haridharna in forum Advanced Java
    Replies: 4
    Last Post: 08-06-2007, 12:48 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
  •