英文:
Generating a Lookup Table
问题
// Java代码部分
// 你的lookup.properties文件可以使用Properties类来读取和解析
Properties properties = new Properties();
try {
InputStream inputStream = getClass().getResourceAsStream("lookup.properties");
properties.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
// 编写一个方法来根据id获取对应的分区路径
public String getPartitionPathById(String id) {
int numericId = Integer.parseInt(id);
for (String key : properties.stringPropertyNames()) {
String[] rangeAndPath = properties.getProperty(key).split("=");
String[] range = rangeAndPath[0].substring(1, rangeAndPath[0].length() - 1).split(",");
int startRange = Integer.parseInt(range[0]);
int endRange = Integer.parseInt(range[1]);
if (numericId >= startRange && numericId <= endRange) {
return rangeAndPath[1];
}
}
return null; // 返回null表示没有找到匹配的分区路径
}
以上是用Java实现根据lookup.properties中的范围查找对应分区路径的方法。如果还有其他问题或需要进一步帮助,请随时提问。
英文:
What's the best way to represent a "lookup" table in Java using properties file:
Say I have this lookup.properties file with: (although I think using a properties file is very wrong for this case)
(0,999)=/mnt/partition0/
(1000,1999)=/mnt/partition1/
(2000,2999)=/mnt/partition2/
(3000,3999)=/mnt/partition3/
(4000,4999)=/mnt/partition4/
(5000,5999)=/mnt/partition5/
(6000,6999)=/mnt/partition6/
(7000,7999)=/mnt/partition7/
And a method
String id = "2001";
String partition = getPartitionPathById(id);
// returns /mnt/partition2/
答案1
得分: 0
以下是翻译好的内容:
这是一个解决方案:
public class PartitionImpl implements Partition {
NavigableMap<Range, String> map;
public PartitionImpl() {
}
@Override public String getPartition(Application application) {
return getPartition(application.getAppId());
}
@Override public String getPartition(String appId) {
if(map == null) {
map = new TreeMap<Range, String>();
}
map.put(new Range(0, 999), "/mnt/partition0/");
map.put(new Range(1000, 1999), "/mnt/partition1/");
map.put(new Range(2000, 2999), "/mnt/partition2/");
map.put(new Range(3000, 3999), "/mnt/partition3/");
map.put(new Range(4000, 4999), "/mnt/partition4/");
map.put(new Range(5000, 5999), "/mnt/partition4/");
map.put(new Range(6000, 6999), "/mnt/partition6/");
map.put(new Range(7000, 7999), "/mnt/partition7/");
String idPart = appId.split("-")[1]; // 返回一个整数值
Integer id = Integer.valueOf(idPart);
AtomicReference<String> thePartition = new AtomicReference<>();
map.entrySet().forEach(rangeStringEntry -> {
String partition = rangeStringEntry.getValue();
Range range = rangeStringEntry.getKey();
if(range.upper >= id && range.lower <= id) {
thePartition.set(partition);
}
});
return thePartition.get();
}
}
public class Range implements Comparable<Range> {
public int lower, upper;
public int value;
public Range(int lower, int upper) {
this.lower = lower;
this.upper = upper;
}
@Override public int compareTo(@NotNull Range range) {
if (range.lower == this.lower && range.upper == this.upper) {
return 0;
}
return -1;
}
}
英文:
Here's a solution:
public class PartitionImpl implements Partition {
NavigableMap<Range, String> map;
public PartitionImpl() {
}
@Override public String getPartition(Application application) {
return getPartition(application.getAppId());
}
@Override public String getPartition(String appId) {
if(map == null) {
map = new TreeMap<Range, String>();
}
map.put(new Range(0, 999), "/mnt/partition0/");
map.put(new Range(1000, 1999), "/mnt/partition1/");
map.put(new Range(2000, 2999), "/mnt/partition2/");
map.put(new Range(3000, 3999), "/mnt/partition3/");
map.put(new Range(4000, 4999), "/mnt/partition4/");
map.put(new Range(5000, 5999), "/mnt/partition4/");
map.put(new Range(6000, 6999), "/mnt/partition6/");
map.put(new Range(7000, 7999), "/mnt/partition7/");
String idPart = appId.split("-")[1]; // return an integer value
Integer id = Integer.valueOf(idPart);
AtomicReference<String> thePartition = new AtomicReference<>();
map.entrySet().forEach(rangeStringEntry -> {
String partition = rangeStringEntry.getValue();
Range range = rangeStringEntry.getKey();
if(range.upper >= id && range.lower <= id) {
thePartition.set(partition);
}
});
return thePartition.get();
}
}
public class Range implements Comparable<Range> {
public int lower, upper;
public int value;
public Range(int lower, int upper) {
this.lower = lower;
this.upper = upper;
}
@Override public int compareTo(@NotNull Range range) {
if (range.lower == this.lower && range.upper == this.upper) {
return 0;
}
return -1;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论