• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

set's addd() method

 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Here why add() method allows s++ entry two times?
I thought the answer is runtime error but answer is 3. I thought ss.remove(1) gives nullPointer exception, as ss.add(s++) 2nd time in add() method gives false.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dolly,

ss.add(s++), is not adding a same value to the set two times. If you closely look into that, initially the value of s=0 and it is added i.e

first value of ss = 0 and then s is incremented to 1
second value of ss =1 and then s is incremented to 2
third value of ss=2

So we have three distinct values being added to set, and it returns true while removing all the three values and size of the set is 3, as three different elements being added.

Hope this clarifies your doubt.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dolly shah:


Here why add() method allows s++ entry two times?
I thought the answer is runtime error but answer is 3. I thought ss.remove(1) gives nullPointer exception, as ss.add(s++) 2nd time in add() method gives false.



HI.........
You are mistaken by the way add() and remove() methods works.
Its actually due to AutoBoxing feature.When we supply s++ as agument to add() method{i.e add(s++)},it will automatically create an object and add it to "set".

Now ,your code has got some remove() method.
remove() will remove an object from "set" if it exists and returns true.
If it doesnt exist ,it will return false and does nothing.

So in your code the remove() methods are ineffective,size of "set" remains "3".

Hope you got it.
Regards,
Mack.
 
Mack Stevens
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember friends,these concepts come under Collection Framework.In this we add objects to the collection, not primitive values.so that you can place heterogenous object in any collection.
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems like it's smart enough to autobox and add it to the set, however it is not smart enough to autobox and remove from the set if you don't specify it's (short). I tried ss.remove((short)0); and that seems to do the job. Is that more like a security feature or?
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi when you write:
ss.remove(0);
0 is an int. It will be boxed to an Integer.
Because the elements of your Set are Short(and not Integer), it will not be removed from the Set

Note that the method remove don't throw NullpointerException if the
element that you want to remove don't belong to the Set:
If the element belong to the Set it will be removed and return true
If not it will return false.

If you write a code like this:


The result will be 0;
To have 0 with your code you ave to cast your values:


[ September 02, 2007: Message edited by: Collins Mbianda ]
 
Mack Stevens
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony Smith:
Seems like it's smart enough to autobox and add it to the set, however it is not smart enough to autobox and remove from the set if you don't specify it's (short). I tried ss.remove((short)0); and that seems to do the job. Is that more like a security feature or?



..............................................
Thanks for your work on it,what you said is true.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

however it is not smart enough to autobox and remove from the set if you don't specify it's (short)



Let me try. This about "not being smart enough to autobox" seems strange to me. If I look at documentation for Set it says:

boolean add(E o);
boolean remove(Object o);

If I am correct this actually means that add method is generic and remove is not. So we can pass as arguments to add method just shorts. It than autoboxes them into wrapper Short.

When we use remove method we can pass whatever we want. In our case we are passing 0, 1 and 2 to remove methods. Those literals are of type int and are autoboxed into Integer. Our Set holds Short objects, Integer do not match and false is returned. It is the same if we pass String to remove method.

When we use ss.remove((short)0); 0 is autoboxed into Short which match our object in collection, object is removed and true is returned.
 
Skool. Stay in. Smartness. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic