javaWorld
javaWorld

Reputation: 9

Is there a way to return a set of objects combined as one object?

So I have a list of objects, these objects are intervals, for ex [2-10]. The left end is always less than the right end, (start < end).

Now say I calculate the union of intervals, like [2-9] and [10-12], I want to return [2-9,10-12] as a single object.

Is there any way to return that instance of intervals with just using a list of interval objects?

Also, the non number characters are built from my toString method, not be be confused as part of the object itself.

Upvotes: 0

Views: 107

Answers (2)

Charles
Charles

Reputation: 954

I suggest using a new structure for your IntervalUnion class that can handle multiple start and end points.

WARNINGS: This unions implementation is not correct. This implementation empties the two intervals you are union. If [1,4] and [2,6] is unioned you will not get the expected result [1,6]

public class Interval {

    List<Integer> starts = new ArrayList<>();
    List<Integer> ends = new ArrayList<>();

    public Interval() {
    }

    public Interval(int start, int end) {
        starts.add(start);
        ends.add(end);
    }

    public Interval union(Interval interval) {
        Interval result = new Interval();
        while(this.starts.size()>0||interval.starts.size()>0){
            if(!this.starts.isEmpty() && this.starts.get(0) <= interval.starts.get(0)) {
                result.starts.add(this.starts.get(0));
                result.ends.add(this.ends.get(0));
                this.starts.remove(0);
                this.ends.remove(0);
            } else {
                result.starts.add(interval.starts.get(0));
                result.ends.add(interval.ends.get(0));
                interval.starts.remove(0);
                interval.ends.remove(0);
            }
        }
        return result;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for(int i = 0; i < starts.size();i++){
            sb.append(starts.get(i));
            sb.append("-");
            sb.append(ends.get(i));
            if(i<starts.size()-1) {
                sb.append(",");
            }
        }
        sb.append("]");
        return sb.toString();
    }

    public static void main(String... args) {
        Interval i = new Interval(1, 2);
        Interval j = new Interval(4, 5);
        Interval k = new Interval(7, 9);

        System.out.println(i.union(j).union(k));
    }
}

With output: [1-2,4-5,7-9]

Upvotes: 0

Antoniossss
Antoniossss

Reputation: 32527

Once you will get the List of your IntervalUnions, you will be able to print it as required using already implemented toString method. Printing list will be like

public static void main(String... args) throws InterruptedException {

    List<MyInterval> myList = new ArrayList<>();
    myList.add(new MyInterval(5, 10));
    myList.add(new MyInterval(20, 30));
    System.out.println(myList.toString());
}

static class MyInterval {
    int x, y;
    MyInterval(int x, int y) {
        this.x = x;
        this.y = y;
    }
    @Override
    public String toString() {
        return x + "-" + y;
    }
}

Which prints

[5-10, 20-30]

So that is exactly as required. Now swap MyInterval with collection of combined interval unions and you are done.

Upvotes: 1

Related Questions