• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

K&B 310-055 Page 547

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Straight from our loving K&B book, page 547:


Why contains() method takes Object, even if I have parameterized my ArrayList with String. Only String can be added and we need not to cast what get(...) method returns, but why contains() ,indexOf(),remove() ask for Object.

indexOf() also requires Object
remove() also requires Object (another remove() asks for index though)

Please Help!!!


Regards,
cmbhatt
[ April 20, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi chandra..!

You are right. We will never be able to add anything other than string in to this list.
But the same is not ture for the contains method.It can check for any thing other than the string.As you see the following prints false.




I know that you are going to ask me the question

"If it not taking in any thing other than string, why is it checking for some thing else?"



I am very eager to know the answer.
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chandra,

Good thought.

booleancontains(Object elem)
Returns true if this list contains the specified element.

If You observe the method it will not modify the List. Thats why input "Object" is sufficient.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Srinivasan,
thats right, but when i'm making my collection typesafe, then when should i look for generalized things even though i'm sure they will never be there unless i mix this with legacy code.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I think, this matter is something related to legacy code.
All the methods (we are calling exception cases) come from the List interface, and List defines them as Object (asked parameter).


Not very sure but guessing it could be, not other reasonable cause coming to
my sleepy brain.





Regards,
cmbhatt
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question : if you are saying that the method should be like this boolean contains(String elem) instead of boolean contains(Object elem). Then the answer is backward compitiblity.


I am sorry, if i had misinterpreted the question. Tx.
[ April 21, 2007: Message edited by: Pankaj Patel ]
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pankaj Patel,


we can have like this:
boolean contains(E ele){

}
But it would be redundant code doing nothing special and breaks legacy code bec'ze specific type given priority.
[ April 21, 2007: Message edited by: Srinivasan thoyyeti ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Map too, get method asks for Object.

Pankaj, I can agree with you to some extent if not completely. Why do only some methods suffer backward compatibility?

I find Map's get(...) method, asking for Object too.

I didn't get the satisfactory answer so far!!! :roll:


Srini says,
But it would be redundant code doing nothing special and breaks legacy code bec'ze specific type given priority.



Yeah Srini, your this statement seems good.

Thank you very much!

Regards,
cmbhatt
[ April 21, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 358
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an answer..but it would be nice to make you guys think.
Did you see the public boolean equals(Object o) method? Why does it takes an object as argument?
Apply same rules here too!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic