ssss
ssss

Reputation: 71

Mapping (x,y) to single number value

I want a function to map the (x,y) value to a single number value and corresponding reverse mapping in Java.

For example (2,34) should map to some value 100 and if we have 100 we should be able to find (2,34).

Upvotes: 6

Views: 7371

Answers (8)

Jon Egerton
Jon Egerton

Reputation: 41559

An alternative strategy based on paxdiablo's answer is to use the decimal point, so you would put the x component before the decimal place and the y component after it.

This can entail some messy string handling, but doesn't have the limitation that you need to be able to bound one of your coordinates.

Upvotes: 0

ssss
ssss

Reputation: 11

public class hash {
public static void main(String args[]) throws IOException
    {
    HashMap<List<Integer>,List<Integer>> g=new HashMap<List<Integer>,List<Integer>>();
    List<Integer> xys=new ArrayList<Integer>();
    List<Integer> ss=new ArrayList<Integer>();
    System.out.println("Enter the coordinates in (x,y)");
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("x");
    int n1=Integer.parseInt(reader.readLine());
    System.out.println("y");

    int n2=Integer.parseInt(reader.readLine());
    xys.add(n1);
    xys.add(n2);
    int d=(int)(Math.pow(xys.get(0),xys.get(1)));

    ss.add(d);

   g.put(xys,ss);
   g.put(ss,xys);
        List<Integer> r= g.get(ss);

        List<Integer> r1=g.get(xys);

        System.out.print("Hash for the value (x,y) mapping");
        System.out.print(r1);


        System.out.print("Hash for the value N, reverse mapping");
        System.out.println(r);


}
}

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 881563

Well, if you have a maximum value for one of them, then yes. Say y is limited to 0...9999, then could can do:

int num = x * 10000 + y;

(assuming of course that the result will fit in the type - you can possibly upgrade to a wider type like long if necessary). Then, to go back:

int x = num / 10000;
int y = num % 10000;

Upvotes: 9

Joonas Pulakka
Joonas Pulakka

Reputation: 36577

Remember, primitive numbers are just a bunch of raw bits. If your numbers are 32-bit integers (int), you could conveniently pack two of them into one 64-bit integer (long). Something like this:

int a = 3;
int b = 4;
long c = ((long) a) << 16 | b;

...

a = (int) (c >> 16);
b = (int) (c & 0xffff);

Upvotes: 2

peoro
peoro

Reputation: 26060

If you've got an upper limit (say 100 is the higher for x in (x,y)), then you could associate any n to a pair of values (x,y) with this function: n = x * 100 + y.

If you don't have any limit things get more difficult. A way to associate a number belonging to R, to a number of R^2 is that of enumerating diagonals. Look at this example, it's a matrix where the (x,y) cell contains the n associated to it:

1  2  4  7  11 ..
3  5  8  12 ..
6  9  13 ..
10 ..
..

Upvotes: 5

Jesper
Jesper

Reputation: 206816

What is the background for this requirement?

If the range of x, y values is unlimited, then there is no way to do this in a unique way (so that you can always reverse the process).

If the range of the numbers is limited, then you could do the following: Suppose that x and y are int values both in the range 0 to 99. Then you calculate the result by: z = 100 * x + y. The reverse would be: x = z / 100 and y = z % 100.

Upvotes: 0

user467871
user467871

Reputation:

You can keep them as a String.

String a = "(2,34)";
String b = "100";

Map<String, String> m = new HashMap<String, String>();
m.put(a, b);
m.put(b, a);

/*print*/
System.out.println(m.get(a));
System.out.println(m.get(b));

Upvotes: 0

Hery
Hery

Reputation: 7639

You should take a look at HashMap. Something like HashMap<Integer, List<Integer>> seems to work well for your case.

Upvotes: 1

Related Questions