代码的第一部分创建一个随机的arrayList。 方法1是传统的"嵌套循环的方法。 方法2删除未在arrayList2Temp中找到的元素,以便在下一次迭代中保存一个比较。从理论上讲,后一种方法应该更快,但事实并非如此。我在代码中构建了一个秒表,因此它输出执行每个方法所需的时间(以毫秒为单位)。最后一部分是一致性检查,以确保每个方法确实创建相同的结果。我应该使用嵌套的数组列表吗?并行执行此任务的正确方法是什么,即不必迭代每个元素?在任何情况下,我都会对能够减少几毫秒的任何建议感到高兴。
Delete4
package delete4;
import java.util.ArrayList;
import java.util.Random;
/**
*
* @author
*/
public class Delete4 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//create random arrayList
Random rand = new Random();
int Recipient = 0;
int list0ItemCount = 5000;
int Score = 0;
ArrayList<Class1> arrayList0 = new ArrayList<>();
for (int i = 0; i < list0ItemCount; i++) {
Recipient = rand.nextInt((100000 - 1) + 1) + 1;
while (Recipient == i) {
Recipient = rand.nextInt((100000 - 1) + 1) + 1;
}
Score = rand.nextInt((10 + 10) + 1) - 10;
arrayList0.add(new Class1(i, Recipient, Score, Score));
}
double startTime;
double endTime;
double duration = 0;
//method 1
startTime = System.nanoTime();
ArrayList<Class1> arrayList1 = new ArrayList<>(arrayList0);
ArrayList<Class1> arrayList1Temp = new ArrayList<>(arrayList0);
int list1ItemCount = arrayList1.size();
int list1TempItemCount = arrayList1.size();
for (int i = 0; i < list1ItemCount; i++) {
for (int j = 0; j < list1TempItemCount; j++) {
if (arrayList1.get(i).Recipient == arrayList1Temp.get(j).Recipient && arrayList1.get(i).ID != arrayList1Temp.get(j).ID) {
arrayList1.set(i, new Class1(arrayList1.get(i).ID, arrayList1.get(i).Recipient, arrayList1.get(i).Score, arrayList1.get(i).CumulativeScore + arrayList1Temp.get(j).Score));
}
}
}
endTime = System.nanoTime();
duration = (endTime - startTime) / 1000000;
System.out.println(duration);
//method 2
startTime = System.nanoTime();
ArrayList<Class1> arrayList2 = new ArrayList<>(arrayList0);
ArrayList<Class1> arrayList2Temp = new ArrayList<>(arrayList0);
int list2ItemCount = arrayList2.size();
int list2TempItemCount = arrayList2Temp.size();
for (int i = 0; i < list2ItemCount; i++) {
boolean found = false;
int k = 0;
for (int j = 0; j < list2TempItemCount; j++) {
if (arrayList2.get(i).Recipient == arrayList2Temp.get(j).Recipient && arrayList2.get(i).ID != arrayList2Temp.get(j).ID) {
arrayList2.set(i, new Class1(arrayList2.get(i).ID, arrayList2.get(i).Recipient, arrayList2.get(i).Score, arrayList2.get(i).CumulativeScore + arrayList2Temp.get(j).Score));
found = true;
}
if (arrayList2.get(i).Recipient == arrayList2Temp.get(j).Recipient && arrayList2.get(i).ID == arrayList2Temp.get(j).ID) {
k = j;
}
}
if (found == false) {
arrayList2Temp.remove(k);
list2TempItemCount = arrayList2Temp.size();
}
}
endTime = System.nanoTime();
duration = (endTime - startTime) / 1000000;
System.out.println(duration);
//consistency check
for (int i = 0; i < list0ItemCount; i++) {
if (arrayList2.get(i).ID != arrayList1.get(i).ID ||
arrayList2.get(i).Recipient != arrayList1.get(i).Recipient ||
arrayList2.get(i).Score != arrayList1.get(i).Score ||
arrayList2.get(i).CumulativeScore != arrayList1.get(i).CumulativeScore) {
System.out.println("error1");
}
}
}
}
的Class1
package delete4;
/**
*
* @author
*/
public class Class1 {
int ID;
int Recipient;
int Score;
int CumulativeScore;
Class1(int ID, int Recipient, int Score, int CumulativeScore) {
this.ID = ID;
this.Recipient = Recipient;
this.Score = Score;
this.CumulativeScore = CumulativeScore;
}
}