简体   繁体   English

从列表中创建固定大小的子列表,如果元素较少,则添加零以完成子列表

[英]Create a sublist of fixed size from a list and if the elements are less add zeros to complete the sublist

I am new to Java 8 and trying the implement creating partitions of fixed size of 5 elements from a list. 我是Java 8的新手,正在尝试从列表中创建5个元素的固定大小分区的工具。 If the list is short of elements then i need to add zeros to it. 如果列表缺少元素,那么我需要在其中添加零。

For example: If the original list has below elements: [1,2,3,4,5,6,7] 例如:如果原始列表包含以下元素:[1,2,3,4,5,6,7]

The sublist will be as below: [1,2,3,4,5],[6,7,0,0,0] 子列表如下:[1,2,3,4,5],[6,7,0,0,0]

So far, i found the below method. 到目前为止,我发现了以下方法。 Can someone please help me with changing this to add zeros if a sublist is less than 5? 如果子列表少于5,有人可以帮我更改此值以添加零吗?

public static <T> List<List<T>> splitLists(List<T> alist, final int len) {
    return IntStream.range(0, alist.size()) 
            .filter(i -> i % len == 0) 
            .boxed() 
            .map(i -> alist.subList(i, Math.min(i + len, alist.size()))) 
            .collect(Collectors.toList()); 
}

Also, i need to pass a list of BigDecimal to this method,but want to keep this a generic list only so that i can reuse this method in other scenarios as well. 另外,我需要将BigDecimal的列表传递给此方法,但只想保留此通用列表,以便在其他情况下也可以重用此方法。

List<BigDecimal> l = new ArrayList<>();
l.add(new BigDecimal(1));
l.add(new BigDecimal(2));
l.add(new BigDecimal(3));
l.add(new BigDecimal(4));
l.add(new BigDecimal(5));
l.add(new BigDecimal(6));
l.add(new BigDecimal(7));
List<List<BigDecimal>> findPattern = splitLists(l,5);

Changing the question after answers were postet is a bad habit. 在答案发布后更改问题是个坏习惯。 However, BigDecimal, here we go: 但是,BigDecimal,我们开始:

import java.math.BigDecimal

List<BigDecimal> l = new ArrayList<>();
l.add(new BigDecimal(1));
l.add(new BigDecimal(2));
l.add(new BigDecimal(3));
l.add(new BigDecimal(4));
l.add(new BigDecimal(5));
l.add(new BigDecimal(6));
l.add(new BigDecimal(7));

List<List<BigDecimal>> findPattern = splitLists(l, 5, new BigDecimal (0));

We calculate the length mismatch and add a neutral element. 我们计算长度不匹配,并添加一个中性元素。 Of course we need to get one of type T: 当然,我们需要获得T类型之一:

    int mismatch = len - (alist.size () % len);
    for (int i = 0; i < mismatch; ++i) 
         alist.add (neutral);


public static <T> List<List<T>> splitLists (final List<T> alist, final int len, T neutral) {
    int mismatch = len - (alist.size () % len);
    for (int i = 0; i < mismatch; ++i) alist.add (neutral) ;
    return IntStream.range(0, alist.size()) 
            .filter(i -> i % len == 0) 
            .boxed() 
            .map(i -> alist.subList(i, Math.min(i + len, alist.size()))) 
            .collect(Collectors.toList()); 
}

Call to that list expects one more parameter, the neutral element: 对该列表的调用需要另一个参数,即中性元素:

-> List<List<BigDecimal>> findPattern = splitLists(l, 5, new BigDecimal (0));
|  Modified variable findPattern of type List<List<BigDecimal>> with initial value [[1, 2, 3, 4, 5], [6, 7, 0, 0, 0]]
|    Update overwrote variable findPattern

(Testet in jshell, if you wonder about the fancy output.) (如果您想了解精美的输出,请使用jshell中的Testet。)

If you need your old List untouched, either pass a copy or create a copy in the method to work on. 如果您需要保持旧列表不变,请通过副本或在方法中创建副本以进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM