Java 如何计算Arraylist中的相似行
我有一个类型为SearchList的Arraylist list1,它是我的pojo类。我想在列表1中找到类似行的数量及其出现频率。。 我如何做到这一点 我的代码:Java 如何计算Arraylist中的相似行,java,arraylist,count,Java,Arraylist,Count,我有一个类型为SearchList的Arraylist list1,它是我的pojo类。我想在列表1中找到类似行的数量及其出现频率。。 我如何做到这一点 我的代码: List<SearchList> list1= new ArrayList<SearchList>();; SearchList sList = new SearchList(); for (int k = 0; k < list.size(); k++) { sList.setAre
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);
}
List list1=new ArrayList();;
SearchList sList=新的SearchList();
对于(int k=0;k
我已经对计数频率进行了此操作,但没有执行:
Set<SearchList> uniqueSet = new HashSet<SearchList>(list1);
for (SearchList temp : uniqueSet) {
System.out.println(temp + ": " + Collections.frequency(list1, temp));
}
Set uniqueSet=newhashset(列表1);
用于(搜索列表温度:唯一集){
System.out.println(temp+“:”+Collections.frequency(list1,temp));
}
不会执行(uniqueSet
必须至少有一个元素才能执行循环代码),因为集合也将为空:
Set<SearchList> uniqueSet = new HashSet<SearchList>(list1); // set is empty
Set uniqueSet=newhashset(list1);//集合是空的
尝试在SearchList上实现equals()方法,因为Collections.frequency()调用此方法,并确保“uniqueSet”不是空的。您应该执行以下操作:
- 确保您的全局定义列表中有条目(顺便说一句,将其名称更改为误导性较小的名称。)
- 确保将SearchList的新实例添加到列表1中。此时,您正在将同一个SearchList对象反复添加到列表1中。因此,如果在sList对象上调用setArea或setLocation,则所有lsit条目都将更改其值。列表项只是指向内存中真实对象的引用
- 确保为SearchList类提供适当的equals()和hashcode()实现。 检查equals()和hashcode()上的此链接:
区分行,那么使用Java 8
,操作非常简单:
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();
int countSimilarByArea=searchListList
.stream()
.收集(
收集器。收集(()->新树集((
p1,p2)->p1.getArea().compareTo(p2.getArea()))
.size();
或者,如果行的清晰度仅取决于位置的值:
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();
int countSimilarByLocation=searchListList
.stream()
.收集(
收集器。收集(()->新树集((
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);
}
}
import java.util.ArrayList;
导入java.util.List;
导入java.util.TreeSet;
导入java.util.stream.collector;
最终课程搜索列表{
私人最终串区;
私有最终字符串位置;
公共搜索列表(字符串区域、字符串位置){
这个面积=面积;
这个位置=位置;
}
公共字符串getArea(){
返回区;
}
@凌驾
公共字符串toString(){
返回“SearchList[area=“+area+”,location=“+location+”]”;
}
公共字符串getLocation(){
返回位置;
}
@凌驾
公共int hashCode(){
最终整数素数=31;
int结果=1;
result=prime*result+((area==null)?0:area.hashCode();
结果=素数*结果
+((location==null)?0:location.hashCode());
返回结果;
}
@凌驾
公共布尔等于(对象obj){
if(this==obj)
返回true;
if(obj==null)
返回false;
如果(getClass()!=obj.getClass())
返回false;
SearchList other=(SearchList)obj;
如果(面积==null){
if(other.area!=null)
返回false;
}如果(!面积等于(其他面积))
返回false;
if(位置==null){
if(其他.location!=空)
返回false;
}如果(!location.equals(other.location))
返回false;
返回true;
}
}
公营部门{
公共静态void main(字符串[]args){
List searchListList=new ArrayList();
添加(新的搜索列表(“A”、“印度”);
添加(新的搜索列表(“B”、“印度”);
添加(新的搜索列表(“A”、“印度”);
添加(新的搜索列表(“A”、“美国”);
添加(新的搜索列表(“A”、“美国”);
int countSimilar=(int)searchListList.stream().distinct().count();
System.out.println(类似计数);
int countSimilarByArea=searchListList
.stream()
.收集(
收集器。收集(()->新树集((
p1,p2)->p1.getArea().compareTo(p2.getArea()))
.size();
System.out.println(countSimilarByArea);
int countSimilarByLocation=searchListList
.stream()
.收集(
收集器。收集(()->新树集((
p1,p2)->p1.getLocation().compareTo(
p2.getLocation()).size();
System.out.println(countSimilarByLocation);
}
}
我看到的主要问题是,您多次将SearchList
的相同实例添加到list1
中(每次都应该添加一个新实例)。您所说的不执行
是什么意思?您的搜索列表覆盖了hashCode和equals?您会得到什么错误?“多次将相同的搜索列表实例添加到列表1”意味着什么?for loop不在外列表是如何定义的?但是list1不是空的列表的大小是什么?列表的大小是动态的。但是它不是空的。如果它不是空的,它将