生成查找表

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

Generating a Lookup Table

问题

  1. // Java代码部分
  2. // 你的lookup.properties文件可以使用Properties类来读取和解析
  3. Properties properties = new Properties();
  4. try {
  5. InputStream inputStream = getClass().getResourceAsStream("lookup.properties");
  6. properties.load(inputStream);
  7. } catch (IOException e) {
  8. e.printStackTrace();
  9. }
  10. // 编写一个方法来根据id获取对应的分区路径
  11. public String getPartitionPathById(String id) {
  12. int numericId = Integer.parseInt(id);
  13. for (String key : properties.stringPropertyNames()) {
  14. String[] rangeAndPath = properties.getProperty(key).split("=");
  15. String[] range = rangeAndPath[0].substring(1, rangeAndPath[0].length() - 1).split(",");
  16. int startRange = Integer.parseInt(range[0]);
  17. int endRange = Integer.parseInt(range[1]);
  18. if (numericId >= startRange && numericId <= endRange) {
  19. return rangeAndPath[1];
  20. }
  21. }
  22. return null; // 返回null表示没有找到匹配的分区路径
  23. }

以上是用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)

  1. (0,999)=/mnt/partition0/
  2. (1000,1999)=/mnt/partition1/
  3. (2000,2999)=/mnt/partition2/
  4. (3000,3999)=/mnt/partition3/
  5. (4000,4999)=/mnt/partition4/
  6. (5000,5999)=/mnt/partition5/
  7. (6000,6999)=/mnt/partition6/
  8. (7000,7999)=/mnt/partition7/

And a method

  1. String id = &quot;2001&quot;;
  2. String partition = getPartitionPathById(id);
  3. // returns /mnt/partition2/

答案1

得分: 0

以下是翻译好的内容:

  1. 这是一个解决方案
  2. public class PartitionImpl implements Partition {
  3. NavigableMap<Range, String> map;
  4. public PartitionImpl() {
  5. }
  6. @Override public String getPartition(Application application) {
  7. return getPartition(application.getAppId());
  8. }
  9. @Override public String getPartition(String appId) {
  10. if(map == null) {
  11. map = new TreeMap<Range, String>();
  12. }
  13. map.put(new Range(0, 999), "/mnt/partition0/");
  14. map.put(new Range(1000, 1999), "/mnt/partition1/");
  15. map.put(new Range(2000, 2999), "/mnt/partition2/");
  16. map.put(new Range(3000, 3999), "/mnt/partition3/");
  17. map.put(new Range(4000, 4999), "/mnt/partition4/");
  18. map.put(new Range(5000, 5999), "/mnt/partition4/");
  19. map.put(new Range(6000, 6999), "/mnt/partition6/");
  20. map.put(new Range(7000, 7999), "/mnt/partition7/");
  21. String idPart = appId.split("-")[1]; // 返回一个整数值
  22. Integer id = Integer.valueOf(idPart);
  23. AtomicReference<String> thePartition = new AtomicReference<>();
  24. map.entrySet().forEach(rangeStringEntry -> {
  25. String partition = rangeStringEntry.getValue();
  26. Range range = rangeStringEntry.getKey();
  27. if(range.upper >= id && range.lower <= id) {
  28. thePartition.set(partition);
  29. }
  30. });
  31. return thePartition.get();
  32. }
  33. }
  34. public class Range implements Comparable<Range> {
  35. public int lower, upper;
  36. public int value;
  37. public Range(int lower, int upper) {
  38. this.lower = lower;
  39. this.upper = upper;
  40. }
  41. @Override public int compareTo(@NotNull Range range) {
  42. if (range.lower == this.lower && range.upper == this.upper) {
  43. return 0;
  44. }
  45. return -1;
  46. }
  47. }
英文:

Here's a solution:

  1. public class PartitionImpl implements Partition {
  2. NavigableMap&lt;Range, String&gt; map;
  3. public PartitionImpl() {
  4. }
  5. @Override public String getPartition(Application application) {
  6. return getPartition(application.getAppId());
  7. }
  8. @Override public String getPartition(String appId) {
  9. if(map == null) {
  10. map = new TreeMap&lt;Range, String&gt;();
  11. }
  12. map.put(new Range(0, 999), &quot;/mnt/partition0/&quot;);
  13. map.put(new Range(1000, 1999), &quot;/mnt/partition1/&quot;);
  14. map.put(new Range(2000, 2999), &quot;/mnt/partition2/&quot;);
  15. map.put(new Range(3000, 3999), &quot;/mnt/partition3/&quot;);
  16. map.put(new Range(4000, 4999), &quot;/mnt/partition4/&quot;);
  17. map.put(new Range(5000, 5999), &quot;/mnt/partition4/&quot;);
  18. map.put(new Range(6000, 6999), &quot;/mnt/partition6/&quot;);
  19. map.put(new Range(7000, 7999), &quot;/mnt/partition7/&quot;);
  20. String idPart = appId.split(&quot;-&quot;)[1]; // return an integer value
  21. Integer id = Integer.valueOf(idPart);
  22. AtomicReference&lt;String&gt; thePartition = new AtomicReference&lt;&gt;();
  23. map.entrySet().forEach(rangeStringEntry -&gt; {
  24. String partition = rangeStringEntry.getValue();
  25. Range range = rangeStringEntry.getKey();
  26. if(range.upper &gt;= id &amp;&amp; range.lower &lt;= id) {
  27. thePartition.set(partition);
  28. }
  29. });
  30. return thePartition.get();
  31. }
  32. }
  1. public class Range implements Comparable&lt;Range&gt; {
  2. public int lower, upper;
  3. public int value;
  4. public Range(int lower, int upper) {
  5. this.lower = lower;
  6. this.upper = upper;
  7. }
  8. @Override public int compareTo(@NotNull Range range) {
  9. if (range.lower == this.lower &amp;&amp; range.upper == this.upper) {
  10. return 0;
  11. }
  12. return -1;
  13. }
  14. }

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:

确定