0

I have two collections

The first contains all the elements.

The second contains the elements that I am interested in from the first collection.

The data is in an alphabetical format:

AAA

AA.12.AA

BBB.234.B1

CC.89

The first collection contains around 300.000 records roughly.

Now, if I want to get 10 thousand records from the first collection it is taking up to 40 seconds to find them.

Collection types: firstColl = ArrayList , secondColl = List

Action: I iterate all elements in the firstColl and for every element I check if the secondColl has the element in it.

Just want to know if anyone knows a most performance way to do it by using maybe BigList, Streams,...

CODE:

List<RegionPolygon> regionPolygons = new ArrayList<>();

for (RegionPolygon regionPolygon: result) {

    if (regionsArray.contains(regionPolygon.getRegionRef())) {
        regionPolygons.add(regionPolygon);
    }
}

Note: RegionPolygon has a property which is a String with a very long value (more than 2000 thousand characters easily, although I am not using that property to check if it is the region that I am looking for). Just wanted to say this cause I don't know if this is part of the problem.

result = firstColl

regionsArray = secondColl

Thanks,

Aisatora
  • 444
  • 6
  • 17
  • 1
    *"Now, if I want to get 10 thousand records from the first collection it is taking up to 40 seconds to find them."* - how? What do you do, what 10 thousand records? What code have you used that takes 40 seconds? – luk2302 Apr 30 '19 at 13:17
  • 1
    plz give us some code to show what the data and data structures you are using – Matthew I. Apr 30 '19 at 13:17
  • Are those collections sorted in any way? – Aaron Apr 30 '19 at 13:20
  • Updated! @Aaron they come alphabetically sorted as they come from the DB. – Aisatora Apr 30 '19 at 13:25
  • 1
    @Aisatora Can't you do the selection on your DB instead? – Ivar Apr 30 '19 at 13:28
  • 1
    [This article](http://www.learn4master.com/algorithms/union-and-intersection-of-two-sorted-arrays-java) describes well how to take advantage of the order of the lists to compute their intersection – Aaron Apr 30 '19 at 13:29
  • 1
    https://stackoverflow.com/questions/7574311/efficiently-compute-intersection-of-two-sets-in-java – Satyendra Kumar Apr 30 '19 at 13:33
  • 1
    It would be a lot easier if `secondColl` were a `Set` or if `firstColl` were a `Map` from `regionRef`->`RegionPolygon` – Mark Jeronimus Apr 30 '19 at 14:16
  • Hi @MarkJeronimus, This is one of the examples that I want to try out... I will detail the results, thanks – Aisatora Apr 30 '19 at 14:18
  • @Ivar I can but I have all the data sorted in memory cause I will have to access the same set of data over and over and hitting the database each time does not sound efficient to me. Thanks, – Aisatora Apr 30 '19 at 14:20

0 Answers0