-
Notifications
You must be signed in to change notification settings - Fork 0
/
ST07DistinctAndSorted.java
78 lines (60 loc) · 2.19 KB
/
ST07DistinctAndSorted.java
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
77
78
package com.ysingh.structure;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class ST07DistinctAndSorted {
public static void main(String args[]) {
List<Integer> numbers = Arrays.asList(12,9,13,5,4,2,4,12,15);
List<String> courses = Arrays.asList("Spring", "Spring Boot", "API", "Microservices", "AWS", "PCF", "Azure", "Docker", "Kubernetes");
System.out.println("Print Distinct Numbers in List:");
printDistinctNumbersInList(numbers);
System.out.println();
System.out.println("Print Numbers in manner in List:");
printNumbersInSortedMannerInList(numbers);
System.out.println();
System.out.println("Print Courses in sorted manner in List:");
printCoursesInSortedMannerInList(courses);
System.out.println();
System.out.println("Print Courses in reverse sorted manner in List:");
printCoursesInReverseSortedMannerInList(courses);
System.out.println();
System.out.println("Print Courses in length sorted manner in List:");
printCoursesInLengthSortedMannerInList(courses);
System.out.println();
}
private static void printDistinctNumbersInList(List<Integer> numbers) {
List<Integer> visited = new ArrayList<>();
for(int number : numbers) {
if(!visited.contains(number)) {
System.out.println(number);
visited.add(number);
}
}
}
private static void printNumbersInSortedMannerInList(List<Integer> numbers) {
numbers.sort(Comparator.comparing(Integer::intValue));
for(int number : numbers) {
System.out.println(number);
}
}
private static void printCoursesInSortedMannerInList(List<String> courses) {
courses.sort(Comparator.naturalOrder());
for(String course : courses) {
System.out.println(course);
}
}
private static void printCoursesInReverseSortedMannerInList(List<String> courses) {
courses.sort(Comparator.reverseOrder());
for(String course : courses) {
System.out.println(course);
}
}
private static void printCoursesInLengthSortedMannerInList(List<String> courses) {
Comparator<String> sortByLength = Comparator.comparing(String::length);
courses.sort(sortByLength);
for(String course : courses) {
System.out.println(course);
}
}
}