Is there a way to compare two regions of a char array in Java (or of two different arrays) without creating new objects in the heap?
有没有办法比较Java(或两个不同的数组)中char数组的两个区域而不在堆中创建新对象?
I know I can do something like this:
我知道我可以这样做:
char[] region1 = Arrays.copyOfRange(bigbuffer,0,100);
char[] region2 = Arrays.copyOfRange(bigbuffer,100,200);
if (Arrays.equals(region1,region2))
System.out.printf("Equal");
But that will create two objects that later have to be garbage collected and traverse the array buffer twice. It would be so much better if there were a function that could just compare the two regions. I imagine something like this:
但是这将创建两个对象,以后必须进行垃圾回收并遍历数组缓冲区两次。如果有一个功能可以比较这两个区域,那将会好得多。我想象这样的事情:
if (Arrays.compareRegions(bigBuffer,0,100,bigBuffer,100,200)==0)
System.out.printf("Equal");
Is there such a thing in Java?
Java中有这样的东西吗?
2 个解决方案
#1
2
You don't have to create new arrays - a single for
loop is enough.
您不必创建新数组 - 单个for循环就足够了。
for(int i=0; i < 100; i++) {
if (bigbuffer[i] != bigbuffer[i+100]) {
System.out.println("Not Equal");
break;
}
}
#2
1
Have had a quick search and seems no built-in solution in at least Java and most famous helper libraries (Commons Lang and Guava).
有一个快速搜索,似乎没有至少Java和最着名的帮助库(Commons Lang和Guava)的内置解决方案。
It should be trivial to write one yourself. Here is one sample (in psuedo-code) that do a equal comparison (instead of compare). Enhance it to fit your needs
自己写一个应该是微不足道的。这是一个样本(在伪代码中)进行相等比较(而不是比较)。增强它以满足您的需求
public static <T> boolean arrayEquals(T[] arr1, int startIndex1, T[] arr2, int startIndex2, int lengthToCompare) {
if (arr1 == null || arr2 == null) {
throws new NullPointerException();
}
if (arr1.length - lengthToCompare < startIndex1
|| arr2.length - lengthToCompare < startIndex2) {
throw new ArrayIndexOutOfBoundException();
}
// some extra checking like startIndex > 0 etc.
// This is the real logic anyway
for (int i = 0; i < lengthToCompare; ++i) {
if (! Objects.equals(arr1[startIndex + i], arr2[startIndex2 + i]) {
return false;
}
}
return true;
}
Another approach which only create several small objects in heap, if you are not dealing with primitive-type-array, is:
如果你不处理原始类型数组,另一种只在堆中创建几个小对象的方法是:
Foo[] arr1 = ....;
Foo[] arr2 = ....;
if (Arrays.asList(arr1).subList(0,100).equals(
Arrays.asList(arr2).subList(100,200)) {
....
}
This relies on the property that:
这取决于以下属性:
-
Arrays.asList()
returns aList
impl which internally backed-up by the input array -
subList()
returns a view of the original list, not a copy
Arrays.asList()返回一个由输入数组内部备份的List impl
subList()返回原始列表的视图,而不是副本
#1
2
You don't have to create new arrays - a single for
loop is enough.
您不必创建新数组 - 单个for循环就足够了。
for(int i=0; i < 100; i++) {
if (bigbuffer[i] != bigbuffer[i+100]) {
System.out.println("Not Equal");
break;
}
}
#2
1
Have had a quick search and seems no built-in solution in at least Java and most famous helper libraries (Commons Lang and Guava).
有一个快速搜索,似乎没有至少Java和最着名的帮助库(Commons Lang和Guava)的内置解决方案。
It should be trivial to write one yourself. Here is one sample (in psuedo-code) that do a equal comparison (instead of compare). Enhance it to fit your needs
自己写一个应该是微不足道的。这是一个样本(在伪代码中)进行相等比较(而不是比较)。增强它以满足您的需求
public static <T> boolean arrayEquals(T[] arr1, int startIndex1, T[] arr2, int startIndex2, int lengthToCompare) {
if (arr1 == null || arr2 == null) {
throws new NullPointerException();
}
if (arr1.length - lengthToCompare < startIndex1
|| arr2.length - lengthToCompare < startIndex2) {
throw new ArrayIndexOutOfBoundException();
}
// some extra checking like startIndex > 0 etc.
// This is the real logic anyway
for (int i = 0; i < lengthToCompare; ++i) {
if (! Objects.equals(arr1[startIndex + i], arr2[startIndex2 + i]) {
return false;
}
}
return true;
}
Another approach which only create several small objects in heap, if you are not dealing with primitive-type-array, is:
如果你不处理原始类型数组,另一种只在堆中创建几个小对象的方法是:
Foo[] arr1 = ....;
Foo[] arr2 = ....;
if (Arrays.asList(arr1).subList(0,100).equals(
Arrays.asList(arr2).subList(100,200)) {
....
}
This relies on the property that:
这取决于以下属性:
-
Arrays.asList()
returns aList
impl which internally backed-up by the input array -
subList()
returns a view of the original list, not a copy
Arrays.asList()返回一个由输入数组内部备份的List impl
subList()返回原始列表的视图,而不是副本