搜索在ArrayList中与某些字符串自定义对象(Searching in a ArrayList

2019-06-18 05:54发布

我有一个自定义的对象的ArrayList。 我想此ArrayList弦乐内搜索。

为对象的类是这样的:

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

我知道如何搜索字符串中的ArrayList,但如何做到这一点与我的自定义对象的ArrayList:

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
}
}

所以我想有一个函数在我的ArrayList与搜索例如五个对象为所有“名”的字符串。

Answer 1:

最简单的办法是让一个for在那里你验证,如果atrrtibute name自定义对象的有希望的字符串

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

我认为这可以帮助你。



Answer 2:

更新:使用Java语法8

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());

如果你不介意使用外部libaray -您可以使用谓词从谷歌番石榴库如下:

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"));

}


Answer 3:

试试这个

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


Answer 4:

大概是这样的:

ArrayList<DataPoint> myList = new ArrayList<DataPoint>();
//Fill up myList with your Data Points

//Traversal
for(DataPoint myPoint : myList) {
    if(myPoint.getName() != null && myPoint.getName().equals("Michael Hoffmann")) {
        //Process data do whatever you want
        System.out.println("Found it!");
     }
}


Answer 5:

对于定制类藏品中正常工作,你必须实现/重写equals()之类的方法。 对于还排序重写compareTo()

请参阅这篇文章或谷歌如何正确地实现这些方法。



Answer 6:

contains()方法只是调用equals()ArrayList元素,所以你可以重载类的equals()基础上, name类变量。 返回trueequals() ,如果name等于匹配的String 。 希望这可以帮助。



Answer 7:

使用Apache CollectionUtils

CollectionUtils.find(myList, new Predicate() {
   public boolean evaluate(Object o) {
      return name.equals(((MyClass) o).getName());
   }
}


Answer 8:

String string;
for (Datapoint d : dataPointList) {    
   Field[] fields = d.getFields();
   for (Field f : fields) {
      String value = (String) g.get(d);
      if (value.equals(string)) {
         //Do your stuff
      }    
   }
}


Answer 9:

boolean found;

for(CustomObject obj : ArrayOfCustObj) {

   if(obj.getName.equals("Android")) {

      found = true;
   }
}


文章来源: Searching in a ArrayList with custom objects for certain strings