• Post Reply Bookmark Topic Watch Topic
  • New Topic

add data to list if data not exist  RSS feed

 
wesley johnsen
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have a list, you can add data to the list.
But the meaning is if the data exist in the list then you can not add data to the list.
How can I make this?

I have code but this is not working.

thanks in advance

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post code that will actually compile, otherwise you will get comments about things that you are not asking about. For example, as given, this code will not compile because the class name and the constructor name don't match.

Check out the List API documentation: https://docs.oracle.com/javase/7/docs/api/java/util/List.html

There is a method in there that you can use to check if an element is already in the list.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wesley johnsen wrote:But the meaning is if the data exist in the list then you can not add data to the list.
This is quite confusing.
did you mean if the same data exist in the list then list should not allow for addition of duplicate data.
Or
any data should not be added if list contains some data?

for the first case why don't you choose a set instead,it will not add any duplicate data--->don't forget to override equals() and hashSet().

if you really want to solve the problem with list,try using the list.contains() in your addToList() method-it would buy you some simplicity,abstraction(by refactoring your for loop).
ADVICE:please use readable and intensional name for variable and methods.addToList just looks like that it will simply add to the list it should be like ConditionalAdd() or something else you like.i really find it hard to parse meaning of "boekenlijst",Try some other readable names,These things would make your code look better and readable.

Kind Regards!
Praveen.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:ADVICE:please use readable and intensional name for variable and methods.addToList just looks like that it will simply add to the list it should be like ConditionalAdd() or something else you like.i really find it hard to parse meaning of "boekenlijst",Try some other readable names,These things would make your code look better and readable.

The OP is Dutch and boekenlijst means booklist in English.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Or, as someone else mentioned, use a Set instead of a List. A Set will guarantee that duplicate objects are not in the Set.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:. . . The OP is Dutch and boekenlijst means booklist in English.
Is that two words? Shou‍ld it read boekenLijst?

I can see a problem about that field. I think it is in the wrong place. I don't think it shou‍ld be in the Book/Boek class at all. Books do not keep a list (or a set) of themselves. If there is such a list/set, it would live in a Library/Bibliotheek class. What is happening now is that you are creating a new list every time the Book class is instantiated, and adding that book to the list. But that means each book has a list containing one book. If you had a Library/Bibliotheek class, you would have the list/set in its objects, and you would add each book to the library and thereby to the library's list (or set).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!