What's the harm in using Anonymous class?

后端 未结 6 1230
-上瘾入骨i
-上瘾入骨i 2021-01-31 08:41

The question arose while reading a answer to this question - How do I join two lists in java. This answer gave the solution

List newList = new Arra         


        
6条回答
  •  没有蜡笔的小新
    2021-01-31 09:14

    It's not a bad approach per se, say, in performance or anything like that, but the approach is a bit obscure and when using something like this, you always (say, 99%) have to explain this approach. I think that's one of the biggest reasons not to use this approach, and while typing:

    List newList = new ArrayList();
    newList.addAll(listOne);
    newList.addAll(listTwo);
    

    is a little more typing, it's a bit easier to read, which helps a lot in understanding or debugging code.

提交回复
热议问题