Search code examples
javasolrdataimporthandler

Why return the copy of the array?


I am reading the source code of dataimporthandler component of solr. I meet a question

  private List<String> readBySplit(String splitBy, String value) {
    String[] vals = value.split(splitBy);
    List<String> l = new ArrayList<String>();
    l.addAll(Arrays.asList(vals));
    return l;
  }

↑ listing 1 (method from RegexTransformer class)

  private List<String> readBySplit(String splitBy, String value) {
    String[] vals = value.split(splitBy);
    return Arrays.asList(vals);
  }

↑ listing 2 (I think the above method should be)

Can anyone tell me what the significant difference between above two code listings? Thanks.


Solution

  • Arrays.asList() Returns a fixed-size list backed by the specified array.

    That's from javadoc. So if you want a dynamic sized list, you need first code.