Java 16 includes Record Classes:
public record Pair<K, V>(K key, V value)
{
// intentionally empty
}
Creates a class with final fields, constructor, methods to retrieve key & value, and overrides of equals, hashCode and toString.
Usage example:
var one = new Pair<Integer, String>(1, "one");
var two = new Pair(2, "two");
System.out.println(one); // Pair[key=1, value=one]
if (!one.equals(two))
System.out.printf("not same as Pair for %d%n", two.key());
not sure what is best, if available (java 16) I would use that
JLS 8.10
JEP 395
To avoid an XY problem here's what I'm doing:
I'm working on an assignment where I'm supposed to maintain a graph and an adjacency list and do Dijkstra's algorithm and stuff, and the way I'm going about it is to make a Vertex class that has fields such as the name of the vertex (since names/labels are relevant in the assignment) and also a field that is a list of adjacent vertices. However, I'm not sure how to deal with weighted edges. It's easy enough if say vertex A has a list that looks like [B, C, E] which indicates that A has an edge that connects to B, C, and E each, but there's no indication of what the weights of those edges are, which is essential for Dijsktra's.
I'd like the list of adjacent vertices to look more like [B:4, C:2, E:6] meaning that the weight from A to B is 4, the weight from A to C is 2, and the weight from A to E is 6. But I'm not actually sure how to do this?