new Double(object.toString());
But it seems weird to me that you're going from an Object to a Double. You should have a better idea what class of object you're starting with before attempting a conversion. You might have a bit of a code quality problem there.
Note that this is a conversion, not casting.
If your Object represents a number, eg, such as an Integer, you can cast it to a Number then call the doubleValue() method.
Double asDouble(Object o) {
Double val = null;
if (o instanceof Number) {
val = ((Number) o).doubleValue();
}
return val;
}
This is called Autoboxing which is a feature that was added in Java 5. It will automatically convert between primitive types and the wrapper types such as double (the primitive) and java.lang.Double (the object wrapper). The java compiler automatically transforms the line:
Double d = 5.1;
into:
Double d = Double.valueOf(5.1);
It is called AutoBoxing
Autoboxing and Auto-Unboxing of Primitive Types Converting between primitive types, like int, boolean, and their equivalent Object-based counterparts like Integer and Boolean, can require unnecessary amounts of extra coding, especially if the conversion is only needed for a method call to the Collections API, for example.
The autoboxing and auto-unboxing of Java primitives produces code that is more concise and easier to follow. In the next example an int is being stored and then retrieved from an ArrayList. The 5.0 version leaves the conversion required to transition to an Integer and back to the compiler.
Before
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(0, new Integer(42));
int total = (list.get(0)).intValue();
After
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(0, 42);
int total = list.get(0);
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);
}
}
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>();
}