Skip to content

Commit 8b807b8

Browse files
author
ryo
committed
update the comparator
1 parent aa6e00c commit 8b807b8

File tree

8 files changed

+92
-16
lines changed

8 files changed

+92
-16
lines changed

src/main/java/io/ascending/training/collection/AgeComparator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
public class AgeComparator implements Comparator<Employee> {
66
@Override
77
public int compare(Employee e1, Employee e2) {
8-
return (int) (e1.getSalary() - e2.getSalary());
8+
return (int) (e1.getAge() - e2.getAge());
99
}
1010
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.ascending.training.collection;
2+
3+
import java.util.Comparator;
4+
5+
public class FirstNameComparator implements Comparator<Employee> {
6+
@Override
7+
public int compare(Employee e1, Employee e2) {
8+
// char[] e1FirstNames = e1.getName().toCharArray();
9+
// char[] e2FirstNames = e2.getName().toCharArray();
10+
// //"zhang" , "zhang"
11+
// int size=e1FirstNames.length;
12+
// for(int i=0;i<size-1;i++){
13+
// if(e1FirstNames[i]-e2FirstNames[i]==0){
14+
// continue;
15+
// }else{
16+
// return e1FirstNames[i]-e2FirstNames[i];
17+
// }
18+
// }
19+
// return 0;
20+
//"st1 - str2" == str1.compareTo(str2);
21+
// return e1.getName().compareTo(e2.getName());
22+
if(e1.getName().compareTo(e2.getName())==0){
23+
return Long.valueOf(e1.getSalary() - e2.getSalary()).intValue();
24+
}else{
25+
return e1.getName().compareTo(e2.getName());
26+
}
27+
}
28+
}

src/main/java/io/ascending/training/collection/SalaryComparator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
public class SalaryComparator implements Comparator<Employee> {
66
@Override
77
public int compare(Employee e1, Employee e2) {
8-
return (int) (e1.getSalary() - e2.getSalary());
8+
return Long.valueOf(e1.getSalary() - e2.getSalary()).intValue();
99
}
1010
}

src/main/java/io/ascending/training/collection/Solution.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.ascending.training.collection;
22

33
import java.util.Arrays;
4+
import java.util.Collections;
45
import java.util.Comparator;
56

67
public class Solution {
@@ -47,15 +48,17 @@ public static void main(String[] args){
4748
//sorting employees array using Comparable interface implementation
4849
Arrays.sort(empArr);
4950
System.out.println("Default Sorting of Employees list:\n"+Arrays.toString(empArr));
50-
// Comparator<Employee> salaryComparator = (e1, e2)->(int)(e1.getSalary()-e2.getSalary());
51-
Arrays.sort(empArr, new SalaryComparator());
52-
System.out.println("Employees list sorted by Salary:\n"+Arrays.toString(empArr));
51+
Arrays.sort(empArr, new AgeComparator());
5352

54-
//sort employees array using Comparator by Age
55-
Arrays.sort(empArr, (o1, o2)->o1.getAge()-o2.getAge());
53+
Comparator<Employee> salaryComparator = (e1, e2)->(int)(e1.getSalary()-e2.getSalary());
54+
Arrays.sort(empArr, salaryComparator);
55+
System.out.println("Employees list sorted by Salary:\n"+Arrays.toString(empArr));
56+
//
57+
// //sort employees array using Comparator by Age
58+
Arrays.sort(empArr,(o1, o2)->o1.getAge()-o2.getAge());
5659
System.out.println("Employees list sorted by Age:\n"+Arrays.toString(empArr));
57-
58-
//sort employees array using Comparator by Name
60+
//
61+
// //sort employees array using Comparator by Name
5962
Arrays.sort(empArr, NameComparator);
6063
System.out.println("Employees list sorted by Name:\n"+Arrays.toString(empArr));
6164
}

src/main/java/io/ascending/training/lambda/LambdaTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ public static void main(String[] args){
2727
MyString classString = new LambdaTest.MyStringImpl();
2828
System.out.println(classString.myStringFunction("Ryo"));
2929

30+
MyString classString2 = new MyString() {
31+
@Override
32+
public String myStringFunction(String str) {
33+
return "Good Morning " + str + "!";
34+
}
35+
};
36+
System.out.println(classString2.myStringFunction("Ryo"));
37+
38+
3039

3140
MyString value = (str) -> "Good Morning " + str + "!";
3241
MyString value2 = (str) -> "Good Afternoon " + str + "!";;

src/main/java/io/ascending/training/operator/Assignment.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package io.ascending.training.operator;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
36
public class Assignment {
47
class Test
58
{
@@ -36,10 +39,11 @@ public static void main(String[] args)
3639
f = f * 2;
3740

3841
// shorthand assignment operator
39-
a += 1;
40-
b -= 1;
41-
e *= 2;
42-
f /= 2;
42+
a -= 1;
43+
b += 1;
44+
e /= 2;
45+
f *= 2;
46+
4347
System.out.println("a, b, e, f ("
4448
+ "using shorthand operators)= "
4549
+ a + ", " + b + ", "
@@ -48,7 +52,7 @@ public static void main(String[] args)
4852
System.out.println("a, b ("
4953
+ "pass by value)= "
5054
+ a + ", " + b);
51-
55+
//
5256
Integer i = new Integer(10);
5357
Integer j = new Integer(20);
5458
swap(i, j);
@@ -58,11 +62,21 @@ public static void main(String[] args)
5862
int jx = 20;
5963
swapValue(ix,jx);
6064
System.out.println(ix);
65+
System.out.println(jx);
66+
67+
List<Integer> integerList = new ArrayList();
68+
addValue(integerList);
69+
System.out.println(integerList);
6170
// Test t = new Test(5);
6271
// change(t);
6372
// System.out.println(t.x);
6473
}
6574

75+
public static void addValue(List<Integer> ls){
76+
ls.add(new Integer(5));
77+
}
78+
79+
//a=copy(ix), b=copy(jx)
6680
public static void swapValue(int a, int b){
6781
int temp=b;
6882
b=a;

src/main/java/io/ascending/training/operator/Shift.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ public static void main(String[] args)
99

1010
// left shift operator
1111
// 0000 0101<<2 =0001 0100(20)
12-
// similar to 5*(2^2)
12+
// similar to 5*(2^left shift n time)
1313
System.out.println("a<<2 = " + (a << 2));
1414

1515
// right shift operator
1616
// 0000 0101 >> 2 =0000 0001(1)
17-
// similar to 5/(2^2)
17+
// similar to 5/(2^ right shift n time)
1818
System.out.println("a>>2 = " + (a >> 2));
1919

2020
// unsigned right shift operator

src/main/java/io/ascending/training/time/DateExample.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import java.time.LocalDate;
44
import java.time.LocalDateTime;
5+
import java.time.ZoneId;
6+
import java.time.ZonedDateTime;
7+
import java.time.format.DateTimeFormatter;
58
import java.util.Date;
69

710
public class DateExample {
@@ -17,7 +20,26 @@ public static void main(String[] args){
1720
LocalDate ld = LocalDate.now();
1821
System.out.println(ld);
1922

23+
// LocalDateTime.of(10,5,5,30,0,0);
2024
LocalDateTime ldtparse = LocalDateTime.parse("2019-06-22T23:29:58.309");
2125
System.out.println(ldtparse);
26+
27+
System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("MM/dd/yyyy")));
28+
29+
// Date d = new Date();
30+
// new Date(d.getTime()+1000*60*60*24);
31+
// d.getTime();
32+
33+
// System.out.println(LocalDate.now().plusDays(1).plusMonths(1));
34+
// System.out.println(LocalDateTime.now());
35+
36+
// System.out.println(ZonedDateTime.now());
37+
// ZonedDateTime dc = ZonedDateTime.now();
38+
// System.out.println(dc);
39+
// ZoneId swissZone = ZoneId.of("Europe/Zurich");
40+
// ZonedDateTime swissZoned = dc.withZoneSameInstant(swissZone);
41+
// LocalDateTime swissLocal = swissZoned.toLocalDateTime();
42+
// System.out.println(swissZoned);
43+
// System.out.println(swissLocal);
2244
}
2345
}

0 commit comments

Comments
 (0)