如何通过对象的ArrayList迭代和对象属性查找重复? [英] how to Iterate through an arrayList of object and find duplicates in object properties?

查看:90
本文介绍了如何通过对象的ArrayList迭代和对象属性查找重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类:

class A {
    ArrayList<String> s = new ArrayList<>();
    double d = Double.MAX_VALUE;
}

我有A类的如的ArrayList℃的ArrayList中,A&GT; ALIST =新的ArrayList&LT;&GT;();
ALIST 我有一些元素,比如以下几个方面:

I've an arrayList of class A like ArrayList<A> alist = new ArrayList<>(); In alistI have some elements like followings:

[[b  c]  1.3]
[[c  d]  0.2]
[[c  b  o]  0.9]
[[x  o  j]  1.8]
[[c  d]  1.7]
[[b  c  o]  2.2]
[[f  p  n]  1.1]
and so on...

所有我需要的是通过这个迭代 ALIST 并检查是否有在字符串列表中的任何重复 A级目的。如果是这样那么preserve只有一个元素,并添加其他D取值与它的d值,并移除 ALIST 其它元件有相同的字符串列表。例如:
从上面的列表中,我们可以看到第二和第五元素有重复的字符串列表,以便与附加值增加他们的价值观和preserve只是其中之一,并删除其他。同,要素第3和第6具有相同的字符串列表(字符串命令并非以内部串名单很重要)。因此,对于上面的列表中,预计产量将是:

All I need is to iterate through this alistand check if there any duplicates in String list of class A object. If so then preserve only one element and add others d values with its d value and remove other elements from alistwhich have the same String list. For example:
from the above list we can see 2nd and 5th elements have duplicate string list so add their values and preserve only one of them with that added value and remove others. Same as, element 3rd and 6th has same String list(string order is not important in inner string list). So for the above list, the expected output will be:

[[b  c]  1.3]
[[c  d]  1.9]
[[b  c  o]  3.1]
[[x  o  j]  1.8]
[[f  p  n]  1.1]

任何人可以帮助我解决这个提供一些样品code吗?我不是在Java中那么好。所以,请原谅我,如果我犯了任何错误!

Can anybody help me with this by providing some sample code please? I'm not that good in Java. So please pardon me if I made any mistake!

谢谢!

推荐答案

一个良好的开端将被定义什么()方法在A类的.equals那么它只是作为循环访问您的收藏容易,和问a.eqauls(b)。

A good place to start would be defining what the .equals() method does in class A. Then it is just as easy as iterating over your collection, and asking if a.eqauls(b).

这篇关于如何通过对象的ArrayList迭代和对象属性查找重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆