-5

I would like to know if is it possible to create an object of class with two parameters. Indeed, I want two store two values (double, int) with the same key in my hashmap as this:

{1 = (4.0, 5),.... }

So, I did something like that but I do not how to get the resultat above:

public class Dijkstra{
private final List<Integer> L;
private final int V;
private final int P;
private final double C;

  public Dijkstra(){
      L = new ArrayList<>();
  }

//Create my object to put after in my hashmap
public Dijkstra(int P, double C){
this.P = P;
this.C = C;
}
.....
public Map mymethod() {
Map<Integer, Object> m = new HashMap<Integer, Object>();
}
}

So, I want to create an object of my class with my two parameters to put it in my hashmap. How can I do this in java?

Thank you.

MAYA
  • 1,243
  • 1
  • 12
  • 20
  • `Map` ...? – Lino Jul 27 '17 at 07:01
  • but Djkstra has other pa?rameters – MAYA Jul 27 '17 at 07:03
  • 1
    *what* are you trying to achieve? – Lino Jul 27 '17 at 07:04
  • You define a class with two elements, getters and setters. And you create a map from Integer to that class. – RealSkeptic Jul 27 '17 at 07:05
  • is it possible to define it with the same class that has more elements? – MAYA Jul 27 '17 at 07:05
  • It's a mistake. I edit my code. It is Dijkstra not myclass – MAYA Jul 27 '17 at 07:08
  • `Map map = new HashMap<>(); map.put(1, new Dijkastra(5,4.0));` – Lino Jul 27 '17 at 07:09
  • @Lino, the parameter order is reverse, first is int and scond is double. – Chandan Rajput Jul 27 '17 at 07:11
  • @Lino, my class has more than two elements. Ok. My question is why not to create an object with two parameters in the same class even it has more than two parameters. Why is not possible in java? – MAYA Jul 27 '17 at 07:15
  • There are several SO-questions about why Java does not have a native pair or tuple type. https://stackoverflow.com/q/156275/3888450 https://stackoverflow.com/q/521171/3888450 – Stefan Dollase Jul 27 '17 at 07:24
  • @Stefan Dollase my question is not why Java does not have a native pair or tuple type. I was looking for another method rather than create a new class in other file. As a beginner in Java, I did a lot of research in the forum but I was looking for explanation. Finally, I understand how it works. – MAYA Jul 27 '17 at 07:26

2 Answers2

2

You need to create a class for that that will hold exactly these two values:

import java.util.*;

class MyValuePair {
    private int intValue;
    private double doubleValue;

    MyValuePair(double doubleValue, int intValue) {
        this.intValue = intValue;
        this.doubleValue = doubleValue;
    }

    //Getter and Setter as well as constructor(s) as needed
    public String toString() {
        return "(" + doubleValue + ", " + intValue + ")";
    }

}

public class Dijkstra {

    static Map<Integer, MyValuePair> map = new HashMap<>();

    public static void main(String[] args) {
        MyValuePair pair = new MyValuePair(4.0, 5);
        map.put(1, pair);
        System.out.println(map);
    }

}

So your value pairs go in MyValuePair and the objects of that class will go in the Map. You can create a new public class for it if you need (= in a new file with public keyword) or leave it in there as internal class.

Anything else with Lists and whatever you need will go in the Dijkstra class.

After the comment:

As said it is possible! But you do not want to do that. However this would be a code that would do the same where the Dijkstra class holds the values as well:

public class Dijkstra {

    static Map<Integer, Dijkstra> map = new HashMap<>();

    private double doubleValue;
    private int intValue;

    public Dijkstra(int intValue, double doubleValue) {
        this.intValue = intValue;
        this.doubleValue = doubleValue;
    }

    public String toString() {
        return "(" + doubleValue + ", " + intValue + ")";
    }

    public static void main(String[] args) {
        Dijkstra pair = new Dijkstra(4.0, 5);
        map.put(1, pair);
        System.out.println(map);
    }

}
geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
  • Ok. My question is why not to create an object with two parameters in the same class even it has more than two parameters. Why is not possible in java? – MAYA Jul 27 '17 at 07:14
  • It is possible. The issue then is that you would waste a lot of memory and you are having a class that serves more than one purpose. You generally want that one class is designed for one thing. In your case Dijkstra is supposed to hold the gathered values of the current path (I assume) and provide methods to calculate the path. It should not have a second purpose for holding values for itself. – geisterfurz007 Jul 27 '17 at 07:17
  • I added a way of doing what I think you were thinking of, but again.... I recommend not to do it. – geisterfurz007 Jul 27 '17 at 07:21
  • Thank you so much for explanation. I prefer the first solution since it will be the same file and does not wast memory. Indeed, this object is just for Dijkstra class. – MAYA Jul 27 '17 at 07:23
1

You should create a new class representing whatever your int and double parameters represent as:

public class MyRepresentaion{
    private int myInt;
    private double myDouble;

    public MyRepresentation(int myInt, double myDouble){
        this.myInt = myInt;
        this.myDouble = myDouble;
    }

    public int getMyInt(){
        return myInt;
    }
    public double getMyDouble(){
        return myDouble;
    }
    public void setMyInt(int value){
        myInt = value;
    }
    public void setMyDouble(double value){
        myDouble = value;
    }
}

You should then just put your MyRepresentaion object in the map:

public Map mymethod() {
    Map<Integer, MyRepresentaion> m = new HashMap<Integer, MyRepresentaion>();
}
Mads T
  • 530
  • 3
  • 14