如何计算Arraylist中的类似行

时间:2015-10-19 11:17:41

标签: java arraylist count

我有一个类型为SearchList的Arraylist list1,这是我的pojo类。我想在list1中找到类似行的数量及其频率。 我如何实现这一目标?

我的代码:

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);       
}

我这样做是为了计算频率,但是没有执行:

 Set<SearchList> uniqueSet = new HashSet<SearchList>(list1);
     for (SearchList temp : uniqueSet) {
            System.out.println(temp + ": " + Collections.frequency(list1, temp));
     }

4 个答案:

答案 0 :(得分:0)

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

list1为空,因此for循环中的代码为:

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

不会执行(uniqueSet必须至少有一个元素才能执行循环代码)因为该集合也将为空:

 Set<SearchList> uniqueSet = new HashSet<SearchList>(list1); // set is empty

答案 1 :(得分:0)

尝试在SearchList上实现equals()方法,因为Collections.frequency()调用此方法,并确保“uniqueSet”不为空。

答案 2 :(得分:0)

您应该执行以下操作:

  • 确保您的全局定义列表中有条目(将其名称更改为不那么误导btw的内容。)
  • 确保将新的SearchList实例添加到list1。目前,您一遍又一遍地向List1添加相同的SearchList-Object。因此,如果在sList对象上调用setArea或setLocation,则所有lsit条目都将更改其值。列表条目只是指向内存中真实对象的引用。
  • 确保为SearchList类提供正确的equals()和hashcode()实现。 检查此关于equals()和hashcode()的链接:Why do I need to override the equals and hashCode methods in Java?

答案 3 :(得分:0)

如果您希望distinct基于区域位置值的行,则使用Java 8 Streams,它很简单:

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

或者,如果行的清晰度仅取决于区域的值:

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

或者,如果行的清晰度仅取决于位置的值:

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

用法:

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);
    }
}