You are not logged in. Please login at www.codechef.com to post your questions!

×

How to implement Pair Class in Java?

Is there any inbuilt library for pair class in java like in c++? If not how to we implement pair class in java and how can we do pair sort with respect to the first element and pair sort with respect to the second element in java?

asked 18 Jan, 14:00

saisatwik_k's gravatar image

3★saisatwik_k
11
accept rate: 0%

edited 18 Jan, 18:36


This should function similar to the C++ pair and be good enough for competitive programming purposes

class Pair<S extends Comparable<S>, T extends Comparable<T>> implements Comparable<Pair<S, T>> {
    S first;
    T second;

    Pair(S f, T s) {
        first = f;
        second = s;
    }

    @Override
    public int compareTo(Pair<S, T> o) {
        int t = first.compareTo(o.first);
        if (t == 0) return second.compareTo(o.second);
        return t;
    }

    @Override
    public int hashCode() {
        return (31 + first.hashCode()) * 31 + second.hashCode();
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof Pair)) return false;
        if (o == this) return true;
        Pair p = (Pair) o;
        return first.equals(p.first) && second.equals(p.second);
    }

    @Override
    public String toString() {
        return "Pair{" + first + ", " + second + "}";
    }
}
link

answered 18 Jan, 22:28

meooow's gravatar image

6★meooow ♦
7.1k718
accept rate: 48%

edited 18 Jan, 22:34

Yes,It is there in Java also.

It is not work in Java 7,But It is available in Java 8.

We need to store the values into Pair using the parameterized constructor provided by the "javafx.util.Pair" class.

Note that the "(Key,Value)" pair used in HashMap/TreeMap. Here, <key, value=""> simply refers to a pair of values that are stored together.

Check Here Once : https://www.geeksforgeeks.org/pair-class-in-java/

link

answered 18 Jan, 14:19

siva140191's gravatar image

3★siva140191
1335
accept rate: 12%

But when I import JavaFX library in codechef IDE it was showing a syntax error.

(18 Jan, 18:31) saisatwik_k3★

This is my implementation. This is what I use.

public static class Point extends Object implements Comparable<Point> {

//variables are final for a reason
//delete the previous point and add a new modified one
private final int x;
private final int y;

public Point(){}

public Point(int x, int y){
    this.x = x;
    this.y = y;
}

public int getX(){return this.x;}
public int getY(){return this.y;}

public void show(){
    System.out.println("[" + this.getX() + " , " + this.getY() + "]");
}

public int distance(Point p2){

    double d = Math.pow(this.x - p2.x, 2) + Math.pow(this.y - p2.y, 2);

    return (int)Math.sqrt(d);
}



/* This method is called while sorting
 * by default it sorts in ascending order
 * of x and y
 */
@Override
public int compareTo(Point p){

    if(this.x != p.x)
        return this.x = p.x;

    return this.y - p.y;
}

//https://www.artima.com/lejava/articles/equality.html
@Override
public boolean equals(Object obj){
    if(super.equals(obj)) return true;
    if(!obj instanceof Point) return false;

    Point that = (Point)obj;
    return this.x == that.x && this.y == that.y;
}

//see the link above
@Override 
public int hashCode() {
    return (59 * (59 + getX()) + getY());
}

@Override
public String toString(){
    return "[" + this.getX() + " , " + this.getY() + "]";
}

}

link

answered 18 Jan, 22:58

aky91's gravatar image

3★aky91
01
accept rate: 0%

edited 18 Jan, 23:02

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×111
×20

question asked: 18 Jan, 14:00

question was seen: 154 times

last updated: 18 Jan, 23:02