Java 是否有方法根据对象字段的唯一值的数量将对象列表拆分为n个列表?
我有一个返回对象列表的方法 在该对象中,有一个字符串字段orderType 有没有办法根据orderType的唯一值将该列表分成n个列表 例如,该方法返回一个包含10个对象的列表,其中4个对象的orderType值为x,3个对象的orderType值为y,另外4个对象的orderType值为z 然后结果将是3个列表,分别包含4、3、4个对象 orderType不仅具有x、y和z作为可能的值 我很难将其转化为实际代码,但我的想法是这样的Java 是否有方法根据对象字段的唯一值的数量将对象列表拆分为n个列表?,java,oop,Java,Oop,我有一个返回对象列表的方法 在该对象中,有一个字符串字段orderType 有没有办法根据orderType的唯一值将该列表分成n个列表 例如,该方法返回一个包含10个对象的列表,其中4个对象的orderType值为x,3个对象的orderType值为y,另外4个对象的orderType值为z 然后结果将是3个列表,分别包含4、3、4个对象 orderType不仅具有x、y和z作为可能的值 我很难将其转化为实际代码,但我的想法是这样的 List<Orders> orderList =
List<Orders> orderList = getOrder();
for(int i=0; i<orderList.size();i++){
if(orderList.get(i).orderType is unique){
create a new list
}else{
add to an existing list having the same orderType
}
}
List orderList=getOrder();
对于(int i=0;i我建议使用这个相当一般的函数:
public static <T, U> Map<U, List<T>> getLists(List<T> values, Function<T, U> orderBy) {
Map<U, List<T>> lists = new HashMap<>();
for (T value : values) {
U type = orderBy.apply(value);
if (!lists.containsKey(type)) {
lists.put(type, new ArrayList<>());
}
lists.get(type).add(value);
}
return lists;
}
如何使用:
public static void main(String args[]){
List<Order> orders = new ArrayList<>();
orders.add(new Order("TYPE1", "Order1"));
orders.add(new Order("TYPE2", "Order2"));
orders.add(new Order("TYPE1", "Order3"));
orders.add(new Order("TYPE1", "Order4"));
orders.add(new Order("TYPE2", "Order5"));
orders.add(new Order("TYPE1", "Order6"));
orders.add(new Order("TYPE1", "Order7"));
orders.add(new Order("TYPE1", "Order8"));
Map<String, List<Order>> map = getLists(orders, Order::getOrderType);
for (String key : map.keySet()) {
System.out.println(key);
for (Order order : map.get(key)) {
System.out.println(order.getOrderType() + " " + order.getName());
}
System.out.println();
}
}
publicstaticvoidmain(字符串参数[]){
列表顺序=新的ArrayList();
订单。添加(新订单(“类型1”、“订单1”);
订单。添加(新订单(“类型2”、“订单2”);
订单。添加(新订单(“类型1”、“订单3”);
订单。添加(新订单(“类型1”、“订单4”);
订单。添加(新订单(“类型2”、“订单5”);
订单。添加(新订单(“类型1”、“订单6”);
订单。添加(新订单(“类型1”、“订单7”);
订单。添加(新订单(“类型1”、“订单8”);
Map Map=GetList(订单,订单::getOrderType);
for(字符串键:map.keySet()){
系统输出打印项次(键);
for(顺序:map.get(key)){
System.out.println(order.getOrderType()+“”+order.getName());
}
System.out.println();
}
}
我认为将元素提取到映射更容易,然后可以利用收集器.toMap
第三个参数,该参数组合了同一个键的值
public void tryIt() {
// given
class Foo {
String x;
Foo(String x) {
this.x = x;
}
@Override
public String toString() {
return this.x;
}
}
List<Foo> foos = new ArrayList<>();
IntStream.range(0, 4).forEach(i -> {
foos.add(new Foo("x"));
foos.add(new Foo("z"));
});
IntStream.range(0, 3).forEach(i -> foos.add(new Foo("y")));
// create a mapping of element, to list of original object, grouping by same element key
Map<String, List<Foo>> collect = foos.stream().collect(Collectors.toMap(
f -> f.x,
Collections::singletonList,
(l1, l2) -> Stream.concat(l1.stream(), l2.stream()).collect(Collectors.toList())));
System.out.println(collect);
}
请显示一个和预期的输出请添加一些代码、您正在获得的输出和您想要的输出。您是否接受一个{“x”:[obj,obj,obj],“y”:[…],“z”:[…]}
?@OneCricketeer是的,一个地图可以工作toothanks man,我会先尝试理解代码,然后再尝试
public void tryIt() {
// given
class Foo {
String x;
Foo(String x) {
this.x = x;
}
@Override
public String toString() {
return this.x;
}
}
List<Foo> foos = new ArrayList<>();
IntStream.range(0, 4).forEach(i -> {
foos.add(new Foo("x"));
foos.add(new Foo("z"));
});
IntStream.range(0, 3).forEach(i -> foos.add(new Foo("y")));
// create a mapping of element, to list of original object, grouping by same element key
Map<String, List<Foo>> collect = foos.stream().collect(Collectors.toMap(
f -> f.x,
Collections::singletonList,
(l1, l2) -> Stream.concat(l1.stream(), l2.stream()).collect(Collectors.toList())));
System.out.println(collect);
}
{x=[x, x, x, x], y=[y, y, y], z=[z, z, z, z]}