I am a beginner in Java. I have some sample data of nodes:
A -> B
B -> F
C -> R
A -> B
B -> C
R -> C
I have already taken out 2 lists: [A,B,C,A,B,R] and [B,F,R,B,C,C]
However, how should I go about storing the pairs [AB, BF, CR, AB, BC, RC] so that I can find unique pairs? By unique, I mean AB is not equal to BA .
1) So Basically I would like to identify unique pairs.
2) I also want to count the number of times each unique pair has appeared.
EDITED:
3) I am also interested in finding how many different nodes each node connects to.
4) And how many different nodes connect to each node
I am struggling to decide whether I really need to write my own class or is there an easier method?
You can create a custom class to store pairs of strings and then use a HashMap
to keep track of the count
public class StringPair {
String leftString;
String rightString;
//NOTE: override hashcode and equals methods
}
And then you can use HashMap
for keeping tracking of the count:
Map<StringPair, Integer> pairCountMap = new HashMap<StringPair, Integer>();
if(pairCountMap.containsKey(aPairObject)) {
pairCountMap.put(aPairObject, pairCountMap.get(aPairObject)+1);
} else {
pairCountMap.put(aPairObject, 0);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With