Skip to content

Commit 1b2797d

Browse files
Arpit AggarwalArpit Aggarwal
authored andcommitted
Comapre List - SO Solution
1 parent 1be4477 commit 1b2797d

File tree

7 files changed

+168
-4
lines changed

7 files changed

+168
-4
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.test.so.solutions;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.stream.IntStream;
6+
7+
public class CountInt {
8+
9+
public static void main(String[] args) {
10+
List<String> list = Arrays.asList("a", "b", "c", "d");
11+
int count = (int) IntStream.range(0, list.size()).count();
12+
System.out.println(count);
13+
}
14+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.test.so.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.TreeSet;
6+
import java.util.stream.Collectors;
7+
8+
final class SearchList {
9+
private final String area;
10+
private final String location;
11+
12+
public SearchList(String area, String location) {
13+
this.area = area;
14+
this.location = location;
15+
}
16+
17+
public String getArea() {
18+
return area;
19+
}
20+
21+
@Override
22+
public String toString() {
23+
return "SearchList [area=" + area + ", location=" + location + "]";
24+
}
25+
26+
public String getLocation() {
27+
return location;
28+
}
29+
30+
@Override
31+
public int hashCode() {
32+
final int prime = 31;
33+
int result = 1;
34+
result = prime * result + ((area == null) ? 0 : area.hashCode());
35+
result = prime * result
36+
+ ((location == null) ? 0 : location.hashCode());
37+
return result;
38+
}
39+
40+
@Override
41+
public boolean equals(Object obj) {
42+
if (this == obj)
43+
return true;
44+
if (obj == null)
45+
return false;
46+
if (getClass() != obj.getClass())
47+
return false;
48+
SearchList other = (SearchList) obj;
49+
if (area == null) {
50+
if (other.area != null)
51+
return false;
52+
} else if (!area.equals(other.area))
53+
return false;
54+
if (location == null) {
55+
if (other.location != null)
56+
return false;
57+
} else if (!location.equals(other.location))
58+
return false;
59+
return true;
60+
}
61+
62+
}
63+
64+
public class CountSimilar {
65+
public static void main(String[] args) {
66+
67+
List<SearchList> searchListList = new ArrayList<>();
68+
searchListList.add(new SearchList("A", "India"));
69+
searchListList.add(new SearchList("B", "India"));
70+
searchListList.add(new SearchList("A", "India"));
71+
searchListList.add(new SearchList("A", "USA"));
72+
searchListList.add(new SearchList("A", "USA"));
73+
74+
int countSimilar = (int) searchListList.stream().distinct().count();
75+
76+
System.out.println(countSimilar);
77+
78+
int countSimilarByArea = searchListList
79+
.stream()
80+
.collect(
81+
Collectors.toCollection(() -> new TreeSet<SearchList>((
82+
p1, p2) -> p1.getArea().compareTo(p2.getArea()))))
83+
.size();
84+
System.out.println(countSimilarByArea);
85+
86+
int countSimilarByLocation = searchListList
87+
.stream()
88+
.collect(
89+
Collectors.toCollection(() -> new TreeSet<SearchList>((
90+
p1, p2) -> p1.getLocation().compareTo(
91+
p2.getLocation())))).size();
92+
93+
System.out.println(countSimilarByLocation);
94+
}
95+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.test.so.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.stream.IntStream;
6+
7+
public class ListCompare {
8+
public static void main(String[] args) {
9+
10+
List<String> list1 = new ArrayList<String>();
11+
list1.add("A");
12+
list1.add("B");
13+
list1.add("C");
14+
15+
List<String> list2 = new ArrayList<String>();
16+
list2.add("A");
17+
list2.add("B");
18+
list2.add("D");
19+
20+
IntStream.range(0, list1.size()).forEach(i -> {
21+
if (list1.get(i).equals(list2.get(i)))
22+
System.out.println(i);
23+
});
24+
}
25+
26+
}

src/com/test/so/solutions/ListSort.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public static void main(String[] args) {
2020
list.add(new ListSort(3, "adf"));
2121
list.add(new ListSort(6, "aad"));
2222
list.add(new ListSort(2, "xx"));
23-
list.sort((s1, s2) -> s1.a - s2.a);
23+
24+
list.sort((s1, s2) -> s1.a - s2.a);
2425

2526
for (ListSort element : list) {
2627
System.out.println(element.a + element.b);

src/com/test/so/solutions/ListToString.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ public static void main(String[] args) {
1212
list.add("three");
1313

1414
System.out.println(String.join(",", list));
15-
1615
System.out.println(list.stream().collect(Collectors.joining(",")));
17-
1816
System.out.println(getStringPriorJava8(list));
1917
}
2018

src/com/test/so/solutions/OddNumbers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class OddNumbers {
99
public static void main(String[] args) {
1010
System.out.print("Enter Number : ");
1111
int n = scanner.nextInt();
12-
IntStream.range(0, n).filter(element -> element % 2 != 0).boxed()
12+
IntStream.range(0, n).filter(element -> element % 2 != 0)
1313
.forEach(System.out::println);
1414
}
1515
}

src/com/test/streams/Reduce.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.test.streams;
2+
3+
import java.util.stream.Stream;
4+
5+
class Employee {
6+
private final int age;
7+
8+
public Employee(final int age) {
9+
this.age = age;
10+
}
11+
12+
public int getAge() {
13+
return age;
14+
}
15+
}
16+
17+
public class Reduce {
18+
19+
public static void main(String[] args) {
20+
Employee employee = new Employee(10);
21+
Employee employee1 = new Employee(15);
22+
Employee employee2 = new Employee(20);
23+
int sum = Stream.of(employee, employee1, employee2)
24+
.map(Employee::getAge).
25+
reduce(0, (a, b) -> -a + b);
26+
System.out.println(sum);
27+
28+
}
29+
30+
}

0 commit comments

Comments
 (0)