【发布时间】:2017-12-25 13:36:35
【问题描述】:
我有一个方法如下
public List<List<CustomClass>> categorize(List<CustomClass> customClass){
List<List<CustomClass>> returnValue = new ArrayList<>();
for (CustomClass customClassValue: customClass) {
List<CustomClass> one = new ArrayList<>(), two = new ArrayList<>(), three = new ArrayList<>(), four = new ArrayList<>();
switch (customClassValue.getAge()){
case 1:
one.add(customClassValue);
break;
case 2:
two.add(customClassValue);
break;
case 3:
three.add(customClassValue);
break;
case 4:
four.add(customClassValue);
break;
}
returnValue.add(one);
returnValue.add(two);
returnValue.add(three);
returnValue.add(four);
}
return returnValue;
}
从有效性和性能的角度来看,除了使用List<List<CustomClass>> 之外,java 集合中还有其他选择吗?
简单描述一下函数的作用:
获取自定义对象列表的输入,并根据对象中的字段对其进行分类,并分别返回每个类别项。
【问题讨论】:
-
包含
List的自定义类呢?而不是List<List<CustomClass>>有List<CustomClass>并将第二个List放在您的自定义类中。 -
简单描述一下该函数的作用:获取自定义对象列表的输入,并根据对象中的字段对其进行分类,并分别返回每个类别项。
-
您正在创建许多列表,其中四分之一有一个
CustomClass,而其他列表是空的。你确定这是你想要做的吗? -
不创建列表并将它们添加到 returnValue 将超出 for 循环
-
@Venkat 不在您提供的代码中。如果您想获得有用的答案,也许您应该提供您正在使用的真实代码。
标签: java android performance collections