简体   繁体   English

如何比较arraylist与列表 <string[]> java的

[英]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());
        }   

Output for 1 result of array and list : 输出1个数组和列表的结果:

array data -> [9, 10, 83, Concepts: 1-10, <p>We&#x2019;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&#x2019;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&#x2019;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>]

list data -> 9 10 83 Concepts: 1-10 <p>We&#x2019;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&#x2019;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&#x2019;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;
          }

Now what I want to achieve is to create two lists , 现在我想要实现的是创建两个列表,

ArrayList<ContentTable> updatedData=new ArrayList<ContentTable>();
ArrayList<ContentTable> addedData=new ArrayList<ContentTable>();

these would be filled with data after comparison of sarray and currentdata in such a way that, 在比较sarraycurrentdata之后,这些将被填充数据,以这种方式,

if ct.getSec_name() or ct.getContent() at a particular index in currentdata is not equal to the data present in sarray then it would be added to updatedData 如果ct.getSec_name()ct.getContent()在一个特定的索引currentdata不等于存在于该数据sarray然后将它添加到updatedData

And, 和,

if ct.getId() , ct.getSubid() , ct.getChpid() at a particular index is not equal to any of sarray data then it would be added to addedData 如果特定索引处的ct.getId()ct.getSubid()ct.getChpid()不等于任何sarray数据,那么它将被添加到addedData

What would be the elegant way to do this with lesser complexity and I want to do it fastest as it might take time to compare each element in Arraylist currentData to compare with each element in ArrayList sarray . 以较低的复杂度执行此操作的优雅方法是什么?我希望尽可能快地完成它,因为比较Arraylist currentData中的每个元素以与ArrayList sarray每个元素进行比较可能需要时间。

How about wrapping each element in array to Content. 如何将数组中的每个元素包装到Content。 Now adding these content objects to Set. 现在将这些内容对象添加到Set。 Iterate over the ArrayList and for each element in array list, check if its present in the set. 迭代ArrayList并对于数组列表中的每个元素,检查它是否存在于集合中。 If not update the addedData. 如果没有更新addedData。 If yes, get that object and compare both the content objects using equals. 如果是,请获取该对象并使用equals比较两个内容对象。

Note that you would anyway override the hashCode and equals in Content type to make this(Set) work. 请注意,您无论如何都会覆盖hashCode并在Content类型中等于使(Set)工作。

The one time activity that you do in the beginning will take time, which is getting the set ready. 你在开始时做的一次性活动需要时间,这就是准备就绪。 But once this set is ready, look ups with be really fast. 但是一旦这个设置准备就绪,那么看起来非常快。

So, a better solution for my problem was to not compare a List<String[]> to ArrayList , but to convert List<String[]> to another Arraylist and then compare the new Arraylist against the older one. 因此,对我的问题更好的解决方案是不将List<String[]>ArrayList ,而是将List<String[]>转换为另一个Arraylist ,然后将新的Arraylist与旧的Arraylist进行比较。 And that's quite simple and easier approach , I found. 我发现,这是一种非常简单易行的方法。

                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);
                }

Now, newData is having the fresh data that I want to compare against currentData. 现在,newData正在拥有我想要与currentData进行比较的新数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM