• Post Reply Bookmark Topic Watch Topic
  • New Topic

Collections  RSS feed

 
Ravi kapa
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,I have a doubt in collections.LinkedList is implementation class for List interface so whatever the behavior of List interface, LinkedList must implement List interface behavior is that right if right then What my doubt is List interface allow duplicates but LinkedList will not allow duplicates.how it is possible?...thanq

Example:

output:
LinkedList Elements
[Beginner, Java, tutorial]
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Moving this topic to the general java forum -- as this topic has nothing to do with "I/O and Streams".

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a1.add("tutorial");


BTW, what is the "a1" variable in your example?

Henry
 
Ravi kapa
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the question misplace.I am placing it now in general java...
 
Ravi kapa
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually my intention of this question was when the sub class implementing superclas(interface)then the sub class must implement the exact behavior of that super class.Like if List interface is Ordered and not allow duplicate elements then the sub classes(which implement List interface) like ArrayList,Linked list are also Ordered and allow duplicates. Is it right or wrong?correct my guess.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you read the Java Tutorials section, which will probably answer your question.

. . .


or

. . .

Apart from the additional “not”, which I presume is unintentional, . . . yes.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An implementing class must implement the contract specified by the interface.
Allowing duplicate elements is not part of the contract of the List interface. As it says in the Javadoc
lists typically allow duplicate elements. ... It is not inconceivable that someone might wish to implement a list that prohibits duplicates

As for your original question, LinkedLists do allow duplicates. If you look at your code, the first three vales are added to l1, but the supposed duplicate "tutorial" is being added to a1, which is why Henry asked you what a1 is.
 
Ravi kapa
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all of your reply's..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!