Last active
December 17, 2015 11:29
-
-
Save alexflav23/5602060 to your computer and use it in GitHub Desktop.
StackOverflow Fun
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Pair { | |
private String left; | |
private String right; | |
public Pair(String v1, String v2) { | |
left = v1; | |
right = v2; | |
}; | |
public String getLeft() { | |
return left; | |
}; | |
public String getRight() { | |
return right; | |
} | |
@Override | |
public int hashCode() { | |
final int prime = 31; | |
int result = 1; | |
result = prime * result + ((left == null) ? 0 : left.hashCode()); | |
result = prime * result + ((right == null) ? 0 : right.hashCode()); | |
return result; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) | |
return true; | |
if (obj == null) | |
return false; | |
if (getClass() != obj.getClass()) | |
return false; | |
Pair other = (Pair) obj; | |
if (left == null) { | |
if (other.left != null) | |
return false; | |
} else if (!left.equals(other.left)) | |
return false; | |
if (right == null) { | |
if (other.right != null) | |
return false; | |
} else if (!right.equals(other.right)) | |
return false; | |
return true; | |
} | |
}; | |
public static void main(String[] args) { | |
HashMap<Pair, Integer> myMap = new HashMap<Pair, Integer>(); | |
List<Pair> list = new ArrayList<Pair>(); | |
list.add(new Pair("A", "B")); | |
list.add(new Pair("A", "B")); | |
list.add(new Pair("B", "F")); | |
list.add(new Pair("C", "R")); | |
list.add(new Pair("A", "R")); | |
list.add(new Pair("B", "C")); | |
list.add(new Pair("R", "C")); | |
list.add(new Pair("R", "C")); | |
for (Pair pair : list) { | |
if (myMap.containsKey(pair)) { | |
myMap.put(pair, myMap.get(pair) + 1); | |
} else { | |
myMap.put(pair, 1); | |
} | |
} | |
Iterator it = myMap.entrySet().iterator(); | |
while (it.hasNext()) { | |
Map.Entry pairs = (Map.Entry) it.next(); | |
System.out.println(pairs.getKey() + " = " + pairs.getValue()); | |
it.remove(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment