Java 重构下面的代码,以利用非常小的空间,即使对于大型数据也是如此

Java 重构下面的代码,以利用非常小的空间,即使对于大型数据也是如此,java,algorithm,Java,Algorithm,“一列火车有WagoCount,货车索引为0,1,…WagoCount-1。必须使用FillWang函数将每辆货车填入火车的构造器中。该函数接受货车的索引并返回货车的货物。下面的代码起作用,但是服务器只有小火车有足够的内存。请修改代码,这样服务器就有足够的内存,即使是大火车也可以 “认为我们可以将哈希表集合转换为数组,但不知道如何开始,请提供帮助。或者任何想法都会有很大帮助。" import java.util.function.function; 公车{ 私人哈希表货车; 公共列车(内部货车车

“一列火车有WagoCount,货车索引为0,1,…WagoCount-1。必须使用FillWang函数将每辆货车填入火车的构造器中。该函数接受货车的索引并返回货车的货物。下面的代码起作用,但是服务器只有小火车有足够的内存。请修改代码,这样服务器就有足够的内存,即使是大火车也可以

“认为我们可以将哈希表集合转换为数组,但不知道如何开始,请提供帮助。或者任何想法都会有很大帮助。"

import java.util.function.function;
公车{
私人哈希表货车;
公共列车(内部货车车厢、功能货车){
this.wagons=新哈希表();
for(int i=0;iwagonIndex);
对于(int i=0;i<10;i++){
系统输出打印项次(“货车:+i+”,货物:+train.peek货车(i));
}
}
}

您可以使用
int[]
它消耗的内存更少。 它是保存整数的最佳结构。
Hashtable
具有复杂的结构和巨大的数字存储开销,甚至
Ineger[]
比int[]消耗更多的内存。因此,最好的结构是原语数组。请看一看好的解释

我们使用数组索引按所需位置访问元素,而不是
哈希表。get
它所需的cpu资源更少:

public class Train {
    private int[] wagons;

    public Train(int wagonCount, Function<Integer, Integer> fillWagon) {
        this.wagons = new int[wagonCount];

        for (int i = 0; i < wagonCount; i++) {
            this.wagons[i] = fillWagon.apply(i);
        }
    }

    public int peekWagon(int wagonIndex) {
        return this.wagons[wagonIndex];
    }

    public static void main(String[] args) {
        Train train = new Train(10, wagonIndex -> wagonIndex);

        for (int i = 0; i < 10; i++) {
            System.out.println("Wagon: " + i + ", cargo: " + train.peekWagon(i));
        }
    }
}
公车{
私人货车;
公共列车(内部货车车厢、功能货车){
this.wagons=新整数[wagoCount];
for(int i=0;iwagonIndex);
对于(int i=0;i<10;i++){
系统输出打印项次(“货车:+i+”,货物:+train.peek货车(i));
}
}
}

如果在构造函数执行过程中需要填充所有货车,那么当内存大小限制为某个小常量时,就无法在内存中存储任意数量的货车内容。当然,使用
int
数组所需的内存会比map少一点,但它仍然会随着时间线性增长输入大小

但是,如果允许延迟实际存储货车内容,那么您可以使用构造函数保留对回调函数的引用,并且仅在调用
peekwago
时调用它。您仍然可以使用可用于存储部分货车内容的少量内存,但只能用于最后查询的k wago这样,你将在内存中有定期查询的内容,但当特定的货车不在(或不再)内存中时,你将需要(再次)检索内容。然后你将再次调用回调函数

当然,这假设回调函数不会有不希望的副作用,并且当传递相同的参数时,它总是返回相同的值

如果这些假设是正确的,那么您的代码可能如下所示:

import java.util.*;
import java.util.function.Function;

public class Main {
    static int maxSize = 4;
    private LinkedHashMap<Integer, Integer> wagons;
    private Function<Integer, Integer> fillWagon;

    public Main(int wagonCount, Function<Integer, Integer> fillWagon) {
        this.wagons = new LinkedHashMap<Integer, Integer>();
        this.fillWagon = fillWagon;
    }

    public int peekWagon(int wagonIndex) {
        int content;
        if (!this.wagons.containsKey(wagonIndex)) {
            if (this.wagons.size() >= maxSize) {
                // Make room by removing an entry
                int key = this.wagons.entrySet().iterator().next().getKey();  
                this.wagons.remove(key);
            }
            content = this.fillWagon.apply(wagonIndex);
        } else {
            // Remove entry so to put it at end of LinkedHashMap
            content = this.wagons.get(wagonIndex);
            this.wagons.remove(wagonIndex);
        }
        this.wagons.put(wagonIndex, content);
        return content;
    }

    /* ... */
}
import java.util.*;
导入java.util.function.function;
公共班机{
静态int maxSize=4;
私家马车;
私家车;
公用干管(int wagonCount,函数FILLWANG){
this.wagons=newlinkedhashmap();
this.fillWagon=fillWagon;
}
公共内部货车(内部货车索引){
智力内容;
如果(!this.wagons.containsKey(wagonIndex)){
if(this.wagons.size()>=maxSize){
//通过移除入口来腾出空间
int key=this.wagons.entrySet().iterator().next().getKey();
这个。货车。移除(钥匙);
}
content=this.fillWagon.apply(wagonIndex);
}否则{
//删除条目,以便将其放在LinkedHashMap的末尾
content=this.wagons.get(wagonIndex);
此.wagons.remove(wagonIndex);
}
this.wagons.put(wagonIndex,content);
返回内容;
}
/* ... */
}

谢谢@i.bondarenko。这个门槛是否满足问题中的给定条件,即“修改代码,使服务器即使对于大型列车也有足够的内存”。如果是的话,如果您对解决方案稍加解释,我会很高兴。
import java.util.*;
import java.util.function.Function;

public class Main {
    static int maxSize = 4;
    private LinkedHashMap<Integer, Integer> wagons;
    private Function<Integer, Integer> fillWagon;

    public Main(int wagonCount, Function<Integer, Integer> fillWagon) {
        this.wagons = new LinkedHashMap<Integer, Integer>();
        this.fillWagon = fillWagon;
    }

    public int peekWagon(int wagonIndex) {
        int content;
        if (!this.wagons.containsKey(wagonIndex)) {
            if (this.wagons.size() >= maxSize) {
                // Make room by removing an entry
                int key = this.wagons.entrySet().iterator().next().getKey();  
                this.wagons.remove(key);
            }
            content = this.fillWagon.apply(wagonIndex);
        } else {
            // Remove entry so to put it at end of LinkedHashMap
            content = this.wagons.get(wagonIndex);
            this.wagons.remove(wagonIndex);
        }
        this.wagons.put(wagonIndex, content);
        return content;
    }

    /* ... */
}