2

I have an Arraylist list1 of type SearchList which is my pojo class. I want to find number of similar rows and their frequency occuring in list1.. How do I achieve this?

My code:

List<SearchList> list1= new ArrayList<SearchList>();;
SearchList sList = new SearchList();
 for (int k = 0; k < list.size(); k++) {
        sList.setArea(list.get(k).getArea());
        sList.setLocation(list.get(k).getLocation());
        list1.add(sList);       
}

I have done this for counting frequency but doesn't execute:

 Set<SearchList> uniqueSet = new HashSet<SearchList>(list1);
     for (SearchList temp : uniqueSet) {
            System.out.println(temp + ": " + Collections.frequency(list1, temp));
     }
Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
Sheena Tyagi
  • 341
  • 1
  • 4
  • 17

4 Answers4

0
List<SearchList> list1= new ArrayList<SearchList>(); //list1 is empty

list1 is empty so the code with in the for loop:

for (SearchList temp : uniqueSet) {
            System.out.println(temp + ": " + Collections.frequency(list1, 
temp));
        }

would not execute (uniqueSet must have at least one element in order the loop code to be executed) as the set will also be empty:

 Set<SearchList> uniqueSet = new HashSet<SearchList>(list1); // set is empty
dsharew
  • 10,377
  • 6
  • 49
  • 75
0

Try implementing the equals() method on SearchList, since Collections.frequency() calls this method, and be sure that "uniqueSet" is not empty.

Lerkendal
  • 11
  • 2
0

you should do the following:

  • make sure there are entries in your globally defined list (change its name to something less misleading btw.)
  • make sure you add new instances of SearchList to your list1. At the moment you are adding the same SearchList-Object over and over to your list1. Therefore if you call setArea or setLocation on your sList Object all your lsit entries will change their values. The list entry is just a reference pointing at the real object in memory.
  • make sure you supply proper equals() and hashcode() implementations for your SearchList class. check this Link on equals() and hashcode(): Why do I need to override the equals and hashCode methods in Java?
Community
  • 1
  • 1
markus
  • 1,631
  • 2
  • 17
  • 31
0

If you want to distinct the row based on value of both area and location value then with Java 8 Streams, it is as simple as:

int countSimilar = (int) searchListList.stream().distinct().count();

Or, if distinctness of row depends on value of area only:

int countSimilarByArea = searchListList
                .stream()
                .collect(
                        Collectors.toCollection(() -> new TreeSet<SearchList>((
                                p1, p2) -> p1.getArea().compareTo(p2.getArea()))))
                .size();

Or, if distinctness of row depends on value of location only:

int countSimilarByLocation = searchListList
                .stream()
                .collect(
                        Collectors.toCollection(() -> new TreeSet<SearchList>((
                                p1, p2) -> p1.getLocation().compareTo(
                                p2.getLocation())))).size();

Usage:

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

final class SearchList {
    private final String area;
    private final String location;

    public SearchList(String area, String location) {
        this.area = area;
        this.location = location;
    }

    public String getArea() {
        return area;
    }

    @Override
    public String toString() {
        return "SearchList [area=" + area + ", location=" + location + "]";
    }

    public String getLocation() {
        return location;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((area == null) ? 0 : area.hashCode());
        result = prime * result
                + ((location == null) ? 0 : location.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        SearchList other = (SearchList) obj;
        if (area == null) {
            if (other.area != null)
                return false;
        } else if (!area.equals(other.area))
            return false;
        if (location == null) {
            if (other.location != null)
                return false;
        } else if (!location.equals(other.location))
            return false;
        return true;
    }

}

public class CountSimilar {
    public static void main(String[] args) {
        List<SearchList> searchListList = new ArrayList<>();
        searchListList.add(new SearchList("A", "India"));
        searchListList.add(new SearchList("B", "India"));
        searchListList.add(new SearchList("A", "India"));
        searchListList.add(new SearchList("A", "USA"));
        searchListList.add(new SearchList("A", "USA"));

        int countSimilar = (int) searchListList.stream().distinct().count();

        System.out.println(countSimilar);

        int countSimilarByArea = searchListList
                .stream()
                .collect(
                        Collectors.toCollection(() -> new TreeSet<SearchList>((
                                p1, p2) -> p1.getArea().compareTo(p2.getArea()))))
                .size();
        System.out.println(countSimilarByArea);

        int countSimilarByLocation = searchListList
                .stream()
                .collect(
                        Collectors.toCollection(() -> new TreeSet<SearchList>((
                                p1, p2) -> p1.getLocation().compareTo(
                                p2.getLocation())))).size();

        System.out.println(countSimilarByLocation);
    }
}
Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
  • Getting error: "The method main cannot be declared static; static methods can only be declared in a static or top level type" – Sheena Tyagi Oct 19 '15 at 12:36
  • Have a look at http://stackoverflow.com/questions/22787063/the-method-main-cannot-be-declared-static-static-methods-can-only-be-declared – Arpit Aggarwal Oct 19 '15 at 12:47