Map<Player, Collection<Player>> map = ...
void put(Player a, Player b)
{
Collection<Player> coll = map.get(a);
if (coll == null)
{
coll = new ArrayList<Player>();
map.put(a, coll);
}
coll.add(b);
}
notPlayed = new LinkedList <Entry <String, String>>();
public class Pair <A, B>
{
private A a;
private B b;
public Pair(A a, B b)
{
this.a = a;
this.b = b;
}
public A getA()
{
return a;
}
public B getB()
{
return b;
}
}
notPlayed = new LinkedList <Pair <String, String>>();
public boolean equals(Pair <A, B> p1, Pair <A, B> p2)
{
boolean aEqualsA = false;
boolean bEqualsB = false;
boolean aEqualsB = false;
boolean bEqualsA = false;
if (p1.getA() == p2.getA())
{
aEqualsA = true;
}
if (p1.getB() == p2.getB())
{
bEqualsB = true;
}
if (p1.getA() == p2.getB())
{
aEqualsB = true;
}
if (p1.getB() == p2.getA())
{
bEqualsA = true;
}
if ((aEqualsA && bEqualsB) || (aEqualsB && bEqualsA))
{
return true;
}
return false;
}
class Pair<A, B>
{
public boolean equals(Object object)
{
... dein job...
}
public int hashCode()
{
... dito ...
}
}
class K ...
public boolean equals(Object object)
{
if( object == null )
return false;
if( object == this )
return true; // identität ist erschlagend
if( ! ( object instanceof K ) )
return false;
// ab hier die teuren methoden beginnen
}
public class Pair<A, B>
{
private A a;
private B b;
public Pair(A a, B b)
{
this.a = a;
this.b = b;
}
public A getA()
{
return a;
}
public B getB()
{
return b;
}
@Override
public boolean equals(Object o)
{
if (!(o instanceof Pair))
{
return false;
}
Pair <A, B> p = (Pair <A, B>) o;
boolean aEqualsA = false;
boolean bEqualsB = false;
boolean aEqualsB = false;
boolean bEqualsA = false;
if (getA() == p.getA())
{
aEqualsA = true;
}
if (getB() == p.getB())
{
bEqualsB = true;
}
if (getA() == p.getB())
{
aEqualsB = true;
}
if (getB() == p.getA())
{
bEqualsA = true;
}
if ((aEqualsA && bEqualsB) || (aEqualsB && bEqualsA))
{
return true;
}
return false;
}
@Override
public int hashCode()
{
return a.hashCode() + b.hashCode();
}
}
[Pair@6036a1f, Pair@68ab28a, Pair@5e7a9c, Pair@c2c9bad, Pair@60063bf, Pair@687ac2a]
Marco13 hat gesagt.:Für die equals-Methode darfst du die Objekte aber NICHT mit "==" vergleichen! Stattdessen musst du dort das "equals" der jeweiligen Objekte verwenden (wenn sie nicht null sind)
public String toString()
{
return "("+a+", "+b+")";
}
return a.hashCode() ^ b.hashCode();
public int hashCode()
{
return a.hashCode() + b.hashCode();
}
public String toString()
{
return a + "=" + b;
}