Why we does not have List.contains(E o) List.remove(E o)

List class is having
bool contains(Object o)
bool remove(Object o)
I was wondering why the List class doesn't have
bool contains(Object o)
bool remove(Object o)
Isn't user will not protected by compiler if they try to do some wrong stuff?
List<String> list = new ArrayList<String>();
list.remove(new Integer(123)); // I expect compiler to prevent us to do this???
Thanks!
Cheok

Peter__Lawrey wrote:
KwangHooi wrote:
Isn't user will not protected by compiler if they try to do some wrong stuff?User's should be protected, but developers need to understand what they are doing.
List<String> list = new ArrayList<String>();
list.remove(new Integer(123)); // I expect compiler to prevent us to do this???That assumes this is an error. The compiler could only determine trivial, pointless cases.
As a developer you need to be able to determine much more complex cases than the compiler ever could.I do not agree with you.
Generics feature in Java allow "a type or method to operate on objects of various types while providing compile-time type safety." When we mention about "compile-time type safety", we means the code may be compiled without errors and warnings and does not raise any unexpected ClassCastExceptions at runtime.
Cosider the following code before we are having generic feature :-
List v = new ArrayList();
v.add("test");
Integer i = (Integer)v.get(0);We will get java.lang.ClassCastException while trying to execute the third line. However, by using generic, programmers are protected from making mistake
List<String> v = new ArrayList<String>();
v.add("test");
Integer i = v.get(0);Compiling the third line of this fragment with J2SE 1.5 will yield a compile-time error because the compiler will detect that v.get(0) returns String instead of Integer.
So, a class designer, shall design the class in such a way that, "easy to be used, difficult to be mis-used"
Come back to the original question :-
If we already have a type safe
public boolean add(E e)
Why cann't we have
public boolean contains(E e)
public boolean remove(E e)

Similar Messages

  • Why Set does not have get() method

    Can some one tell me why set does not have get method?...

    user12203354 wrote:
    ya u are right that designer of this class thought there is no implementation required for get but why he/she thought like this...
    there must be some valid reason for the same.Because a Set's purpose is not to keep things in order; it's to ensure there are no duplicate entries. Set would require extra code to be able to keep track of an order.
    Note that there is a Set implementation that does keep track of insertion order--LinkedHashSet--but it doesn't have a get() method.

  • Is there a version of Firefox still available that does not have plugin-container.exe included?

    I have faithfully updated Firefox on my Windows XP system and now use Firefox/3.6.16. I also run Photoshop Elements 6.0, which requires quite a bit of memory. It is currently getting slowed down because plugin-container.exe is consuming anywhere from 25% to 50% of my memory. If I use Task Mgr to shut down this task, things are much better. Is there a downloadable version of Firefox that does not include plugin-container.exe, and if so, can I replace my current version with that one?

    To LOVEfx4 and Mozilla community. Have Windows XP and had
    Firefox 3.16.6 Since installing Firefox 4 I have had nothing but slowdowns whenever I try to download any sites with Java. Any explanations why? Sorry, I cannot find any threads for Firefox 4.

  • Why IVisualElementContainer does not have "getChildren" method defined?

    Hi all,
    In Flex 3 SDK Container.as has method getChildren() and rawChildren property typed as IChildList.
    But In Flex 4 the interface IVisualElementContainer does not have a definition for getChildren() > thus neither SkinnableContainer nor Group have this method defined.
    Was this decision made deliberately to simplify the interface API?
    I find it convenient to have "getChildren" method implemented inside Group and SkinnableContainer,  for example this can be useful if I want to access all visual childs of those containers to pass them along to some manager class or to dynamically set some common property of all childs ( e.g visibility).

    MX had a concept of two sets of children: regular children and raw children.  One component could have two children lists.
    Spark simplifies this concept because of the skin.  Now there's really only one set of children.  Because of this IVisualElementContainer acts like IChildList and has a very similar API.  You should be able to use IVisualElementContainer as you would IChildList before.
    Note that in places where we do have more than one-set of children, we don't expose them on the main object, but require you to dig down in to the separate object (the skin or the controlBarGroup for example).
    Hope that helps,
    Ryan

  • Why ipad does not have WHATSAPP as well as iphone?

    Why ipad doesn't have WHATSAPP as well as iphoe?

    Ask the developers of the app, nothing to do with Apple.

  • Why Namoroka does not have the Upgrade button?

    Does this happen on you machine?
    http://omploader.org/vNDNvMw

    because it's supposed to be updated with pacman (especially as this is an unofficial build so you can't update via mozilla)?

  • TS3297 my itunes wish list does not have a purchase allbutton why not it did before

    My itunes wish list does not have a purchase all button anymore why not?

    You don't think we've all done that?.............numerous times!
    Ever since this change came out I sent feedback the very next day and several times since.
    We've had iTunes updates since and Apple just doesn't seem to think it's a big deal.
    Why would they get rid of it to begin with?
    I don't purchase a can of beans, one bean at a time.

  • On my CC drop down menu why is it only Lightroom app that does not have CC

    I have historically used Lightroom as a single purchased product and recently I purchased Creative Cloud.
    As a new user I am very unfamiliar with things but I noticed On my Creative Cloud drop down menu containing a list of all my apps I noticed that each of my apps conclude with CC at the end.
    with other words
    Photoshop CC
    Bridge CC
    Edge Inspect CC
    Scout CC
    Lightroom NOTHING ?
    On my CC drop down menu why is it only Lightroom app that does not have CC at the end of Lightroom.
    Has the system recognised my old Lightroom and it has not installed a CC version or is this the same for everybody out there and if so why?
    Why no CC on the drop down menu after the word Lightroom?

    Why no CC on the drop down menu after the word Lightroom?
    Nothing to worry about. It's the same for everyone and it's normal.
    Lightroom does not follow the Cloud version numbering (yet). It's always had its own version numbering.
    When the Cloud was first released there was no Lightroom included.
    Adobe added Lightroom to the Cloud later but never added CC to the numbering. So yet another layer of confusion was added to the versioning of Cloud software.
    There are now two distinct versions of Lightroom:
    Lightroom 5.6 (perpetual license, does not include Lightroom Mobile)
    Lightroom 5.6 (Cloud which includes Lightroom Mobile)

  • Contact list does not have iPhone listing

    My iPhone 4S does not have an iPhone option in the Contact list (just lists 'mobile'), sometimes it will recognize someone else's text but not always, what can I do?

    So why in my Contacts list some iPhones users are noted as iPhone and others are listed as mobile......where did the difference come in? I have never had this connected to my Mac Book Pro at home, except to charge

  • Why when I try to login the Itunes Connect with my Apple ID it always says: "Apple ID does not have permission to access iTunes Connect."?

    Why when I try to login the Itunes Connect with my Apple ID it always says: "Apple ID does not have permission to access iTunes Connect."?

    Look on one of your bank statements and be sure to enter the billing address exactly as it shows on the statement. Some folks forget where they actually get the statement. They put their home address in iTunes/Mac App Store and they actually get the statement at their place of employment or vice versa.

  • Is there a way to remove my facebook contacts from my phone contacts list?  I had an iphone 4 and it was easy to shut off from teh contacts through the groups link at the top left, but the iphone 5s does not have this feature nor can i figure it out throu

    Is there a way to remove my facebook contacts from my phone contacts list?  I had an iphone 4 and it was easy to shut off from teh contacts through the groups link at the top left, but the iphone 5s does not have this feature nor can i figure it out through the settings feature or anything else....HELP please as it is annoying to see all of these contacts!

    Hello ymprice91277, let's solve this mystery! To unlink the Facebook contacts go to Settings, Facebook, turn off contacts.
    WiltonA_VZW
    VZW Support
    Follow us on twitter @VZWSupport

  • HT2499 I have a new MacBook Pro with OS X Lion 10.7.4. The DVD player does not have a feature to store film clips. Why? This is useful for my work and I had it on my older computer? Why the loss of capacity?

    I have a new MacBook Pro with OS X Lion 10.7.4. The DVD player does not have a feature to store film clips. Why? This is useful for my work and I had it on my older computer? Why the loss of capacity?

    I cannot recall the DVD player ever being a place to "store" videos - it is simply an app to play videos.
    If your machine is new, then iMovie should be included as the "shrunk" version of iLife is included with Lion (iMovie, Garageband, and iPhoto). However, iMovie is not a place to store videos either - it is a video editor. You import your clips and edit them in order to then burn to a DVD or share via a social media site.
    Have you considered simply dragging the clips to your folder "Movies"? That would be a great place to just store them (please be aware that videos are usually large files, so keep your hard drive space in mind so you keep a minimum free and available for the OS to operate properly):

  • I was wondering why the iPhone 5 has the option for a battery percentage and the iPod touch 5g doesn't? I mean... they are almost both the same thing... just the iPod does not have the phone.

    I was wondering why the iPhone 5 has the option for a battery percentage and the iPod touch 5g doesn't? I mean... they are almost both the same thing... just the iPod does not have the phone.

    One likely reason is the % battery is not that accurate because of the smaller battery capacity in the iPod compared to iPhone and iPad. Users why use a % battery app post that the % reading changes a lot.

  • Good afternoon Apple Store. I'd really like to ask why my purchase does not have that engraving wich I made out in order??

    Good afternoon Apple Store. I'd really like to ask why my purchase
    does not have that  engraving wich I made out in order?? 
    Regards
    Anton Fedchenko
    <Order Number Edited by Host>

    Try
    http://store.apple.com/us/help/contact?mco=MTM3NDgzMDc
    If you are not in the USA, click the 'Change country' link at the very bottom and select your country. this will take you to the Store page for that country - at the very bottom right you will find a phone number.

  • List the items the user does not have access to.

    I have a requirement to show the user a list of all the libraries of a site with the number of items in each even if the user does not have rights to the library. (the link or button will be grayed out if the user has no access)  
    I am looking for opinions on how to do it with the best performance possible.
    The first candidate is to first enumerate all the items in all the libraries with elevated then do the same as the logon user and compare the collections.
    The drawback is performance so maybe rely on the assumption that the overall content of the site does not change often so keep the collection from the elevated run in cache (with configurable lifetime) and only run as user on page load and compare with the
    cache.
    I am sure there is probably a better way so let's here it. can we take advantage of search?
    This is on SP2013 but we are not using apps for unrelated reasons. We can use server object model.
    Thank you all.
    Ofer Gal

    Hi,
    I believe Search cant be used since Search is Security Trimmed. Search will not return the result if user dosent have the permission. 
    Thanks! Best Regards, Prasham Sabadra http://prashamsabadra.blogspot.in

Maybe you are looking for