在List中将List拆分为两个子列表的最简单,最标准和/或最有效的方法是什么?改变原始列表是可以的,因此不需要复制.方法签名可以是
/** Split a list into two sublists. The original list will be modified to
* have size i and will contain exactly the same elements at indices 0
* through i-1 as it had originally; the returned list will have size
* len-i (where len is the size of the original list before the call)
* and will have the same elements at indices 0 through len-(i+1) as
* the original list had at indices i through len-1.
*/ …Run Code Online (Sandbox Code Playgroud) 我有一个列表a,我想分成几个小列表.
说出包含"aaa"的所有项目,包含"bbb"和更多谓词的所有内容.
我怎么能用java8这样做?
我看到这篇文章,但它只分成两个列表.
public void partition_list_java8() {
Predicate<String> startWithS = p -> p.toLowerCase().startsWith("s");
Map<Boolean, List<String>> decisionsByS = playerDecisions.stream()
.collect(Collectors.partitioningBy(startWithS));
logger.info(decisionsByS);
assertTrue(decisionsByS.get(Boolean.TRUE).size() == 3);
}
Run Code Online (Sandbox Code Playgroud)
我看过这篇文章,但它在Java 8之前很老了.
我似乎只能找到有关列表中最后一个/第一个元素的答案,或者您可以获得特定项目等。
假设我有一个包含 100 个元素的列表,我想返回最后 40 个元素。我怎么做?我尝试这样做,但它给了我一个元素..
Post last40posts = posts.get(posts.size() -40);
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用此处所述的Google GCM Multicast Messaging .我需要同时发送50,000条消息.
文档说我应该将注册ID列表传递给Sender.send().
我的问题 我很困惑,我是否应该一次通过所有50,000个ID或1000个ID的列表,因为文档说"GCM中最有用的功能之一是支持单个邮件最多1,000个收件人."
[编辑1]这样可以吗?
Sender sender = new Sender(API_KEY);
List<List<String>> regIdsParts = regIdInThousands(getRegistrationIds(), 1000);
for (int i = 0; i < regIdsParts.size(); i++) {
Message message = new Message.Builder()
.addData(MsgKey, message).build();
MulticastResult result = sender.send(message, regIdsParts.get(i), 5);
}
public List<List<String>> regIdInThousands(List<String> list, final int L) {
List<List<String>> parts = new ArrayList<List<String>>();
final int N = list.size();
for (int i = 0; i < N; i += L) {
parts.add(new …Run Code Online (Sandbox Code Playgroud) 我知道有一个String拆分方法返回一个数组,但是我需要一个ArrayList。
我从文本字段(数字列表;例如2,6,9,5)获取输入,然后在每个逗号处将其分割:
String str = numbersTextField.getText();
String[] strParts = str.split(",");
Run Code Online (Sandbox Code Playgroud)
有没有办法用ArrayList而不是数组来做到这一点?
我有一个数组:
int[] array = {1,2,3,4,5,6,7,8,9,10};
Run Code Online (Sandbox Code Playgroud)
我想选择一个排序的起点.如果我选择6输出应该是
(6,7,8,9,10,1,2,3,4,5)
Run Code Online (Sandbox Code Playgroud)