-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectCompare.java
More file actions
76 lines (64 loc) · 1.74 KB
/
Copy pathObjectCompare.java
File metadata and controls
76 lines (64 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.course.objects;
import java.util.Comparator;
import java.util.ArrayList;
import java.util.Collections;
/**
*
* @author rafiul
* There are two general way for comparing of objects in java
* First: Implement Comparable Interface in your object which
* have an abstract method name compareTo(Object o). Which will
* allow your object to compare with other same class objects
*
* Second: Use Comparator Interface which which allow you to create
* an instant compare functionality with its abstract method name
* compare(Object o1, Object o2)
*/
class Box implements Comparable<Box>{
public int h;
public int w;
public int l;
public Box(int h, int w, int l){
this.h = h;
this.w = w;
this.l = l;
}
public int compareTo(Box o){
if(o.h > this.h) return 1;
if(o.h < this.h) return -1;
return 0;
}
public String toString(){
return String.format("h: %d, w: %d, l: %d",h,w,l);
}
}
public class ObjectCompare{
public static void main(String[] args) {
ArrayList<Box> list1 = new ArrayList<Box>();
list1.add(new Box(2,4,6));
list1.add(new Box(1,2,3));
list1.add(new Box(4,8,12));
Collections.sort(list1);
System.out.println("First Example");
for(Box b : list1){
System.out.println(b);
}
/*-----------------------------------------------*/
Comparator<Box> c = new Comparator<Box>(){
public int compare(Box o1, Box o2){
if(o1.h > o2.h) return 1;
if(o1.h < o2.h) return -1;
return 0;
}
};
ArrayList<Box> list2 = new ArrayList<Box>();
list2.add(new Box(2,4,6));
list2.add(new Box(1,2,3));
list2.add(new Box(4,8,12));
list2.sort(c);
System.out.println("Second Example");
for(Box b : list2){
System.out.println(b);
}
}
}