Results 1 to 6 of 6

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
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    Quote Originally Posted by sameerr_007 View Post
    BUT when i restart this program and directly say hsh.contains("John");
    It gives me Unsuccessful as output...
    What do you mean by this.

    I tried this

    Java Code:
    System.out.println(hsh.contains("John"));
    It gives true. So i don't know what happened in your case. Can you post your code

  3. #3
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    What are you mean about "directly say hsh.contains("John")" looks like
    Java Code:
         public static void main(String[] arg) throws Exception {
             HashSet value = new HashSet();
             value.add("John");
             if (value.contains("John")) {
                 System.out.println("Success");
             } else {
                 System.out.println("UnSuccess");
             }
    
         }
    Skype: petrarsentev
    http://TrackStudio.com

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

    Default

    Further is the explanation of what i was trying to say
    There are two programs in one package A.java and B.java which are as follows:

    A.java:
    ------------------------------------------------------------
    package ABC;
    import java.io.*;
    import java.util.*;
    import java.lang.*;

    public class A{

    HashSet h1 = new HashSet();

    public void add(String str)
    {
    hsh.add(str);
    }

    public boolean check(String s)
    {
    if(h1.contains(s))
    return true;

    return false;
    }

    public static void main(String args[])
    {
    A a=new A();
    }

    }
    ------------------------------------------------------------

    B.java is as follows:
    ------------------------------------------------------------

    package ABC;

    import java.io.*;
    import java.util.*;
    import java.lang.*;

    public class B
    {
    public static void main(String args[])
    {

    A a1=new A();
    a1.add(args[0]);
    if(a1.check("John"))
    {System.out.println("Successful!");
    }
    }}
    ------------------------------------------------------------
    Input is given by the user...
    I) First time:
    (i) Input: "John"
    (ii) Output: "Success"
    II) Second time executing same B.java
    (i) Input: "Owen"
    (ii) Output : "Unsuccess"

    Conclusion: what i want here is when user is executing second time the same program and the value which he entered the first time should remain in HashSet. Which in my case is not happening..
    What do i do??

  5. #5
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    You should save data in a file, database or don't turn off your program ).
    When you run a program a second time you create new instance HashSet, which is empty and you will have this result.
    Skype: petrarsentev
    http://TrackStudio.com

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default

    You should also not post twice in the same forum.
    There's another copy of this in the Advanced Java Forum.

Similar Threads

  1. help using hashsets?
    By yasmin k in forum New To Java
    Replies: 1
    Last Post: 04-17-2010, 10:09 PM
  2. HashSets
    By DavidG24 in forum New To Java
    Replies: 5
    Last Post: 04-19-2009, 06:02 PM
  3. 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
  •