|
| 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 | +} |
0 commit comments