0

This is a portion of the string

471 pfm9254-disk01_01                                       0           io_grp0       online 3            Chassis2_IBMi       100.00GB  striped 0     fcmap0                60050768028080EF6400000000001391 1            1          empty            0             no        0                     
472 IP10-32-41-70_SLES11SP2_10G                             0           io_grp0       online 0            pfm3079_pfm3128     10.00GB   striped                             60050768028080EF64000000000007CE 0            1          empty            0             no        0                     
473 pfm3128_pva1091_stgvios1                                0           io_grp0       online 1            VIOS_StgPool        35.00GB   striped                             60050768028080EF64000000000007D1 0            1          not_empty        0             no        0                     
474 vdisk64                                                 0           io_grp0       online 13           pfm9253_pfm9254_new 9.94GB    striped                             60050768028080EF6400000000001CC4 0            1          empty            0             no        0                     
475 vdisk65                                                 0           io_grp0       online 13           pfm9253_pfm9254_new 10.00GB   striped                             60050768028080EF6400000000001CC6 0            1          empty            0             no        0                     

I want to check whether it contains any members of my arrayList

I know I can iterate over the arrayList and then check for each element whether the string contains the element. I am looking for an efficient solution

This is my ArrayList

ArrayList<String> volIDsSent = new ArrayList<String>();
volIDsSent.add("60050768028080EF6400000000000AC3");
volIDsSent.add("60050768028080EF6400000000000FF8");

I would get the string from somewhere line by line .

The_Lost_Avatar
  • 992
  • 5
  • 15
  • 35
  • Is your solution not efficient enough now? What kind of performance are you getting at the moment? – Kayaman Oct 24 '13 at 13:14
  • Kayaman : If I have 10 elements in the ArrayList and I am getting the string line by line I would check an element 1000 times then I would search 10000 times, pretty bad efficiency I think – The_Lost_Avatar Oct 24 '13 at 13:17
  • Concur with @Kayaman about cheking if performance is ok before doing anything. One thought for improvement _IF_ you are going to do the search multiple times is to read the array and do a regex. Then use the single regex to match into the string. – asantaballa Oct 24 '13 at 13:17
  • @MAD_ABOUT_JAVA I asked what kind of performance you're getting, not what you think about the performance. Also why would you check the String line by line? That doesn't sound efficient at all. – Kayaman Oct 24 '13 at 13:18
  • Yes, I would search multiple times so I don't mind time taken for the first time. – The_Lost_Avatar Oct 24 '13 at 13:21
  • Hmm, actually liked that enough to put as answer. May use it myself sometime. – asantaballa Oct 24 '13 at 13:22

2 Answers2

1

Consider reading the array once and building a regex string by concatenating with “|” between each one, either manually or with something like StringUtils.join. Then use the regex against each line to read.

asantaballa
  • 3,919
  • 1
  • 21
  • 22
0

If you want to check if one of the Strings in your ArrayList is a substring of your big String, you will have to iterate over the ArrayList and break if you find one occurence (if you really just want to know IF there is one ore more).

It cannot get more efficient because you need to check all of them as long as you havent found any occurence.

LionC
  • 3,106
  • 1
  • 22
  • 31
  • The string comes line by line so I was thinking if at the time of taking the string comes I would check if it has a particular match from the array list, I want to have some code like that. – The_Lost_Avatar Oct 24 '13 at 13:24