Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What data structure should I use to store a pair of strings in Java , if my goal is to find unique pairs?

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?

like image 571
user2394904 Avatar asked May 17 '13 20:05

user2394904


1 Answers

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);
}
like image 79
srikanta Avatar answered Sep 20 '22 21:09

srikanta