从列表中创建另一组列表,其中包含所有重复的项目
From a list , create another set of list that contain all the items that are repeated
我有一个列表
l = ["a","z","c","c","a","e","e"]
我需要在 java
中生成一组列表如下
l1=["a"]
l2=["z"]
l3=["c","c"]
l4=["a"]
l5=["e","e"]
下面是我目前的代码
package test
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class test {
public static void main(String[] args) {
List<String> l =new ArrayList<>();
List<List> ll =new ArrayList<>();
List<String> nl;
l.add("a");
l.add("z");
l.add("c");
l.add("c");
l.add("a");
l.add("a");
l.add("e");
String prev="";
nl = new ArrayList<>();
for (String x : l){
if(prev.equals(x))
{
nl.add(x);
}
else {
if(!nl.isEmpty()) {
ll.add(nl);
}
else{
List<String> nl1 = new ArrayList<>();
nl1.add(x);
ll.add(nl1);
}
}
prev=x;
}
System.out.println(ll);
}
}
产出
[[a], [z], [c], [c, a], [c, a]]
预计
[[a], [z], [c,c], [a,a], [e]]
通过对您的代码进行一些小的修改,下面的代码片段应该会给您带来不希望的结果:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Example {
public static void main(String[] args) {
List<String> values = Arrays.asList("a","z","c","c","a","e","e");
List<List<String>> result = new ArrayList<>();
String previous = null;
List<String> subList = new ArrayList<>();
for (String value : values) {
if (previous == null || value.equals(previous)) {
subList.add(value);
} else {
result.add(subList);
subList = new ArrayList<>();
subList.add(value);
}
previous = value;
}
if (!subList.isEmpty()) {
result.add(subList);
}
System.out.println(result);
}
}
输出:
[[a], [z], [c, c], [a], [e, e]]
另一种使用流和 AtomicReference 的方法:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
public class Example {
public static void main(String[] args) {
List<String> input = Arrays.asList("a", "z", "c", "c", "a", "e", "e");
AtomicReference<String> ar = new AtomicReference<>(input.get(0));
AtomicInteger ai = new AtomicInteger();
List<List<String>> result = new ArrayList<>(
input.stream().collect(
Collectors.groupingBy(str -> str.equals(ar.getAndSet(str)) ? ai.get() : ai.incrementAndGet()))
.values());
System.out.println(result);
}
}
我有一个列表
l = ["a","z","c","c","a","e","e"]
我需要在 java
中生成一组列表如下l1=["a"]
l2=["z"]
l3=["c","c"]
l4=["a"]
l5=["e","e"]
下面是我目前的代码
package test
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class test {
public static void main(String[] args) {
List<String> l =new ArrayList<>();
List<List> ll =new ArrayList<>();
List<String> nl;
l.add("a");
l.add("z");
l.add("c");
l.add("c");
l.add("a");
l.add("a");
l.add("e");
String prev="";
nl = new ArrayList<>();
for (String x : l){
if(prev.equals(x))
{
nl.add(x);
}
else {
if(!nl.isEmpty()) {
ll.add(nl);
}
else{
List<String> nl1 = new ArrayList<>();
nl1.add(x);
ll.add(nl1);
}
}
prev=x;
}
System.out.println(ll);
}
}
产出
[[a], [z], [c], [c, a], [c, a]]
预计
[[a], [z], [c,c], [a,a], [e]]
通过对您的代码进行一些小的修改,下面的代码片段应该会给您带来不希望的结果:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Example {
public static void main(String[] args) {
List<String> values = Arrays.asList("a","z","c","c","a","e","e");
List<List<String>> result = new ArrayList<>();
String previous = null;
List<String> subList = new ArrayList<>();
for (String value : values) {
if (previous == null || value.equals(previous)) {
subList.add(value);
} else {
result.add(subList);
subList = new ArrayList<>();
subList.add(value);
}
previous = value;
}
if (!subList.isEmpty()) {
result.add(subList);
}
System.out.println(result);
}
}
输出:
[[a], [z], [c, c], [a], [e, e]]
另一种使用流和 AtomicReference 的方法:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
public class Example {
public static void main(String[] args) {
List<String> input = Arrays.asList("a", "z", "c", "c", "a", "e", "e");
AtomicReference<String> ar = new AtomicReference<>(input.get(0));
AtomicInteger ai = new AtomicInteger();
List<List<String>> result = new ArrayList<>(
input.stream().collect(
Collectors.groupingBy(str -> str.equals(ar.getAndSet(str)) ? ai.get() : ai.incrementAndGet()))
.values());
System.out.println(result);
}
}