Java - order things in arraylist according to attribute

Hi, I have an arraylist, let's call it "callcenterList" in this arraylist, I will add objects of the "Call" class.

Call objects have different attributes, some of them are Status, Month, and Day (month and day when a call is received and its status).

Status can be Completed or Pending

Thus, the Call object will look like this:
call1:
Atribute1 = whatever.
Atribute2 = whatever.
Month = 3
Day = 25
Status = Pending

So here is what they needed to do:

I need to show all calls with status = Waiting and sort by date. How can i do this? If you need more information or more details, let me know. Thank!
+4
source share
3

Comparator<Call>, compare(Call c1, Call c2) , , 1, 0 -1 . Collections.sort(myList, myComparator) .

public class Call {
   private int value1;
   private int value2;
   public Call(int value1, int value2) {
      this.value1 = value1;
      this.value2 = value2;
   }
   public int getValue1() {
      return value1;
   }
   public void setValue1(int value1) {
      this.value1 = value1;
   }
   public int getValue2() {
      return value2;
   }
   public void setValue2(int value2) {
      this.value2 = value2;
   }
}

public class MyComparator implements Comparator<Call> {

   @Override
   public int compare(Call c1, Call c2) {
      int firstCompare = Integer.compare(c1.getValue1(), c2.getValue1());
      if (firstCompare != 0) {
         return firstCompare;
      } else {
         return Integer.compare(c1.getValue2(), c2.getValue2());
      }
   }

}
+3

( CallComparator()) ArrayList comparator. , . # 7 # 14, CallComparator(). Compare (call14, call7) 0.

, , , :

public class CallComparator implements Comparator<Call> {
   @Override
   public int compare(Call c1, Call c2) {
        if (c1.getMonth() == c2.getMonth() 
             return c1.getDay() - c2.getDay();
        else 
             return c1.getMonth() - c2.getMonth();
        }
 }

, , , getDay() getMonth() Call.

, , , . , compare() - . . Date Java, Java 8 , . LocalDateTime ( , ), . compare()

return c1.getDate().compareTo(c2.getDate());

.

+1

, , . , , Iterator, , , , , .

, , . , .

Google Guava, .

Here is an example. Assume that it Callhas methods getStatus()and getDate(), and getDate()- Comparable(class java.util.Date).

List<Call> calls = ...
final List<Call> filteredCalls = FluentIterable.from(calls).filter(new Predicate<Call>() {
    @Override
    public boolean apply(final Call input) {
        return "pending".equals(input.getStatus());
    }
 }).toSortedList(new Comparator<Call>() {
    @Override
    public int compare(final Call firstCall, final Call secondCall) {
        return firstCall.getDate().compareTo(secondCall.getDate());
    }
 });

In Java 8, this expression becomes much more concise, and you do not need to use a third-party library.

final List<Call> filteredCalls = calls.stream()
                                .filter(c -> "pending".equals(c.getStatus()))
                                .sorted((left, right) -> left.getDate().compareTo(right.getDate()))
                                .collect(Collectors.toList());
0
source

All Articles