Convert List to Range in Map

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

Сonvert List<integer> to Range<integer> in Map

问题

我有一个包含以下值的 Map:

map.of.list={\
  {'2','4'} : '1', \
  {'4','6'} : '2', \
  {'6','8'} : '3' \
 }

@Value("#{${map.of.list}}")
private Map<List<Integer>, Integer> integerMap;

我如何将其转换为一个 Map<Range<Integer>, Integer> rangesMap

Range 的类型为 org.springframework.data.domain.Range

英文:

I have a Map with the following values:

map.of.list={\
  {&#39;2&#39;,&#39;4&#39;} : &#39;1&#39;, \
  {&#39;4&#39;,&#39;6&#39;} : &#39;2&#39;, \
  {&#39;6&#39;,&#39;8&#39;} : &#39;3&#39; \
 }

@Value(&quot;#{${map.of.list}}&quot;)
private Map&lt;List&lt;Integer&gt;, Integer&gt; integerMap;

How can I convert this to a Map&lt;Range&lt;Integer&gt;, Integer&gt; rangesMap?

Type for Range is org.springframework.data.domain.Range.

答案1

得分: 1

使用这个设置:

// 示例输入
Map<List, Integer> input = new HashMap<>();
input.put(List.of(1, 3), 5);
input.put(List.of(4, 7), 8);

// 用于保存结果的映射
Map<Range, Integer> result = new HashMap<>();

你可以使用以下方法来实现...

传统的 for 循环

// 用于转换的代码
for (Map.Entry<List, Integer> entry : input.entrySet()) {

// 将列表键转换为范围
List<Integer> key = entry.getKey();
int from = key.get(0);
int to = key.get(1);
Range range = Range.rightOpen(from, to);

Integer value = entry.getValue();

result.put(range, value);

}

使用 entrySettoMap

Map<Range, Integer> result = input.entrySet()
.stream()
.collect(Collectors.toMap(
e -> Range.rightOpen(e.getKey().get(0), e.getKey().get(1)),
Map.Entry::getValue
));

使用 forEach,如 misnormer42 建议的方法

Map<Range, Integer> result = new HashMap<>();
input.forEach((keys, values) -> {
Range integerRange = Range.rightOpen(keys.get(0), keys.get(1));
ranges.put(integerRange, values);
});
return result;

英文:

With this setup:

// Sample input
Map&lt;List&lt;Integer&gt;, Integer&gt; input = new HashMap&lt;&gt;();
input.put(List.of(1, 3), 5);
input.put(List.of(4, 7), 8);

// Map to hold the result
Map&lt;Range&lt;Integer&gt;, Integer&gt; result = new HashMap&lt;&gt;();

You can do it using...

Traditional for loop

// Code to convert
for (Map.Entry&lt;List&lt;Integer&gt;, Integer&gt; entry : input.entrySet()) {

    // Convert list key to Range
    List&lt;Integer&gt; key = entry.getKey();
    int from = key.get(0);
    int to = key.get(1);
    Range range = Range.rightOpen(from, to);

    Integer value = entry.getValue();
    
    result.put(range, value);
}

Using entrySet and toMap

Map&lt;Range&lt;Integer&gt;, Integer&gt; result = input.entrySet()
        .stream()
        .collect(Collectors.toMap(
                e -&gt; Range.rightOpen(e.getKey().get(0), e.getKey().get(1)),
                Map.Entry::getValue
        ));

Using forEach as suggested by misnormer42

Map&lt;Range&lt;Integer&gt;, Integer&gt; result = new HashMap&lt;&gt;();
input.forEach((keys, values) -&gt; {
    Range&lt;Integer&gt; integerRange = Range.rightOpen(keys.get(0), keys.get(1));
    ranges.put(integerRange, values);
});
return result;

答案2

得分: 1

我可以建议以下方法来解决这个问题:

public final class ConversionUtils {

    private ConversionUtils() {
    }

    public static Map<Range<Integer>, Integer> convertTo(Map<List<Integer>, Integer> fromProperties) {
        Map<Range<Integer>, Integer> result = new HashMap<>();
        fromProperties.forEach((keys, values) -> {
            Range<Integer> integerRange = Range.rightOpen(keys.get(0), keys.get(1));
            result.put(integerRange, values);
        });
        return result;
    }
}
英文:

I can suggest to solve this problem in the following way:

public final class ConversionUtils {

    private ConversionUtils() {
    }

    public static Map&lt;Range&lt;Integer&gt;, Integer&gt; convertTo(Map&lt;List&lt;Integer&gt;, Integer&gt; fromProperties) {
        Map&lt;Range&lt;Integer&gt;, Integer&gt; result= new HashMap&lt;&gt;();
        fromProperties.forEach((keys, values) -&gt; {
            Range&lt;Integer&gt; integerRange = Range.rightOpen(keys.get(0), keys.get(1));
            ranges.put(integerRange, values);
        });
        return result;
    }
}

huangapple
  • 本文由 发表于 2020年9月8日 16:44:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63790320.html
匿名

发表评论

匿名网友

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

确定