在Java中将Sorted Set转换为Range语句 [英] Translate Sorted Set to a Range Statement in Java

查看:89
本文介绍了在Java中将Sorted Set转换为Range语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个排序集,元素为[1,2,3,4,5,10,12,45,46,47,50]

I have a Sorted Set with elements as [1,2,3,4,5,10,12,45,46,47,50]

I需要将此集转换为Range语句,其中连续元素用min .. max表示。
这里准确的是预期产量。

I need to convert this set as a Range Statement where continuous elements are represented with min .. max. To be precise here is the expected output.

1..5 / 10/12 / 45..47 / 50

1..5/10/12/45..47/50

由于1到5是连续的,所以它们用1..5表示,因此45..47

Since 1 to 5 are continuous they are denoted by 1..5 and so as 45..47

10,12等。是不连续的,因此它们被Union分开。

10,12 etc.. are discontinuous and hence they are separated by Union.

任何人都可以帮助我任何内置方法或高效算法来实现这一目标吗?
目前我正在使用迭代器而不是中间。

Can anyone help me any inbuilt method or efficient Algorithm to achieve this? Currently I am using iterators over set but stuck in middle.

String SetToRange(Set<Integer> S, long maxint)
    {
        Integer min,max;
        StringBuilder Range=new StringBuilder("");
        Iterator I=S.iterator();
        switch(S.size())
        {
            case 0: Range.append("0.."+maxint);
                    System.out.println("RangeStatement for Set Size 0 is"+Range);
                    return Range.toString();
            case 1: min=max=(Integer)I.next();
                    Range.append(min);
                    System.out.println("RangeStatement="+Range);    
                    return Range.toString();
            case 2: min=(Integer)I.next();
                    max=(Integer)I.next();
                    if(max==min+1)
                    {
                        Range.append(min.toString()+".."+max.toString());
                    }
                    else
                    {
                        Range.append(min+"\\/"+max);
                    }
                    System.out.println("Range Statement:-"+Range);
                    return Range.toString();
        }
         System.out.println("The Set has more than Two Elements="+S.size());
         min=(Integer)I.next();
         max=(Integer)I.next();
         //Working out logic for this Part using Two Iterators
         return Range.toString();

    }

谢谢,

推荐答案

您可以在 O(n)时间内完成此操作。此示例使用面向对象方法显示它。

You could do this in O(n) time. This example shows it with an oriented-object approach.

首先创建一个 Range 类。

class Range {
    int from;
    int to;

    public Range setFrom(int from) {
        this.from = from;
        return this;
    }

    public Range setTo(int to) {
        this.to = to;
        return this;
    }

    @Override
    public String toString() {
        return "Range [from=" + from + ", to=" + to + "]";
    }   
}

现在只需迭代数组并比较相邻元素。

Now just iterate through the array and compare the adjacent elements.

如果它们的差异优于1,则它们不是连续的,因此您设置上一个范围的上限,将其添加到列表中并创建一个新设置它的下限。

If their difference is superior than 1, they are not contiguous so you set the upperbound of the previous range, you add it to the list and you create a new one setting its lower bound.

public class Test { 

    public static void main(String[] args){
        Set<Integer> setOfIntegers = new LinkedHashSet<>(Arrays.asList(1,2,3,4,5,10,12,45,46,47,49));
        System.out.println(getRanges(setOfIntegers));
    }

    public static List<Range> getRanges(Set<Integer> s){
        List<Range> list = new ArrayList<>();
        Integer[] setOfIntegers = s.toArray(new Integer[s.size()]);

        Range r = new Range().setFrom(setOfIntegers[0]);
        for(int i = 1; i < setOfIntegers.length; i++){
             if(setOfIntegers[i] - setOfIntegers[i-1] != 1){
                 list.add(r.setTo(setOfIntegers[i-1]));
                 r = new Range().setFrom(setOfIntegers[i]);
             }
        }
        list.add(r.setTo(setOfIntegers[setOfIntegers.length-1]));

        return list;
    }
}

一些输出:

[1,2,3,4,5,10,12,45,46,47,49] => [Range [from=1, to=5], Range [from=10, to=10], Range [from=12, to=12], Range [from=45, to=47], Range [from=49, to=49]]

[4,7,8,9,15,20,21] => [Range [from=4, to=4], Range [from=7, to=9], Range [from=15, to=15], Range [from=20, to=21]]

[-17,-6,-4,-3,-2,0,1,4] => [Range [from=-17, to=-17], Range [from=-6, to=-6], Range [from=-4, to=-2], Range [from=0, to=1], Range [from=4, to=4]]

请注意,我没有检查是否set没有元素等等。但是你有一般的想法,根据你的需要修改它不应该那么难。

Note that I didn't check if the set has no elements, etc. But you have the general idea and it should not be that hard to modify it as your needs.

这篇关于在Java中将Sorted Set转换为Range语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆