H35am
H35am

Reputation: 818

Find element in ArrayList Java

I have trouble finding elements, here is my code:

 public static void main(String[] args) {
    BufferedReader br = getFileReader("reader.csv");

    ArrayList<Monitoring> col = getCollection(br);

    //sort the collection on 'beginTime'
    for (Monitoring x : col)
        System.out.println(x.toString());
    BeginTimeComparator beginTime = new BeginTimeComparator();
    Collections.sort(col,beginTime);
    System.out.println("Begin time:");
    for (Monitoring x : col)
        System.out.println(x.toString());

This is the part I have trouble with, I don't know how to search en get back the object with endTime 2015-03-10. BTW this is one line of cvs data:

UnitId;BeginTime;EndTime;Type;Min;Max;Sum

14100072;2015-03-10 07:12:20;2015-03-10 7:13:20;Gps/GpsAccuracyGyroBias;0;0;0

//find the amount of elements that were sent on 'endTime' = 2015-03-10 (just the date)
    EndTimeComparator endTime = new EndTimeComparator();
    String findThis = "2015-03-10";
    Collections.sort(col, endTime);

    for(Monitoring x : col){
             if(x.getEndTime().equals(findThis)){
                 System.out.println("Here is 'endTime= 2015-03-10' :");
                 System.out.println(x.toString());

             }
    }

I have tried this but both didn't work:

int index = Collections.binarySearch(col, findThis.toString(), null);
System.out.println("Here is 'endTime= 2015-03-10' :");
System.out.println(index);

Upvotes: 6

Views: 2942

Answers (3)

Robin Gordijn
Robin Gordijn

Reputation: 685

Guessing that getEndTime() returns a LocalDateTime you can't compare a string with a type of LocalDateTime. You could try to parse the LocalDateTime to LocalDate and fill the 'findThis' variabel with a type of LocalDate.

Because code says more than a 1000 words:

EndTimeComparator endTime = new EndTimeComparator();
Collections.sort(col, endTime);

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate findThis = LocalDate.parse("2015-03-10", dtf);

System.out.println("Here is 'endTime= 2015-03-10' :");
for (Monitoring x : col) {
    if (x.getEndTime().toLocalDate().equals(findThis)) {

        System.out.println(x.toString());

    }
}

Upvotes: 2

tobias_k
tobias_k

Reputation: 82899

According to the example data you provided

UnitId;BeginTime;EndTime;Type;Min;Max;Sum
14100072;2015-03-10 07:12:20;2015-03-10 7:13:20;Gps/GpsAccuracyGyroBias;0;0;0

endTime is "2015-03-10 7:13:20", not "2015-03-10", so using equals will not work. Instead, you could try using startsWith:

String findThis = "2015-03-10";
for (Monitoring x : col) {
    if (x.getEndTime().startsWith(findThis)) {
        System.out.println("Here is 'endTime= 2015-03-10': ");
        System.out.println(x.toString());
    }
}

Or even better: Instead of storing the begin and end times as strings, convert them to Date objects or similar when you read the objects from CSV.

Upvotes: 0

Vitalii Shevchuk
Vitalii Shevchuk

Reputation: 29

You need to provide Comparator for that null or Monitoring should implement comparable (both of them should compare items by time field that you need).

Collections.binarySearch(col, findThis.toString(), null);

Upvotes: 1

Related Questions