我有
List<String> listOne = new ArrayList<String>();
listOne.add("3");
listOne.add("11");
listOne.add("20");`
和
List<String> listTwo = new ArrayList<String>();
listTwo.add("1");
listTwo.add("2");
listTwo.add("3");
listTwo.add("6-11");
listTwo.add("18-20");
我想从listTwo
中存在的listOne
元素中删除。在此示例中,应删除的值包括:3
,6-11
和18-20
。 6-11
因为在listOne
中我们的值11
和18-20
的值相同。
如果可能的话,你能帮助我吗?我不知道如何解决它。由于其删除规则,这与其他人不同。否则,另一个类似问题的其他答案并没有给我解决方案。
答案 0 :(得分:1)
public class Test {
public static void main(String[] args) {
List<String> listOne = new ArrayList<String>();
listOne.add("3");
listOne.add("11");
listOne.add("20");
List<String> listTwo = new ArrayList<String>();
listTwo.add("1");
listTwo.add("2");
listTwo.add("3");
listTwo.add("6-11");
listTwo.add("18-20");
List<String> thirdList = new ArrayList<String>();
for (String secondItem : listTwo) {
boolean match = false;
for (String firstItem : listOne) {
if(secondItem.contains(firstItem)) {
match = true;
}
}
if(!match) {
thirdList.add(secondItem);
}
}
for (String string : thirdList) {
System.out.println(string);
}
}
}
答案 1 :(得分:1)
注释表明只有明确列出的值才能导致元素被删除,例如:
listOne listTwo Action
"1" "1", "3-9", "11" remove "1", but not "11"
"2" "1", "3-9", "11" do nothing
"3" "1", "3-9", "11" remove "3-9"
"4" "1", "3-9", "11" do nothing (3-9 is not a range)
所以,有了这个奇怪的逻辑,这是一个例子:
public static void main(String[] args) {
// should remove 3, 6-11, 18-20
System.out.println(remove(Arrays.asList("3", "11", "20"),
new ArrayList<>(Arrays.asList("1", "2", "3", "6-11", "18-20"))));
// should remove 2, not remove 6-11 (not a range), and not remove 18-20 (8 is not 18, 2 is not 20)
System.out.println(remove(Arrays.asList("2", "8"),
new ArrayList<>(Arrays.asList("1", "2", "3", "6-11", "18-20"))));
}
private static List<String> remove(List<String> listOne, List<String> listTwo) {
for (Iterator<String> listIter = listTwo.iterator(); listIter.hasNext(); ) {
String value = listIter.next();
if (shouldRemove(value, listOne))
listIter.remove();
}
return listTwo; // for easy of use
}
private static boolean shouldRemove(String value, List<String> listOne) {
int idx = value.indexOf('-');
if (idx == -1) {
for (String ref : listOne)
if (ref.equals(value))
return true;
} else {
String value1 = value.substring(0, idx);
String value2 = value.substring(idx + 1);
for (String ref : listOne)
if (ref.equals(value1) || ref.equals(value2))
return true;
}
return false;
}
输出
[1, 2]
[1, 3, 6-11, 18-20]
答案 2 :(得分:0)
public static void main(String[] args) {
List<String> listOne = new ArrayList<>();
listOne.add("3");
listOne.add("11");
listOne.add("20");
List<String> listTwo = new CopyOnWriteArrayList<>();
listTwo.add("1");
listTwo.add("2");
listTwo.add("3");
listTwo.add("6-11");
listTwo.add("18-20");
listOne.stream().forEach(value -> {
listTwo.stream().filter(value2 -> (value2.contains(value))).forEach(value2 -> {
String[] minMax = value2.split(value2);
if (minMax.length == 1 && value2.equals(value)) {
listTwo.remove(value2);
} else if (minMax.length == 2) {
int intValue2 = Integer.parseInt(value2);
int min = Integer.parseInt(minMax[0]);
int max = Integer.parseInt(minMax[1]);
if (intValue2 >= min && intValue2 <= max) {
listTwo.remove(value2);
}
}
});
});
listTwo.stream().forEach(System.out::println);
}
答案 3 :(得分:0)
List<String> listOne = new ArrayList<String>();
listOne.add("3");
listOne.add("11");
listOne.add("20");
List<String> listTwo = new ArrayList<String>();
listTwo.add("1");
listTwo.add("2");
listTwo.add("3");
listTwo.add("6-11");
listTwo.add("18-20");
List<String> values = new ArrayList<>(); // to store matched Strings
for (String s : listTwo) {
String strs[] = new String[2];
if (s.contains("-")) {
strs = s.split("-"); // check for Strings
}
if (listOne.contains(s)
|| listOne.contains(strs[0])
|| listOne.contains(strs[1])) {
values.add(s); // save the value to a list
}
}
listTwo.removeAll(values);