生成查找表

huangapple go评论68阅读模式
英文:

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 = &quot;2001&quot;;
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&lt;Range, String&gt; 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&lt;Range, String&gt;();
}
map.put(new Range(0, 999), &quot;/mnt/partition0/&quot;);
map.put(new Range(1000, 1999), &quot;/mnt/partition1/&quot;);
map.put(new Range(2000, 2999), &quot;/mnt/partition2/&quot;);
map.put(new Range(3000, 3999), &quot;/mnt/partition3/&quot;);
map.put(new Range(4000, 4999), &quot;/mnt/partition4/&quot;);
map.put(new Range(5000, 5999), &quot;/mnt/partition4/&quot;);
map.put(new Range(6000, 6999), &quot;/mnt/partition6/&quot;);
map.put(new Range(7000, 7999), &quot;/mnt/partition7/&quot;);
String idPart = appId.split(&quot;-&quot;)[1]; // return an integer value
Integer id = Integer.valueOf(idPart);
AtomicReference&lt;String&gt; thePartition = new AtomicReference&lt;&gt;();
map.entrySet().forEach(rangeStringEntry -&gt; {
String partition = rangeStringEntry.getValue();
Range range = rangeStringEntry.getKey();
if(range.upper &gt;= id &amp;&amp; range.lower &lt;= id) {
thePartition.set(partition);
}
});
return thePartition.get();
}
}
public class Range implements Comparable&lt;Range&gt; {
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 &amp;&amp; range.upper == this.upper) {
return 0;
}
return -1;
}
}

huangapple
  • 本文由 发表于 2020年10月6日 19:28:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64224857.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定