如何计算ArrayList中具有多个字段的不同类/java文档中的重复项?
有人知道如何用我的代码得到以下答案吗?如何计算ArrayList中具有多个字段的不同类/java文档中的重复项?,java,class,arraylist,duplicates,Java,Class,Arraylist,Duplicates,有人知道如何用我的代码得到以下答案吗? aaa:3 bbb:2 ccc:1 1:2 2:2 3:1 4:1 以下是我到目前为止所做的尝试: 这是主类 package tester1; import java.util.ArrayList; public class Tester1 { public static void main(String[] args) { tester t1 = new tester(1,"aaa");
aaa:3
bbb:2
ccc:1
1:2
2:2
3:1
4:1
以下是我到目前为止所做的尝试: 这是主类
package tester1;
import java.util.ArrayList;
public class Tester1 {
public static void main(String[] args) {
tester t1 = new tester(1,"aaa");
tester t2 = new tester(2,"aaa");
tester t3 = new tester(2,"aaa");
tester t4 = new tester(1,"ccc");
tester t5 = new tester(3,"bbb");
tester t6 = new tester(4,"bbb");
ArrayList<tester> list = new ArrayList<tester>();
list.add(t1);
list.add(t2);
list.add(t3);
list.add(t4);
list.add(t5);
list.add(t6);
test t = new test(list);
t.getter();
}
}
您可以使用
Map
存储计数:
public Map<String,Integer> getCounts(ArrayList<Tester> list){
Map<String,Integer> counter=new HashMap<String,Integer>();
for(Tester s : list){
if(counter.containsKey(s.getKey())){
counter.put(s,counter.get(s.getKey())+1);
}else{
counter.put(s.getKey(),1);
}
}
return counter;
}
publicmap getCounts(ArrayList列表){
Map counter=newhashmap();
用于(测试仪s:列表){
if(counter.containsKey(s.getKey())){
counter.put(s,counter.get(s.getKey())+1);
}否则{
counter.put(s.getKey(),1);
}
}
返回计数器;
}
现在要打印,您只需在地图中迭代:
for(Map.Entry<String,Integer> entry : getCounts(list).entrySet()){
System.out.println(entry.getKey() + " : " + entry.getValue();
}
for(Map.Entry:getCounts(list.entrySet()){
System.out.println(entry.getKey()+”:“+entry.getValue();
}
您可以使用地图
存储计数:
public Map<String,Integer> getCounts(ArrayList<Tester> list){
Map<String,Integer> counter=new HashMap<String,Integer>();
for(Tester s : list){
if(counter.containsKey(s.getKey())){
counter.put(s,counter.get(s.getKey())+1);
}else{
counter.put(s.getKey(),1);
}
}
return counter;
}
publicmap getCounts(ArrayList列表){
Map counter=newhashmap();
用于(测试仪s:列表){
if(counter.containsKey(s.getKey())){
counter.put(s,counter.get(s.getKey())+1);
}否则{
counter.put(s.getKey(),1);
}
}
返回计数器;
}
现在要打印,您只需在地图中迭代:
for(Map.Entry<String,Integer> entry : getCounts(list).entrySet()){
System.out.println(entry.getKey() + " : " + entry.getValue();
}
for(Map.Entry:getCounts(list.entrySet()){
System.out.println(entry.getKey()+”:“+entry.getValue();
}
来自以下文件的javadoc:
返回指定集合中与指定对象相等的元素数。更正式地说,返回集合中元素数e,以便(o==null?e==null:o.equals(e))
那么,为什么它到处都输出0呢?这很简单,因为给定的契约永远无法解析为true
,因为没有Tester
将等于字符串
为了获得正确的输出,您必须首先覆盖Tester
中的equals
和hascode
// both are generated by eclipse source generation for the field name.
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.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;
Tester other = (Tester) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
现在,您需要更改集合#频率
调用以在检测仪
上工作,而不是在检测仪
的名称
字段上工作:
// replaced key.getName() with key
System.out.println(key.getName() + ": " + Collections.frequency(testList, key));
现在您将获得正确的输出(只是不按发生次数排序):
从以下文件的javadoc:
返回指定集合中与指定对象相等的元素数。更正式地说,返回集合中元素数e,以便(o==null?e==null:o.equals(e))
那么,为什么它到处都输出0呢?这很简单,因为给定的契约永远无法解析为true
,因为没有Tester
将等于字符串
为了获得正确的输出,您必须首先覆盖Tester
中的equals
和hascode
// both are generated by eclipse source generation for the field name.
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.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;
Tester other = (Tester) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
现在,您需要更改集合#频率
调用以在检测仪
上工作,而不是在检测仪
的名称
字段上工作:
// replaced key.getName() with key
System.out.println(key.getName() + ": " + Collections.frequency(testList, key));
现在您将获得正确的输出(只是不按发生次数排序):
list.stream()
.collect(Collectors.groupingBy(t->t.getName(),Collectors.counting())
.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue().reversed())
.forEach(e->System.out.println(e.getKey()+“:”+e.getValue());
试试Stream
怎么样。你甚至不需要test
类。
您可以通过以下两个步骤来实现
按出现次数对列表中的每个元素进行分组
按出现次数对生成的地图进行排序
list.stream()
.collect(Collectors.groupingBy(t->t.getName(),Collectors.counting())
.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue().reversed())
.forEach(e->System.out.println(e.getKey()+“:”+e.getValue());
试试Stream
怎么样。你甚至不需要test
类。
您可以通过以下两个步骤来实现
按出现次数对列表中的每个元素进行分组
按出现次数对生成的地图进行排序
下面是另一个版本,它简化了某些部分,但显示了一个带有一些静态导入的简短lambda解决方案:
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class Tester
{
public static void main(final String[] args)
{
final List<String> list = Arrays.asList("aaa", "aaa", "aaa", "ccc", "bbb", "bbb");
final Map<String, Long> map = list.stream()
.collect(groupingBy(identity(), counting()));
System.out.println("map = " + map);
}
}
导入静态java.util.function.function.identity;
导入静态java.util.stream.Collectors.counting;
导入静态java.util.stream.Collectors.groupingBy;
导入java.util.array;
导入java.util.List;
导入java.util.Map;
公共类测试员
{
公共静态void main(最终字符串[]args)
{
最终列表=数组.asList(“aaa”、“aaa”、“aaa”、“ccc”、“bbb”、“bbb”);
最终映射=list.stream()
.收集(分组方式(标识(),计数());
System.out.println(“map=“+map”);
}
}
这里是另一个版本,它简化了某些部分,但显示了一个带有一些静态导入的简短lambda解决方案:
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class Tester
{
public static void main(final String[] args)
{
final List<String> list = Arrays.asList("aaa", "aaa", "aaa", "ccc", "bbb", "bbb");
final Map<String, Long> map = list.stream()
.collect(groupingBy(identity(), counting()));
System.out.println("map = " + map);
}
}
导入静态java.util.function.function.identity;
导入静态java.util.stream.Collectors.counting;
导入静态java.util.stream.Collectors.groupingBy;
导入java.util.array;
导入java.util.List;
导入java.util.Map;
公共类测试员
{
公共静态void main(最终字符串[]args)
{
最终列表=数组.asList(“aaa”、“aaa”、“aaa”、“ccc”、“bbb”、“bbb”);
最终映射=list.stream()
.收集(分组方式(标识(),计数());
System.out.println(“map=“+map”);
}
}
你可以选择Map
而不是Set
你现在拥有的东西有什么问题?它输出了什么?可能的重复你可以选择Map
而不是Set
你现在拥有的东西有什么问题?它输出了什么?可能的重复非常感谢你的帮助。如果有多个我想在测试仪中检查的字段我需要做什么?如果是int,我需要做什么