How to Find an Object in an Arraylist by Property

Searching in a ArrayList with custom objects for certain strings

The easy way is to make a for where you verify if the atrrtibute name of the custom object have the desired string

    for(Datapoint d : dataPointList){
if(d.getName() != null && d.getName().contains(search))
//something here
}

I think this helps you.

How to search for an Object in ArrayList?

created the list and the search item:

List<String> list = new ArrayList<String>();
list.add("book");
list.add("pencil");
list.add("note");

String itemToBeSearched = "book"; // taken as example

if(check(list,itemToBeSearched)){
System.out.println("ITEM FOUND");
}
else
{
System.out.println("NOT FOUND");
}

then the item check function is

public static boolean check(List<String> list, String itemToBeSearched){
boolean isItemFound =false;
for(String singleItem: list){
if(singleItem.equalsIgnoreCase(itemToBeSearched)){
isItemFound = true;
return isItemFound;
}
}
return isItemFound;
}

and it's working for me, please try this and let us know :)

Search for a particular element from object in ArrayList

Using for loop over ArrayList can solve your problem, it's naive method and old fashioned.

Below is the code for it.

import java.util.ArrayList;

public class HelloWorld{

public static void main(String []args){
String author_name = "abc";
ArrayList<book> bk = new ArrayList<book>();
bk.add(new book("abc", "abc", "abc", 10));
bk.add(new book("mno", "mno", "abc", 10));
bk.add(new book("xyz", "abc", "abc", 10));
ArrayList<book> booksByAuthor = new ArrayList<book>();
for(book obj : bk)
{
if(obj.author_nm == author_name)
{
booksByAuthor.add(obj);
}
}

}
}

class book{
public String book_nm;
public String author_nm;
public String publication;
public int price;
public book(String book_nm,String author_nm,String publication,int price){
this.book_nm=book_nm;
this.author_nm=author_nm;
this.publication=publication;
this.price=price;
}
}

Hope you can get an idea from it.



Related Topics



Leave a reply



Submit