2014年1月2日星期四

Java List intersection , union and complement

2 List: A and B, each of size () are tens of thousands of levels , what algorithm can efficiently calculate their intersection , union and complement

order and set an example, it is easy to think of the following algorithm :
A.removeAll (B);
A.addAll (B);

It was found , however , removeAll and addAll efficiency is very low, 2 List all levels when tens of thousands , even to deal with them tens of seconds , the other trying to write their own algorithms , the fastest they have 10 seconds bell 's processing time , we would like to ask what is better algorithm can quickly calculate it? You can also do List, as long as the realization of the intersection of two sets of big data , and sets and complement
------ Solution ------------------- -------------------------
with linkedList to delete or insert a higher efficiency.

union can put all the elements into the set list , so you can get rid repetition and set.

intersection recyclable smaller list, and then one by one to determine whether the other list contains the element that contains it into a new list.
------ For reference only -------------------------------------- -


Thank you very much , experiment a bit , the same A.removeAll (B); and A.addAll (B);, LinkedList ArrayList efficiency with almost the same , even worse than some of the ArrayList , but with Set really rapidly ! My own research goes further in specific how to completely replace the use of Set List, then ask any other questions !

没有评论:

发表评论