我对如何比较两个列表有疑问吗?
这是我的情况。
我有两个ArrayList
(nameList<String>
和countList<Integer>
)。两个列表都可以包含重复值,并且两个列表的大小也相同。
现在,我必须将这两个列表合并为键值对。为此,我创建了一个类型为Person的自定义列表:
public class Person {
private String name;
private Integer charCount;
public Person(String name, Integer charCount) {
this.name = name;
this.charCount = charCount;
}
public String Name() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getCharCount() {
return charCount;
}
public void setCharCount(Integer charCount) {
this.charCount = charCount;
}
}
List<Person> plist1 = new ArrayList<Person>();
for (int i=0; i < nameList.size(); i++) {
plist1.add(new Person(nameList.get(i), countList.get(i)));
}
这给了我正确的价值观:
詹姆斯,5
查尔斯(Charles),6
劳埃德(Lloyd),5
查尔斯,6
我已经创建了另一个数据的列表,其要求与上述完全相同,例如plist2
。现在,我必须比较这两个列表。
也就是说,我必须检查plist1中包含的名称是否也包含在plist2
中。
答案 0 :(得分:0)
您可以使用自定义比较器对两个列表进行排序(首先按名称字段,然后按charCount字段),然后循环浏览列表,同时检查两个列表中的对应对象是否相同。
这将为您提供两个列表在它们包含的对象集合中是否相等(对象的顺序可能不相同)。例如:
Sub Send_Files() 'Working in Excel 2000-2016 'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
Dim OutApp As Object
Dim OutMail As Object
Dim sh As Worksheet
Dim cell As Range
Dim FileCell As Range
Dim rng As Range
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set sh = Sheets("Sheet1")
Set OutApp = CreateObject("Outlook.Application")
For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)
'Enter the path/file names in the C:Z column in each row
Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")
If cell.Value Like "?*@?*.?*" And _
Application.WorksheetFunction.CountA(rng) > 0 Then
Set OutMail = OutApp.CreateItem(0)
With OutMail
.to = cell.Value
.Subject = "Testfile"
.Body = "Hi " & cell.Offset(0, -1).Value
For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
If Trim(FileCell) <> "" Then
If Dir(FileCell.Value) <> "" Then
.Attachments.Add FileCell.Value
End If
End If
Next FileCell
.Send 'Or use .Display
End With
Set OutMail = Nothing
End If
Next cell
Set OutApp = Nothing
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
根据上述算法是相等的,但是在以下情况下,它们将不被视为相等,
plist1 --> James,5
Charles,6
Lloyd,5
Charles,6
plist2 --> Lloyd,5
Charles,6
James,5
Charles,6
答案 1 :(得分:0)
好,这是您想要做的完整演示,它正在比较各种列表并显示其结果。在Person类中,您需要像我一样实现equals方法,然后仅在for循环中迭代两个列表,并在与list相同的索引处比较两个对象。如果两个列表的大小不同,则可以简单地返回false。检查此代码。
public class Person {
private String name;
private Integer charCount;
public Person(String name, Integer charCount) {
this.name = name;
this.charCount = charCount;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getCharCount() {
return charCount;
}
public void setCharCount(Integer charCount) {
this.charCount = charCount;
}
public boolean equals(Object o) {
if (o == null) {
return false;
}
if (o instanceof Person) {
Person person = (Person) o;
return person.getName().equals(this.getName())
&& person.getCharCount().intValue() == this.getCharCount().intValue();
}
return false;
}
public static void main(String[] args) {
List<Person> list1 = Arrays.asList(new Person("a1", 1), new Person("a2", 2));
List<Person> list2 = Arrays.asList(new Person("a1", 1), new Person("a2", 2));
List<Person> list3 = Arrays.asList(new Person("a1", 1), new Person("a3", 3));
// list4 content same as list1 but order different
List<Person> list4 = Arrays.asList(new Person("a2", 2), new Person("a1", 1));
System.out.println(compareList(list1, list2));
// prints true as both list have same person objects and same order
System.out.println(compareList(list1, list3));
// prints false as both list have differing content
System.out.println(compareList(list2, list3));
// prints false as both list have differing content
System.out.println(compareList(list1, list4));
// prints false as both list have same content but differing order
}
public static boolean compareList(List<Person> list1, List<Person> list2) {
if (list1.size() != list2.size()) {
return false;
}
for (int i = 0; i < list1.size(); i++) {
if (!list1.get(i).equals(list2.get(i))) {
return false;
}
}
return true;
}
}