Question

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.

Was it helpful?

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.

OTHER TIPS

Arrays.asList() returns fixed-sized list which is unmodified list. You can not perform add/remove operation on it, on doing so will throw Exception UnsupportedOperationException.

So your first method is appropriate if you want to do some operation with the list

You cant add anything in the list return by code 2 because it returns fixed size list.You will get "java.lang.UnsupportedOperationException " but you can add in the list return by code 1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top