Set.contains returns false even when the set contains the element

As per java documentation, set.contains(o) returns true if and only if this set contains an element e such that (o==null ? e==null : o.equals(e)). In my case it doesn't (I have tested with Java 1.50_09 and Java 1.6.0). Look at the small toy example below. The last assert statement fails. But as per the documentation, it should not, since an element sc exists in the set such that sc.equals(sc2).
Any help will be greatly appreciated.
thanks
Nilesh
import java.util.HashSet;
import java.util.Set;
class StringCapsule {
    String s;
    public StringCapsule(String s) {
        this.s = s;
    public boolean equals(StringCapsule s2) {
        return s.equals(s2.s);
public class TestSets {
    public static void main(String[] args) {
        Set<StringCapsule> set = new HashSet<StringCapsule>();
        String s = "test";
        StringCapsule sc = new StringCapsule(s);
        StringCapsule sc2 = new StringCapsule(s);
        set.add(sc);
        assert (set.contains(sc));
        assert (sc.equals(sc2));
        assert (set.contains(sc2)); // THIS FAILS
        System.out.println("Finished with set size " + set.size());
}Copy above code to TestSets.java, and compile and run to reproduce
javac TestSets.java
java -ea -cp . TestSets

If you have generics available, you should have annotations as well. One way to avoid this kind of problem in the future is to use
@Override
public boolean equals (Object o) when you know you want a method to override it's parent.

Similar Messages

  • File.exists() returns false even when the file exists

    hi,
    the exists() function of java.io.File function returns false even when the file exists on the file system. why is this? i checked the path of the file which gives me the correct path, but exists returns false. can anyone help?

    post some of the code you�re using - then maybe I can help you out
    //Anders

  • My ipod keeps searching for network, even when the network info is all set properly. It has suddenly started doing this. Also, buffering forever...

    My ipod keeps searching for network, even when the network info is all set properly. It has suddenly started doing this. Also, buffering forever... I have tried to reset the network few times, but not working.

    Hello Nazer_502,
    I would be concerned too if my iPhone was acting this way.  It sounds like it is overheating and not holding a charge. I recommend following the steps listed below when experiencing an issue like this:
    Battery life seems short
    Click ( www.apple.com/batteries/iphone.html) for tips on how to prolong iPhone's battery life.
    Try turning iPhone off and then on again.
    Connect iPhone to iTunes and restore iPhone.
    If the screen shows a low-battery image, the battery is low on power and needs to charge for up to ten minutes before you can use it.
    When charging iPhone, make sure to leave it charging until it is fully charged. You'll know the battery is fully charged when the battery icon in the upper-right corner of the screen looks like this .
    Note: When charging using a computer, don't connect iPhone to a keyboard. Also, the computer must be turned on and not in sleep or standby mode. If iPhone is connected to a computer that's not turned on or is in sleep or standby mode, the iPhone battery may drain.
    You can find the full article here:
    iPhone: Hardware troubleshooting
    http://support.apple.com/kb/ts2802
    Thank you for using Apple Support Communities.
    Best,
    Sheila M.

  • My 4s continues to lose wi-fi and reverts to 3G even when the phone is 4 ft from the router.

    My 4s continues to lose wi-fi and reverts to 3G even when the phone is 4 ft from the router. Is this a defect in my phone? It happens when I am home and I switch to my wi-fi. It doesn't matter where the phone is in relation to the router. Please note, I have my phone set to NOT sleep. Does any one know why my phone reverts to 3G? I have to manually set it to wi-fi every time this happens.

    I have the same issue with my iPhone 4s. It was not an issue when I was using iOS5, but it started once I upgraded to iOS6 and the recent update of iOS6.0.1 did not fix it. Judging from the release notes, Apple thinks this is only an iPhone 5 issue, as they say they fixed it for iPhone 5's.
    I have an iPad 2 (iOS6.0.1) and a 3rd generation iPod touch (latest iOS5 release), as well as an Apple TV 2. None of the other devices have this problem and they are all on the same network.
    Something interesting... my iPhone 4s works fine on the wifi where I work. At home, it continually turns on and off. My home network is using a Netgear router (WNDR3400) and the iPhone is connecting to it using WPA2+PSK protocol. Hopefully, this information can help Apple figure out the problem.
    (I remember my iPod Touch having a similar issue a few years ago with iOS 4, when I had a different Netgear router using the same security protocol. I think iOS 5 fixed it for the iPod Touch... I wonder if some Apple products have an issue with Netgear Routers using that encryption.)

  • Persisting a session even when the browser is closed

    i want to persist a user session even when the browser is closed. How to do it through cookies. I know i define a cookie, set its max age and then put that in response. But its not working.
    I am using I.E

    i am not sure if i am doing something wrong. the cookies are enabled and i see that for ie 7, if i open a new tab the session is maintained but if i open a new window (of I.E. 7) altogether then the session is not maintained. Interestingly the code is working in firefox. is there some problem with the I.E. only?

  • Help! iWeb won't open properly after I updated to Yosemite. No old websites, no workspace- nothing is displayed even when the app is open. HELP!

    Help! iWeb won't open properly after I updated to Yosemite. No old websites, no workspace- nothing is displayed even when the app is open. HELP!

    To open your domain file in Lion, Mountain Lion or Mavericks or to switch between multiple domain files Cyclosaurus has provided us with the following script that you can make into an Applescript application with AppleScript Editor. Open Script Editor, copy and paste the script below into Script Editor's window and save as an application.
    do shell script"/usr/bin/defaults write com.apple.iWeb iWebDefaultsDocumentPath -boolean no"
    delay 1
    tell application "iWeb" to activate
    You can download an already compiled version with this link: iWeb Switch Domain. Note:  you may have to Control (right) - click on the app after uncompressing and select Open due to the Sandboxing feature of OS X.  If you have problems with the app create your own with AppleScript Editor.
    Just launch the application, find and select the domain file in your Home/Library/Application Support/iWeb folder that you want to open and it will open with iWeb. It modifies the iWeb preference file each time it's launched so one can switch between domain files.
    WARNING: iWeb Switch Domain will overwrite an existing Domain.sites2 file if you select to create a new domain in the same folder.  So rename your domain files once they've been created to something other than the default name.
    NOTE: In Lion and Mountain Lion the Home/Library folder is now invisible. To make it permanently visible in Lion or Mt. Lion enter the following in the Terminal application window: chflags nohidden ~/Library and press the Return key - 10.7: Un-hide the User Library folder.
    For Mavericks and Yosemite go to your Home folder and use the View ➙ Show View Options menu to bring the this window:

  • Save as does not Create PDF Compatible File even when the creare pdf compatible files is on, Illustrator CC 2014

    save as does not Create PDF Compatible File even when the creare pdf compatible files is on, Illustrator CC 2014

    When I open is adobe acrobat I get this message.
    "This is an Adobe® Illustrator® File that was
    saved without PDF Content.
    To Place or open this  le in other
    applications, it should be re-saved from
    Adobe Illustrator with the "Create PDF
    Compatible File" option turned on. This
    option is in the Illustrator Native Format
    Options dialog box, which appears when
    saving an Adobe Illustrator  le using the
    Save As command."

  • Credit exposure updating even when the sales order is blocked

    Hi,
    We are using FSCM credit management. We observed that the credit exposure is updated even when the sales order is blocked. When we investigated it is found out that SD has removed the requirement 101 in the subsequent functions. The reason is they want the confirmed quantity to be updated even though the order is blocked. This will allow the Material Planning to go ahead and purchase the goods. Is there a way around it where we can have MM see the confirmed quantity in the order and it will not updated the credit exposure?
    Thanks and Regards
    Madhu Vutukuri

    Hi Sravan,
    Please let us know, whether the document is really getting reflected in the blocked documents list for a particular kind of block and you are releasing that particular block in the specified area as we have different areas for releasing blocks. Chances are that in the mean time, the concerned block might have been removed in the system and not updated as it might have been. It will be of great help, if you can provide us with the screenshots of the process followed.
    Regards,
    Aman

  • How do I stop all of my music in my itunes library syncing to my iphone 5 even when the songs aren't ticked?

    How do I stop all of my music in my itunes library syncing to my iphone 5 even when the songs aren't ticked?

    The frustrating part about the restore is that is seems to be finished when it in fact is not.  The restore will look "finish" and your phone will be at the Swipe to Unlock screen. If you swipe and look at your home screen at this point of the restore, you'll see everything is not quite right.  While the process seems to be not doing anything just leave the phone attached and do something else for about an hour.  This part of the process is not mentioned during the restore operation.  I believe there are about two steps to the restore process: 1) restore the phone settings (ringtones, background, notification settings, etc.) and give the user the unlock screen; 2) without letting the user know there is still more to do, begin the slow process of restoring all the music, movies, apps and what not.  It's this second step that causes the confusion.  Nothing lets you know to wait that process out.  I've restored a couple devices from a computer backup and the 2-step process looks the same.

  • It has been only about a month that i bought my macbook pro 15 but it is heating up really fast on the left side even when the cpu usage is only 10%...i'm worried if this is a problem of the hardware.

    it has been only about a month that i bought my macbook pro 15 but it is heating up really fast on the left side even when the cpu usage is only 10%...i'm worried if this is a problem of the hardware?

    The way something "feels" is very subjective.  We would need to know your actual temps.  You can install the free iStat Pro widget which is available as a download directly from Apple:
    http://www.apple.com/downloads/dashboard/status/istatpro.html
    Let us know what CPU temps you're seeing at the time you feel your machine is hot.  As you indicated your CPU usage was only 10%, that would imply that you already know that heavy CPU usage would be one cause for your machine to be running on the warm side... but that doesn't seem to be the cause here.

  • My iphone 4s got water in the headphone port. I put my phone in rice for 24 hours and the LED light is on. It stays on even when the phone is off. How can I fix it with out spending 200 bucks at apple to have them fix it?

    My iphone 4s got water in the headphone poert. I put my phone in rice for 24 hours and the LED is on. It stays on even when the phone is off. How can i fix it without taking it to apple and spendding 200 dollars to have them fix it?

    Short answer is you probably cannot.  Once water gets inside the device and it is powered on, the damage likely is permanant. 

  • Is it true that music pauses when a phone call comes in even when the ios6 'do not disturb' feature is activated?

    is it true that music pauses when a phone call comes in even when the 'do not disturb' feature is activated?
    thanks in advance

    You could test it out and let us know!
    Best of luck.

  • When aligning to center, a white line of space is there even when the artwork fits to artboard

    This is what happens when i have multipel artboards and i align to center, some of the artboard have this white space, even when the artworks themselvs fit perfectly.
    when i move the artboard, so does the white space
    another funny thing is when i align to center of artboard the art is my mask moves so the art is simply not in center anymore.
    last but not least, even then the smartguides tells me it is aligned with the other box, it looks fine and all but when i let go to place it, it moves down under or above.. and both of lines have been exported, Align to Pixel Grid is off, i have uncheck align new objects to pixel grid and unchecked the all snap to..
    Anybody knows what is going on?

    I don't understand the problem. Please post the complete screenshot. And possibly the outline view.

  • Adware has access to system even when the site is blocked.

    ad.yeildmanager.com and all of its' buddies have free access to my system even when the sites are blocked. It only seems to occur when I am at Yahoo and spammers were using my account.
    Norton, McAfee, MS, and Pest Patrol find no bugs and show life is great.
    Every morning I show new program installed but no listing for it.
    I love the browser but

    This new 'feature' is controlled by these two prefs.
    *security.mixed_content.block_active_content
    *security.mixed_content.block_display_content
    *https://developer.mozilla.org/Security/MixedContent
    *https://addons.mozilla.org/firefox/addon/toggle-mixed-active-content/

  • My macbook wakes up and then goes back to standy even when the lid is open.

    so my macbook turns on and then the screen stays on for like 2 seconds then goes back to standby. it does not shut down because when i wake it up again by pressning any button, it is where i was before. Also when it is in standy even when the lid is on, the standby light blinks as if the lid is closed. i can never use it because the screen only stays on for 2 seconds then turns off going to stand by. does anyone have a similar problem or know how to fix this?
    thanks in advance!

    mkedge wrote:
    it worked for snow leopard.
    now, in Lion, when you open the lid, it automatically detects that you opened and it turns on macbooks display
    That's called progress!

Maybe you are looking for

  • My Iphone 5 is not showing in Itune 11.0.4.4 but it show in windows when I connect it to charge

    My Iphone 5 is not showing in Itune 11.0.4.4 but it show in windows when I connect it to charge

  • Error in Invoice process

    Hello Experts, I need your help. I had an requirement from the client to capture the COGS details are in transit. Created new condition type ZVRS, copy of VPRS. Assigned the Account key and G/L accounts in VKOA. Created the order and ZVRS value calcu

  • Information on SAP CRM 7

    Hello I am really new to SAP CRM 7.0. Although i have been working on ABAP for past 4 years now, i am finding it really difficukt to get a hold on CRM now. Can anyone please suggest me the books\links and stuff to learn the software in a more efficie

  • Screen slides to the right

    Ever since I downloaded and installed Lion the screen at the flick of the mouse will move to the right exposing another desktop with widgets and other stuff.   The only way to get back to my regular desktop is to click on the arrow at the lower right

  • Type FLTP stores data with E+

    Hi, I created key figures of type FLTP, accurate to 8 bytes and of type number. When I load data, I notice the data content of these key figures or IO that of this type store the data with numbers as, for example, ....E00 or ....E07. Is this normal?