[英]how to compare arraylist with list<string[]> java
List<String[]> sarray;
ArrayList<ContentTable> currentData=new ArrayList<ContentTable>();
//here sarray is initialized with data
sarray = reader.readAll();
for(String[] arr : sarray)
{
System.out.println("array data "+ Arrays.toString(arr));
}
for(ContentTable ct : currentData)
{
System.out.println("list data "+ct.getId() +" "+ ct.getSubid() +" "+ct.getChpid()+" "+ct.getSec_name()+" "+ct.getContent());
}
输出1个数组和列表的结果:
数组数据 - > [9, 10, 83, Concepts: 1-10, <p>We’ll discuss many of the concepts in this chapter in depth later. But for now, we need a brief review of these concepts to equip you for solving exercises in the chapters that follow.</p>]
[9, 10, 83, Concepts: 1-10, <p>We’ll discuss many of the concepts in this chapter in depth later. But for now, we need a brief review of these concepts to equip you for solving exercises in the chapters that follow.</p>]
列表数据 - > 9 10 83 Concepts: 1-10 <p>We’ll discuss many of the concepts in this chapter in depth later. But for now, we need a brief review of these concepts to equip you for solving exercises in the chapters that follow.</p>
9 10 83 Concepts: 1-10 <p>We’ll discuss many of the concepts in this chapter in depth later. But for now, we need a brief review of these concepts to equip you for solving exercises in the chapters that follow.</p>
//fields with getters and setters in ContentTable Class
public class ContentTable {
int id;
int subid;
int chpid;
String sec_name;
String content;
}
现在我想要实现的是创建两个列表,
ArrayList<ContentTable> updatedData=new ArrayList<ContentTable>();
ArrayList<ContentTable> addedData=new ArrayList<ContentTable>();
在比较sarray
和currentdata
之后,这些将被填充数据,以这种方式,
如果ct.getSec_name()
或ct.getContent()
在一个特定的索引currentdata
不等于存在于该数据sarray
然后将它添加到updatedData
和,
如果特定索引处的ct.getId()
, ct.getSubid()
, ct.getChpid()
不等于任何sarray
数据,那么它将被添加到addedData
以较低的复杂度执行此操作的优雅方法是什么?我希望尽可能快地完成它,因为比较Arraylist currentData
中的每个元素以与ArrayList sarray
每个元素进行比较可能需要时间。
如何将数组中的每个元素包装到Content。 现在将这些内容对象添加到Set。 迭代ArrayList并对于数组列表中的每个元素,检查它是否存在于集合中。 如果没有更新addedData。 如果是,请获取该对象并使用equals比较两个内容对象。
请注意,您无论如何都会覆盖hashCode并在Content类型中等于使(Set)工作。
你在开始时做的一次性活动需要时间,这就是准备就绪。 但是一旦这个设置准备就绪,那么看起来非常快。
因此,对我的问题更好的解决方案是不将List<String[]>
与ArrayList
,而是将List<String[]>
转换为另一个Arraylist
,然后将新的Arraylist
与旧的Arraylist
进行比较。 我发现,这是一种非常简单易行的方法。
for(String[] arr : sarray)
{
ContentTable data=new ContentTable();
for(String s : arr)
{
data.setId(Integer.parseInt(s));
data.setSubid(Integer.parseInt(s));
data.setChpid(Integer.parseInt(s));
data.setSec_name(s);
data.setContent(s);
}
newData.add(data);
}
现在,newData正在拥有我想要与currentData进行比较的新数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.