Collections Java:使用.stream转换ArrayList<;MyObj>;到TreeMap<;字符串,ArrayList<;MyObj>&燃气轮机;
这是一个Java 8低级中间问题: 我在Java 6中有以下代码:Collections Java:使用.stream转换ArrayList<;MyObj>;到TreeMap<;字符串,ArrayList<;MyObj>&燃气轮机;,collections,lambda,java-8,Collections,Lambda,Java 8,这是一个Java 8低级中间问题: 我在Java 6中有以下代码: List <ViewWrapperContentElementTypeProperty> vwPropertyList = getFromDao(); TreeMap <Long, ArrayList<ViewWrapperContentElementTypeProperty>> mappedProperties = new TreeMap<Long, ArrayLi
List <ViewWrapperContentElementTypeProperty> vwPropertyList = getFromDao();
TreeMap <Long, ArrayList<ViewWrapperContentElementTypeProperty>> mappedProperties = new TreeMap<Long, ArrayList<ViewWrapperContentElementTypeProperty>> ();
for (ViewWrapperContentElementTypeProperty vwCetP:vwPropertyList)
{
if(null==mappedProperties.get(vwCetP.getContentElementTypeId()))
{
ArrayList<ViewWrapperContentElementTypeProperty> list = new ArrayList<ViewWrapperContentElementTypeProperty>());
list.add(vwCetP);
mappedProperties.put(vwCetP.getContentElementTypeId(), list);
}
else
{
mappedProperties.get(vwCetP.getContentElementTypeId()).add(vwCetP);
}
}
List vwPropertyList=getFromDao();
TreeMap mappedProperties=newtreemap();
对于(ViewWrapperContentElementTypeProperty vwCetP:vwPropertyList)
{
if(null==mappedProperties.get(vwCetP.getContentElementTypeId())
{
ArrayList=新建ArrayList());
列表。添加(vwCetP);
put(vwCetP.getContentElementTypeId(),list);
}
其他的
{
获取(vwCetP.getContentElementTypeId()).add(vwCetP);
}
}
我可以使用vwPropertyList.stream().map()更有效地实现这一点吗?关于使用
流和lambda表达式,当然。。。这应该如下所示:
Map<Long, List<ViewWrapperContentElementTypeProperty>> mappedProperties =
vwPropertyList.stream()
.collect(Collectors.groupingBy(ViewWrapperContentElementTypeProperty::getContentElementTypeId));
映射映射属性=
vwPropertyList.stream()
.collect(收集器.groupingBy(ViewWrapperContentElementTypeProperty::getContentElementTypeId));
请注意,使用上述流API方法会强制使用接口(Map
,List
),这是一种很好的做法
说到性能,它应该与使用传统循环大致相同。关于使用Stream
s和lambda表达式,当然。。。这应该如下所示:
Map<Long, List<ViewWrapperContentElementTypeProperty>> mappedProperties =
vwPropertyList.stream()
.collect(Collectors.groupingBy(ViewWrapperContentElementTypeProperty::getContentElementTypeId));
映射映射属性=
vwPropertyList.stream()
.collect(收集器.groupingBy(ViewWrapperContentElementTypeProperty::getContentElementTypeId));
请注意,使用上述流API方法会强制使用接口(Map
,List
),这是一种很好的做法
在性能方面,它应该与使用传统循环大致相同。您似乎在寻找一种按操作分组的方法。幸运的是,该类提供了一种实现这一点的方法:
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toCollection;
...
TreeMap<Long, ArrayList<ViewWrapperContentElementTypeProperty>> mappedProperties =
vwPropertyList.stream()
.collect(groupingBy(ViewWrapperContentElementTypeProperty::getContentElementTypeId,
TreeMap::new,
toCollection(ArrayList::new)));
导入静态java.util.stream.Collectors.groupingBy;
导入静态java.util.stream.Collectors.toCollection;
...
树映射属性=
vwPropertyList.stream()
.collect(分组依据)(ViewWrapperContentElementTypeProperty::getContentElementTypeId,
树映射::新的,
toCollection(ArrayList::new));
我使用了重载版本,您可以在其中提供特定的映射实现(如果您确实需要TreeMap
)
另外,toList()
收集器返回一个列表(这是一个ArrayList
,但它是一个实现细节)。由于您显然需要指定一个具体的实现,因为您希望ArrayList
s作为值,所以您可以使用toCollection(ArrayList::new)
来完成这项工作。您似乎在按操作寻找分组。幸运的是,该类提供了一种实现这一点的方法:
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toCollection;
...
TreeMap<Long, ArrayList<ViewWrapperContentElementTypeProperty>> mappedProperties =
vwPropertyList.stream()
.collect(groupingBy(ViewWrapperContentElementTypeProperty::getContentElementTypeId,
TreeMap::new,
toCollection(ArrayList::new)));
导入静态java.util.stream.Collectors.groupingBy;
导入静态java.util.stream.Collectors.toCollection;
...
树映射属性=
vwPropertyList.stream()
.collect(分组依据)(ViewWrapperContentElementTypeProperty::getContentElementTypeId,
树映射::新的,
toCollection(ArrayList::new));
我使用了重载版本,您可以在其中提供特定的映射实现(如果您确实需要TreeMap
)
另外,toList()
收集器返回一个列表(这是一个ArrayList
,但它是一个实现细节)。由于您显然需要指定一个具体的实现,因为您希望ArrayList
s作为值,因此可以使用toCollection(ArrayList::new)
来完成此操作,它不会强制您对接口进行编程。虽然在实践中很好,但您仍然有可能提供一个具体的实现——因为在某些情况下,您可能明确需要一个具体的实现。。。但这种情况应该是例外,而不是规则。但是,如果Jake碰巧需要一个特定的树状图,例如,那么您是对的,他应该继续您的方式。树状图是首选的,因为结果将直接显示在视图中,并且需要顺序。省去了编写比较器的时间,对资源的需求也很低,因为地图永远不会很复杂big@Jake:为什么不用编写比较器?如果没有比较器的TreeMap
就足够了,那么没有比较器的排序也可以。只有当预期顺序与自然顺序不同(或没有)时,才需要比较器。但是,TreeMap
也需要一个比较器……我想我们可能误解了彼此。我只是假设其他评论者对TreeMap的质疑是由于性能较慢。TreeMap保证了自然的键顺序(在我的例子中是字母顺序),而HashMap则不保证。这就是我选择树形图的原因。它不会强迫你编程到一个接口。虽然在实践中很好,但您仍然有可能提供一个具体的实现——因为在某些情况下,您可能明确需要一个具体的实现。。。但这种情况应该是例外,而不是规则。但是,如果Jake碰巧需要一个特定的树状图,例如,那么您是对的,他应该继续您的方式。树状图是首选的,因为结果将直接显示在视图中,并且需要顺序。省去了编写比较器的时间,对资源的需求也很低,因为地图永远不会很复杂big@Jake:为什么不用编写比较器?如果没有比较器的TreeMap
就足够了,那么没有比较器的排序也可以。比较器是o