0
  • it should not include repeated points; that is, points with the same coordinates.
  • and it should not include points with negative coordinates.

This is what I have got so far, but I'm struggling to do the same with y coordinate.

static List<Point> ex5(List<Integer>xs, List<Integer> ys){
    List<Point> p = xs.stream()
            .map(e -> new Point(e , 0))
            .collect(Collectors.toList());
    return p ;
}

Below is the sample data. Any idea what I am missing here?

List<Integer> pointx = new ArrayList<>();
pointx.add(1);
pointx.add(-2);
pointx.add(3);
pointx.add(4);
pointx.add(1);

List<Integer> pointy = new ArrayList<>();
pointy.add(6);
pointy.add(7);
pointy.add(8);
pointy.add(9);
pointy.add(6);
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
lool
  • 21
  • 3
  • could you please add the expected output in your question. It is actually not clear what you meant by ***struggling to do the "same" with y coordinate*** – Gautham M Jun 05 '21 at 15:46
  • Try looking for the **approach** amongst the duplicate links and then try to build your exact solution. – Naman Jun 05 '21 at 16:44

2 Answers2

0

Start out from an IntStream of indices into the two original lists. This will allow you to process pairs of numbers from each list.

static List<Point> ex5(List<Integer>xs, List<Integer> ys){
    if (xs.size() != ys.size()) {
        throw new IllegalArgumentException("Must have smae size");
    }
    List<Point> p = IntStream.range(0, xs.size())
            .filter(index -> xs.get(index) >= 0 && ys.get(index) >= 0)
            .mapToObj(index -> new Point(xs.get(index), ys.get(index)))
            .distinct()
            .collect(Collectors.toList());
    return p ;
}

The distinct operation removes duplicates. Also duplicates that don’t come right after each other, I am not sure whether you wanted that.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Do you really want to call `xs.get(..)` twice? – Nikolas Charalambidis Jun 05 '21 at 15:06
  • @NikolasCharalambidis Not really. This went a little fast, and I encourage anyone using my code to make any adjustments they like. You may swap filtering and creation of points objects. Then you may even fit the `Point` class with a `hasNonnegativeCoords` method for filtering. – Ole V.V. Jun 05 '21 at 15:41
  • thnx for help :) – lool Jun 11 '21 at 09:24
0

it should not include repeated points; that is, points with the same coordinates.

Implement equals and hashCode correctly. Use the Stream#distinct() method or Set instead of List.

and it should not include points with negative coordinates.

Use Stream#filter(Predicate) to filter out unwanted values.


static List<Point> ex5(List<Integer> xs, List<Integer> ys){
    Set<Point> set = new HashSet<>();
    int min = Math.min(xs.size(), ys.size());
    for (int i=0; i<min; i++){
        int x = xs.get(i);
        int y = ys.get(i);
        if (x >=0 && y>=0) {
            set.add(new Point(x, y));
        }
    }
    return new ArrayList<>(set);
}

Few notes:

  • I used Math#min(int, int) to avoid IndexOutOfBoundsException. You might want to handle the case the list sizes are not equal in a better way.

  • It is possible to use using IntStream#ange(int, int) but it brings no real benefit. All it might appear more readable for some developers (a matter of personal preferences).

    int min = Math.min(xs.size(), ys.size());
    Set<Point> set = IntStream.range(0, min)
            .mapToObj(i -> new Point(xs.get(i), ys.get(i)))
            .filter(point-> point.getX()>=0 && point.getY()>=0)
            .collect(Collectors.toSet());
    return new ArrayList<>(set);
    
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183