Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching in a ArrayList with custom objects for certain strings

I have a ArrayList with custom objects. I want to search inside this ArrayList for Strings.

The class for the objects look like this:

public class Datapoint implements Serializable {    private String stateBased;   private String name;   private String priority;   private String mainNumber;   private String groupadress;   private String dptID;    public Datapoint(){   }    public String getMainNumber() {     return mainNumber;   }    public void setMainNumber(String mainNumber) {     this.mainNumber = mainNumber;   }    public String getName() {     return name;   }    ..and so on 

I know how to search for a string in a ArrayList but how to do that in a ArrayList with my custom objects:

ArrayList<String> searchList = new ArrayList<String>(); String search = "a"; int searchListLength = searchList.size(); for (int i = 0; i < searchListLength; i++) { if (searchList.get(i).contains(search)) { //Do whatever you want here } } 

So I want to have a function to search in my ArrayList with for example five objects for all "name" strings.

like image 218
Mokkapps Avatar asked Sep 19 '12 13:09

Mokkapps


People also ask

How do you find a specific object in an ArrayList?

To check if ArrayList contains a specific object or element, use ArrayList. contains() method. You can call contains() method on the ArrayList, with the element passed as argument to the method. contains() method returns true if the object is present in the list, else the method returns false.

Can you use .contains on an ArrayList in Java?

contains() in Java. ArrayList contains() method in Java is used for checking if the specified element exists in the given list or not. Returns: It returns true if the specified element is found in the list else it returns false.

Can you sort an ArrayList of strings in Java?

Collections.An ArrayList can be sorted by using the sort() method of the Collections class in Java. It accepts an object of ArrayList as a parameter to be sort and returns an ArrayList sorted in the ascending order according to the natural ordering of its elements.


2 Answers

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.

like image 75
Renato Lochetti Avatar answered Sep 18 '22 15:09

Renato Lochetti


UPDATE: Using Java 8 Syntax

List<DataPoint> myList = new ArrayList<>(); //Fill up myList with your Data Points  List<DataPoint> dataPointsCalledJohn =      myList     .stream()     .filter(p-> p.getName().equals(("john")))     .collect(Collectors.toList()); 

If you don't mind using an external libaray - you can use Predicates from the Google Guava library as follows:

class DataPoint {     String name;      String getName() { return name; } }  Predicate<DataPoint> nameEqualsTo(final String name) {     return new Predicate<DataPoint>() {          public boolean apply(DataPoint dataPoint) {             return dataPoint.getName().equals(name);         }     }; }  public void main(String[] args) throws Exception {      List<DataPoint> myList = new ArrayList<DataPoint>();     //Fill up myList with your Data Points      Collection<DataPoint> dataPointsCalledJohn =             Collections2.filter(myList, nameEqualsTo("john"));  } 
like image 35
munyengm Avatar answered Sep 18 '22 15:09

munyengm