给定一个List
实例,将List
的大小增加f
的最有效方法是什么,以便新元素与原始元素重复,与原始数组交错?
e.g。
f = 2
Original = [a,b,c,...,x,y,z]
New = [a,a,b,b,c,c,...,x,x,y,y,z,z]
我目前的实施是:
List< Foo > interleave( List< Foo > original, int f ) {
int newSize = original.size() * f;
List< Foo > interleaved = new ArrayList< Foo >( newSize );
for( Foo foo : original ) {
for( int j = 0; j < factor; j++ ) {
interleaved.add( new Foo( foo ) );
}
}
}
问题是我的原始列表可能非常大,因此性能不是很好。我有一种预感,有一种更有效的方法可以做到这一点;有人有什么建议吗?
答案 0 :(得分:1)
这可以很好地完成,而不需要完全重复。
List< String > interleave(final List< String > original, final int f ) {
final int size = f * original.size();
final List<String> originalCopy = new ArrayList<String>();
for (String each : original) {
originalCopy.add(new String(each)); // <=== duplicate here.
}
return new AbstractList<String>() {
@Override
public String get(int index) {
return originalCopy.get(index / f);
}
@Override
public int size() {
return size;
}
};
}
测试
System.out.println(interleave(Arrays.asList("a", "b", "c"), 2));
System.out.println(interleave(Arrays.asList("x", "y"), 3));
输出
[a, a, b, b, c, c]
[x, x, x, y, y, y]
答案 1 :(得分:1)
您提供的代码已经过优化,但可以进行一些改进;重要的是取决于您的确切需求。
首先,如果您的克隆元素将保留与原始元素相同的值,或者只是其中一些(与总数相比)将更改其值,您可能需要考虑参考 - 基于克隆而不是当前的“真正克隆一切”代码,如果不是一种甚至不创建新列表的完全不同的方法。
/**
* PROS:
* -Very low memory-footprint, as no new objects are created in memory, just references to a single (original) object.
* -Can be done with generalization; A single method will function for most classes and data-types, as is below.
*
* CONS:
* -If you need each clone element to be changed independently from eachother and/or the orininal, this will not work directly,
* because any change to an reference-element will apply to all other reference-elements that point to that same Object.
*
* @param <E> Sub-class generalizator. Used so that the returned list has the same sub-class as the source.
* @param list Source list. The list containing the elements to be interleaved.
* @param f The factor to interleave for. In effect, the number of resulting elements for each original.
* @return A list containing the interleaved elements, with each element being a REFERENCE to the original object.
*/
public static <E> List<E> interleaveByReference(List<E> list, int f) {
List<E> interleaved = new ArrayList<E>(list.size() * f);
for (E obj : list) {
for (int i = 0; i < f; i++) {
interleaved.add(obj);
}
}
return interleaved;
}
如果您只需要几个克隆来更改值,那么您的交错列表可能更好地基于参考,并且需要更改的元素将在以后单独替换。< / p>
但请注意,此方法的有效性将高度依赖于您需要更改原始列表元素的数量;如果太多需要改变,这种方法虽然在内存占用方面仍然更好,但在速度性能上会更差(这似乎是你主要考虑的因素)。
“个别克隆的后期”可以用类似的东西来实现:
public static void replaceWithTrueClone(List<String> list, int objIndex) {
list.add(objIndex, new String(list.get(objIndex)));
list.remove(objIndex + 1);
}
//OR
public static void replaceWithNewObject (List<String> list, int objIndex, String newObject) {
list.add(objIndex, newObject);
list.remove(objIndex + 1);
}
如果每个元素的大部分在程序执行过程中都有独立的值,那么你当前的方法已经非常准确了。
可以进行两项改进。直接在代码中显示它会更容易,这就是我要做的事情:
/**
* PROS:
* -Each element is an independent object, and can be set to independent values without much of an effort.
*
* CONS:
* -Each element has it's own allocated memory for it's values, thus having a much heavier memory footprint.
* -Is constructor-dependent, and thus cannot be generalized as easily;
* Each different expected class will probably need it's own method.
*
* @param list Source list. The list containing the elements to be interleaved.
* @param f The factor to interleave for. In effect, the number of resulting elements for each original.
* @return A list containing the interleaved elements.
* For each of the original elements, the first is a REFERENCE, and the other are CLONES.
*/
public static List<String> interleaveByClone(List<String> list, int f) {
List<String> interleaved = new ArrayList<String>(list.size() * f);
for (String obj : list) {
interleaved.add(obj); //The first element doesn't have to be cloned, I assume.
//If it has to be cloned, delete the line above, and change 'i=1' to 'i=0' on the line below.
for (int i = 1; i < f; i++) {
interleaved.add(new String(obj));
}
}
return interleaved;
}
/*
* What was changed from the original is commented below.
*/
public static List<String> original(List<String> original, int factor) {
/*
* It is unnessessary to have this 'newSize' variable. It gets needlessly maintained until the end of the method.
* Although the impact is unworthy of measurement (negligible), it still exists.
*/
int newSize = original.size() * factor;
List<String> interleaved = new ArrayList<String>(newSize); //Just do the '*factor' operarion directly, instead of 'newSize'.
for (String foo : original) {
/*
* If you can use the original here, that's one less cloning operation (memory-allocation, etc...) per original element.
* A low-impact optimization, but still a good one.
*/
for (int j = 0; j < factor; j++) {
interleaved.add(new String(foo));
}
}
return interleaved;
}
原始列表中有两百万个元素,系数为2,我得到以下平均速度超过10次:
interleaveByReference()
方法。interleaveByClone()
方法。original()
方法。答案 2 :(得分:0)
可以通过重新插入原始元素并仅创建重复项来实现次要加速:
List< Foo > interleave( List< Foo > original, int factor ) {
int newSize = original.size() * factor ;
List< Foo > interleaved = new ArrayList< Foo >( newSize );
for( Foo foo : original ) {
interleaved.add( foo );
for( int j = 1; j < factor; j++ ) {
interleaved.add( new Foo( foo ) );
}
}
return interleaved;
}