How can I write a Comparator comparing multiple arguments? [duplicate] - java

This question already has answers here:
How to compare objects by multiple fields
(23 answers)
Closed 4 years ago.
Im trying to write a Comparator which compares two objects of the class Coordinate. The Coordinate class is Pretty simple:
public class Coordinate {
private int x, y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
Now I want the Comparator to compare the x and the y value for two instances of the class Coordinate. Here is an example:
I have a Coordinate c1 which has x = 42 and y = 23. My second Coordinate c2 has x = 23 and y = 54. Now i put them both in an ArrayList an want to sort the List. I wanted to be sorted like the following:
The Coordinate with the lowest y value goes Always first, when you have two Coordinates having the same y value the Coordinate goes first which has a lower x value.
Example:
c1 (y = 4, x = 5 ) < c2 (y = 4, x = 6) < c3 (y = 5, x = 2)
So how can I write a Comparator for this Purpose?
Thank you very much!

Comparator<Coordinate> c = Comparator.comparingInt(Coordinate::getY)
.thenComparingInt(Coordinate::getX);
You can build compound comparators by means of thenComparing and thenComparingX.
var list = List.of(
new Coordinate(6, 4),
new Coordinate(2, 5),
new Coordinate(5, 4)
);
list.sort(c);
System.out.println(list);
The snippet prints
[{y=4, x=5}, {y=4, x=6}, {y=5, x=2}]

Using Comparator
import java.util.ArrayList;
import java.util.Comparator;
class Coordinate {
private int x, y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public String toString() {
return "x = " + x + " y = " + y;
}
}
public class Temp {
public static void main(String[] args) {
ArrayList<Coordinate> A = new ArrayList<>();
A.add(new Coordinate(1, 2));
A.add(new Coordinate(2, 1));
A.add(new Coordinate(3, 2));
A.sort(new Comparator<Coordinate>() {
#Override
public int compare(Coordinate o1, Coordinate o2) {
if (o1.getY() < o2.getY()) {
return -1;
} else if (o1.getY() > o2.getY()) {
return 1;
} else {
if (o1.getX() < o2.getX()) {
return -1;
} else if (o1.getX() > o2.getX()) {
return 1;
}
return 0;
}
}
});
System.out.println(A.toString());
}
}
Using Comparable Interface
import java.util.ArrayList;
class Coordinate implements Comparable<Coordinate> { # Notice implementing Comparable interface
private int x, y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
#Override
public int compareTo(Coordinate o) { # implementing the abstract method of Comparable interface
if (y < o.y) {
return -1;
} else if (y > o.y) {
return 1;
} else {
if (x < o.x) {
return -1;
} else if (x > o.x) {
return 1;
}
return 0;
}
}
public String toString() {
return "x = " + x + " y = " + y;
}
}
public class Temp {
public static void main(String[] args) {
ArrayList<Coordinate> A = new ArrayList<>();
A.add(new Coordinate(1, 2));
A.add(new Coordinate(2, 1));
A.add(new Coordinate(3, 2));
A.sort(null);
System.out.println(A.toString());
}
}
Output
[x = 2 y = 1, x = 1 y = 2, x = 3 y = 2]

Related

Y axis inverted in code that should form a rectangle

So i code consist of 3 parts 2 classes and test Idea is that 2 classes form a rectangle using inputs form the test.
This is code for test
#Test
public void testRectangle1() {
Point center = new Point(20, 30);
Rectangle rect = new Rectangle(center, 20, 20);
assertAll(
() -> assertEquals(10, rect.getTopLeft().getX()),
() -> assertEquals(20, rect.getTopLeft().getY()),
() -> assertEquals(30, rect.getBottomRight().getX()),
() -> assertEquals(40, rect.getBottomRight().getY()),
() -> assertEquals(20, rect.getWidth()),
() -> assertEquals(20, rect.getHeight())
);
}
First class called Point it works fine.
public class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point() {
this(0, 0);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void moveTo(int newX, int newY) {
x = newX;
y = newY;
}
public void moveRel(int dx, int dy) {
x += dx;
y += dy;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Point other = (Point) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}
So this is teh second class that forms recatangle itself. It consist of constructor on a few aditional methods for forming my rectangle
public class Rectangle {
public int width = 0;
public int height = 0;
public Point center;
public Rectangle(Point center, int width, int height) {
int x = 0;
int y = 0;
this.width=width;
this.height=height;
this.center=center;
}
public Point getTopLeft() {
Point point = new Point(center.getX(), center.getY());
point.moveRel(- width / 2, height / 2);
return point;
}
public Point getBottomRight() {
Point point = new Point(center.getX(), center.getY());
point.moveRel(width / 2, - height / 2);
return point;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}
So the problem is it seems that y axis is somehow inverted becouse when i run the test it returns wrong value,
() -> assertEquals(20, rect.getTopLeft().getY()), Where it returns 40 instead of 20 and test
() -> assertEquals(40, rect.getBottomRight().getY()), returns 2o instead of 40. So it seems that top left and bottom right point is inverted on y axis.
You add height/2 in moveRel to get topLeft. Hence it is normal that you get 40 and not 20.
Seems there is a sign error before height parameters in:
public Point getTopLeft() {
Point point = new Point(center.getX(), center.getY());
point.moveRel(- width / 2, height / 2);
return point;
}
public Point getBottomRight() {
Point point = new Point(center.getX(), center.getY());
point.moveRel(width / 2, - height / 2);
return point;
}

Overriding print statement in java based on object class

I have the following code in Java
I am able to print the calculated values in printDistance method.
Output:
4.0
3.0
But I need the output in below format(Expected Output).Please help
3D Distance = 4.0
2D Distance = 3.0
public class Point2D {
double x;
double y;
public double getX() {
return x;
}
public double getY() {
return y;
}
public Point2D(double x, double y) {
this.x = x;
this.y = y;
}
public double dist2D(Point2D p) {
x = (int) Math.ceil(Math.sqrt(Math.pow((p.getX() - x), 2) + Math.pow((p.getY() - y), 2)));
return x;
}
public static void printDistance(double d) {
System.out.println(d);
}
static public void main(String args[]) {
Point3D obj1 = new Point3D(1.0, 2.0, 3.0);
Point3D obj2 = new Point3D(3.0, 4.0,5.0);
printDistance(obj1.dist3D(obj2));
printDistance(obj1.dist2D(obj2));
}
}
class Point3D extends Point2D {
double z;
public double getZ() {
return z;
}
public Point3D(double x, double y, double z) {
super(x, y);
this.z = z;
}
public double dist3D(Point3D p) {
return (int) Math.ceil(Math.sqrt(Math.pow((p.getX() - x), 2) + Math.pow((p.getY() - y), 2) + +Math.pow((p.getZ() - z), 2)));
}
}
You can do it as follows:
public class Point2D {
double x;
double y;
public double getX() {
return x;
}
public double getY() {
return y;
}
public Point2D(double x, double y) {
this.x = x;
this.y = y;
}
public String dist2D(Point2D p) {
x = (int) Math.ceil(Math.sqrt(Math.pow((p.getX() - x), 2) + Math.pow((p.getY() - y), 2)));
return "2D Distance = "+x;
}
public static void printDistance(String d) {
System.out.println(d);
}
static public void main(String args[]) {
Point3D obj1 = new Point3D(1.0, 2.0, 3.0);
Point3D obj2 = new Point3D(3.0, 4.0,5.0);
printDistance(obj1.dist3D(obj2));
printDistance(obj1.dist2D(obj2));
}
}
class Point3D extends Point2D {
double z;
public double getZ() {
return z;
}
public Point3D(double x, double y, double z) {
super(x, y);
this.z = z;
}
public String dist3D(Point3D p) {
return "3D Distance = "+ Math.ceil(Math.sqrt(Math.pow((p.getX() - x), 2) + Math.pow((p.getY() - y), 2) + +Math.pow((p.getZ() - z), 2)));
}
}
Output:
3D Distance = 4.0
2D Distance = 3.0

How do I print out the value of the method 'center' in this code - Java

public class Point {
int x;
int y;
Point(int x, int y){
this.x = x;
this.y = y;
}
Point center(Point other) {
return new Point((x + other.x) / 2, (y + other.y) / 2); // How to print this value?
}
public void printPoint() {
System.out.println("(" + x + "," + y + ")"); //Value of x and y
}
}
public class Main {
public static void main(String[] args) {
Point p = new Point(1,5);
p.printPoint();
}
}
How do I print out the value for "center"?
public static void main(String[] args) {
Point p1 = new Point(1,5);
Point p2 = new Point(3,10);
Point center = p1.center(p2);
center.printPoint();
}

Java Algorithm for finding shortest distance between 2d point and Matrix

I'm stuck on this question for a couple of days now and would really like to get some help.
I am given a 2 dimensional point in the range of (0-1 not including 1), such as (0.5,0.2), and N other points (also in the range of 0-1).
The first part of the question is to implement the "dumb" algorithm, which when given a certain point will find the point with the shortest distance from it, which has a complexity of O(N).
The part I'm stuck at, requires to build a Matrix K on K, where each "cell" will contain the points that belong to that cell. Once done, when given the original point I will need to search for the point with the shortest distance to it only in some of the cells and not the entire Matrix, which should result better complexity.
My original thought is to devide the points so that each block will have an arraylist of points that belong to him, and then to somehow go through the main block(the one that the original point belongs to) and continue by going through it's neighbors, however implementing it hasn't been very successful.
I would highly appreciate any help/ advice.
Below is what I currently have:
public class Point {
private double x;
private double y;
private Block b;
public Point(double x, double y)
{
this.x=x;
this.y=y;
}
public Point(double x, double y, Block b) //consrtuctor for complex case
{
this.x=x;
this.y=y;
b.points.add(this);
}
public double getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public double distance(Point p)
{
double res=0;
res = Math.sqrt(Math.pow(this.x-p.getX(),2)+Math.pow(this.y-p.getY(),2));
return res;
}
}
import java.util.ArrayList;
public class Block {
private int x;
private int y;
public ArrayList<Point> points;
public Block(int x, int y) {
points = new ArrayList<Point>();
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
import java.util.Random;
public class ComplexCase {
private Block[][] blockMat;
public ComplexCase(int k, int n)
{
Random generator = new Random();
Point p1;
Block b1;
double x,y;
int bx1,by1;
int t;
t = 1/k;
blockMat = new Block[k][k];
for (int i =0;i<n;i++)
{
x = generator.nextDouble();
y = generator.nextDouble();
bx1 = (int) (x/t);
by1 = (int) (y/t);
b1 = new Block(bx1,by1);
p1 = new Point(x,y,b1);
}
}
public Block[][] getBlockMat() {
return blockMat;
}
public void setBlockMat(Block[][] blockMat) {
this.blockMat = blockMat;
}
}

How to make my HashMap work as expected?

Suppose one has a simple class:
public class Point implements Comparable<Point> {
public int compareTo(Point p) {
if ((p.x == this.x) && (p.y == this.y)) {
return 0;
} else if (((p.x == this.x) && (p.y > this.y)) || p.x > this.x) {
return 1;
} else {
return -1;
}
}
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
And a HashMap from Point to something, let's say Cell:
cellMap = new HashMap<Point, Cell>();
Then one fills in cellMap as follows:
for (int x = -width; x <= width; x++) {
for (int y = -height; y <= height; y++) {
final Point pt = new Point(x,y);
cellMap.put(pt, new Cell());
}
}
}
And then one does something like (trivial) this:
for (Point pt : cellMap.keySet()) {
System.out.println(cellMap.containsKey(pt));
Point p = new Point(pt.getX(), pt.getY());
System.out.println(cellMap.containsKey(p));
}
And gets true and false in, respectively, first and second cases. What is going on? Is this map comparing hashes instead of values? How to make the example return true in both cases?
Since you are using HashMap, not TreeMap, you need to override hashCode and equals, not compareTo, in your Point class:
#Override
public int hashCode() {
return 31*x + y;
}
#Override
public bool equals(Object other) {
if (other == null) return false;
if (other == this) return true;
if (!(other instanceof Point)) return false;
Point p = (Point)other;
return x == p.x && y == p.y;
}

Categories