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
How to Create Unique Column With Jpa
Java Coding Converted to Pseudo Code
How to Do a Limit Query in Jpql or Hql
Spring Boot JPA Unknown Column in Field List
Spring Boot Could Not Resolve Placeholder in String
How to Open Excel File Using JavaScript Code Without Using Activex Control
Spring Boot Rest API Returns 404
How to Test a Void Method Using Junit And/Or Mockito
Bootstrap.Yml Not Loading in Spring Boot 2
Connection Java - Mysql:Public Key Retrieval Is Not Allowed
Arraylist - How to Check If an Index Exists
Could Not Extract Resultset in Hibernate
Field Required a Bean Which Could Not Be Found in Springboot
On Selenium Webdriver How to Get Text from Span Tag
Loop Over All Fields in a Java Class
Java.Lang.Noclassdeffounderror: Org/Json/Simple/Parser/Parseexception With Eclipse and Spring
How to Return a Select Query from JPA Repository With Foreign Key Type