我在Java中有这个数组:
private int[] all = new int [] {0,1,2,3,4,5};
我想设置一个等于all-{2,3,5}
的新var(等于{0,1,4})
是否有任何内置函数在两个数组之间执行减法?
答案 0 :(得分:2)
您可以使用Java 8流编写集合减法。
我们的想法是构造一个您想要排除的整数哈希集,然后运行原始数组,根据O(1)中的排除集检查每个元素,并仅保留不应排除的元素
int[] all=new int [] {0,1,2,3,4,5};
int[] ex=new int [] {2,3,5};
Set<Integer> exSet = IntStream.of(ex).boxed().collect(Collectors.toCollection(HashSet::new));
int[] res = IntStream.of(all).filter(val -> !exSet.contains(val)).toArray();
答案 1 :(得分:1)
我明白你想说什么,想出办法,你究竟想要什么。 如果使用ArrayList,则可以使用removerAll()来删除或减去。 让我们看看工作代码
import java.util.ArrayList;
public class removeAllInArrayList {
public static void main(String[] args) {
ArrayList <Integer> numbers = new ArrayList<Integer>();
numbers.add(0);
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);
System.out.print("The number array contains:- \t");
display (numbers);
ArrayList <Integer> numbers2 = new ArrayList<Integer>();
numbers2.add(0);
numbers2.add(2);
numbers2.add(4);
numbers2.add(6);
System.out.print("The number2 array contains:- \t ");
display(numbers2);
System.out.println("Subtract number2 from number.");
numbers.removeAll(numbers2);
System.out.print("Now, number array contains:- \t");
display (numbers);
}
public static void display (ArrayList array){
for (int i =0 ; i < array.size() ; i++){
System.out.print(array.get(i) + " ");
}
System.out.println ();
}
}
以下代码的结果是
The number array contains:- 0 1 2 3 4 5
The number2 array contains:- 0 2 4 6
Subtract number2 from number.
Now, number array contains:- 1 3 5
希望这有效,并有助于解决代码。
答案 2 :(得分:0)
我不认为有这种内置方法。您可以尝试找到一些可以做到这一点的库,或者编写自己的方法:
public static int[] removeEqual(int[] a_arr, int[] b_arr) {
List<Integer> list = new ArrayList<>();
for (int a : a_arr) { list.add(a); }
Iterator<Integer> iter = list.iterator();
while(iter.hasNext()) {
int a = iter.next();
for (int b : b_arr) {
if (a == b) {iter.remove();}
}
}
int[] result = new int[list.size()];
for (int i = 0; i < list.size(); i++) {result[i] = list.get(i);}
return result;
}
测试
public static void main(String[] args) {
System.out.println("expected: [0, 1, 4], actual: " +
Arrays.toString(removeEqual(new int[] {0,1,2,3,4,5}, new int[] {2, 3, 5})));
}
输出:预期:[0,1,4],实际:[0,1,4]
答案 3 :(得分:0)
我测试了Streams vs Collections:
public static void main(String... args)
{
Random rand = new Random();
int[] all = new int[100000];
int[] ex = new int[50000];
fillArrays(rand, all, ex);
long start = System.currentTimeMillis();
testWithStreams(all, ex);
System.out.println(String.format("Streams: %d", System.currentTimeMillis() - start));
fillArrays(rand, all, ex);// Avoid any external optimization
start = System.currentTimeMillis();
testWithCollections(all, ex);
System.out.println(String.format("Collections: %d", System.currentTimeMillis() - start));
fillArrays(rand, all, ex);// Avoid any external optimization
start = System.currentTimeMillis();
testWithCollectionsToArray(all, ex);
System.out.println(String.format("Collections -> Array: %d", System.currentTimeMillis() - start));
fillArrays(rand, all, ex);// Avoid any external optimization
start = System.currentTimeMillis();
testWithStreamsAndSet(all, ex);
System.out.println(String.format("Streams + HashSet: %d", System.currentTimeMillis() - start));
}
private static void fillArrays(Random rand, int[] all, int[] ex)
{
for (int i = 0; i < all.length; i++)
{
all[i] = rand.nextInt();
}
for (int i = 0; i < ex.length; i++)
{
ex[i] = all[rand.nextInt(all.length)];
}
// System.out.println(String.format("all values: %d, %d, %d", all[0], all[1], all[2]));
// System.out.println(String.format("ex values: %d, %d, %d", ex[0], ex[1], ex[2]));
}
private static int[] testWithStreams(int[] all, int[] ex)
{
return Arrays.stream(all).filter(
(elementOfAll) -> Arrays.stream(ex).noneMatch(
(elementOfEx) -> elementOfAll == elementOfEx
)
).toArray();
}
private static List<Integer> testWithCollections(int[] all, int[] ex)
{
List<Integer> listOfAll = Arrays.stream(all).boxed().collect(Collectors.toList());
listOfAll.removeAll(Arrays.stream(ex).boxed().collect(Collectors.toList()));
return listOfAll;
}
private static int[] testWithCollectionsToArray(int[] all, int[] ex)
{
return testWithCollections(all, ex).stream().mapToInt((v) -> v).toArray();
}
private static int[] testWithStreamsAndSet(int[] all, int[] ex)
{
HashSet<Integer> exSet = Arrays.stream(ex).boxed().collect(Collectors.toCollection(HashSet::new));
return Arrays.stream(all).filter((element) -> !exSet.contains(element)).toArray();
}
输出:
Streams: 13823
Collections: 2905
Collections -> Array: 2931
Streams + HashSet: 29
编辑: 添加了dasblinkenlight
的示例答案 4 :(得分:0)
您可以使用列表执行所需操作:
List<Integer> all = new ArrayList<>(Arrays.asList(0, 1, 2, 3, 4, 5));
List<Integer> toRemove = Arrays.asList(2, 3, 5);
all.removeAll(toRemove);
System.out.println(all); // [0, 1, 4]
为了能够从all
列表中删除元素,您应该使用new ArrayList<>(anotherList)
构造函数创建元素,因为Arrays.asList
返回的列表不能在结构上进行修改。