+ 3

I need some help .. How can i check that if ArrayList contains the element or not?

How can i avoid duplicated items on the Arraylist note: items comes from sqlite. and passed from arraylist1 to arraylist2 ..

16th Mar 2019, 6:07 PM
Edrees
Edrees - avatar
8 Answers
+ 4
//I am posting link because, you can see demo code as well as explication too https://www.mkyong.com/java/java-check-if-array-contains-a-certain-value/
16th Mar 2019, 7:21 PM
Sudarshan Rai
Sudarshan Rai - avatar
+ 3
Thank you Shudarshan Rai 👑 Denise Roßberg I'll see them .
16th Mar 2019, 8:20 PM
Edrees
Edrees - avatar
+ 3
Edrees But you can add a getter to your class: public void getStr(){ return str; } In your main class: ArrayList<String> a = new ArrayList<>(); Data b = new Data("M"); a.add(b.getStr);
16th Mar 2019, 9:09 PM
Denise Roßberg
Denise Roßberg - avatar
+ 3
it is just a small example. The complete model contains (name,id,Bitmap,amount,etc).. Ithink i can get names to ArrListOf String As mentioned before then check if contains or not
16th Mar 2019, 9:18 PM
Edrees
Edrees - avatar
+ 2
An ArrayList has a contains() method. https://www.tutorialspoint.com/java/util/arraylist_contains.htm But I think it is better to use a HashSet. In this kind of list you can not add duplicates. https://www.sololearn.com/learn/Java/2182/?ref=app
16th Mar 2019, 7:59 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
It worked when i add String class directly like that ArrayList list = new ArrayList<String..its ok but>(); I think contains method can't read from model As ArrayList<data contains strings too>(); Check this : https://code.sololearn.com/c3IjFUH8VGU7/?ref=app
16th Mar 2019, 8:48 PM
Edrees
Edrees - avatar
+ 2
Edrees The problem is you don't add Strings to your List. Write: System.out.println(a) to see what is really inside your list. Edit: Data is an object. "M" is a String. So contains("M") = false.
16th Mar 2019, 8:56 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
I got it .. only Addresses
16th Mar 2019, 9:04 PM
Edrees
Edrees - avatar