What's the harm in using Anonymous class?

后端 未结 6 1252
-上瘾入骨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:21

    Because you don't need a separate subclass - you just need to create a new ArrayList of the normal class, and addAll() both lists into it.

    Like so:

    public static List addLists (List a, List b) {
        List results = new ArrayList();
        results.addAll( a);
        results.addAll( b); 
        return results;
    }
    

    It's evil to create a subclass, that isn't needed. You don't need to extend or subclass behaviour - just change data values.

提交回复
热议问题