如果我的目标是找到唯一的对,我应该使用什么数据结构在 Java 中存储一对字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16617662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:29:05  来源:igfitidea点击:

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

javadata-structures

提问by user2394904

I am a beginner in Java. I have some sample data of nodes:

我是 Java 的初学者。我有一些节点的示例数据:

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]

我已经拿出了 2 个列表:[A,B,C,A,B,R] 和 [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 .

但是,我应该如何存储对 [AB, BF, CR, AB, BC, RC] 以便我可以找到唯一的对?通过独特,我的意思是 AB 不等于 BA 。

1) So Basically I would like to identify unique pairs.

1)所以基本上我想识别独特的对。

2) I also want to count the number of times each unique pair has appeared.

2)我还想计算每个唯一对出现的次数。

EDITED:

编辑:

3) I am also interested in finding how many different nodes each node connects to.

3)我也有兴趣找到每个节点连接到多少个不同的节点。

4) And how many different nodes connect to each node

4) 每个节点连接了多少个不同的节点

I am struggling to decide whether I really need to write my own class or is there an easier method?

我正在努力决定我是否真的需要编写自己的类还是有更简单的方法?

采纳答案by Kent

Hashtable (datastructure) should work for your requirement. In java, you could consider type HashMap<String,Integer>

哈希表(数据结构)应该适合您的要求。在java中,你可以考虑类型HashMap<String,Integer>

key is the string pair, Integer is count:

键是字符串对,整数是计数:

something like:

就像是:

{
"AB":2,
"CR":1,
"BF":1,
...

}

The complexity of finding unique pairs would be O(n)

找到唯一对的复杂性将是 O(n)

EDIT

编辑

it seems that putting codes here helps to explain the solution:

似乎将代码放在这里有助于解释解决方案:

Map<String, Integer> map = new HashMap<String,Integer>();

//you have two lists with those strings, called list1 and list2.
// list1<String> and list2<String> have same size

String key = null;
for(int i=0;i<list1.size();i++){
    key = list1.get(i) + list2.get(i);
    if(map.containsKey(key))
        map.get(key)++;
    else
        map.put(key,1);
}

//now the map has been filled, you can go through the map, 
//and check the value, if value == 1, then the key is unique.
//for those value >1, you know, which string pair is not unique, 
// and how many times it shows.

codes were not written in IDE, so there could be typoes.

代码不是在 IDE 中编写的,所以可能会有错别字。

回答by srikanta

You can create a custom class to store pairs of strings and then use a HashMapto keep track of the count

您可以创建一个自定义类来存储字符串对,然后使用 aHashMap来跟踪计数

public class StringPair {
   String leftString;
   String rightString;

   //NOTE: override hashcode and equals methods
}

And then you can use HashMapfor keeping tracking of the count:

然后您可以HashMap用于跟踪计数:

Map<StringPair, Integer> pairCountMap = new HashMap<StringPair, Integer>();

if(pairCountMap.containsKey(aPairObject)) {
   pairCountMap.put(aPairObject, pairCountMap.get(aPairObject)+1);
} else {
   pairCountMap.put(aPairObject, 0);
}

回答by harsh

You need a class to designate pair:

您需要一个类来指定对:

public class Pair{
 String prv;
 String next;
 //override hashcode and equals
}

If you use Setand fill it in with all pair, you'll end-up having unique pairs:

如果您使用Set并填充所有对,您最终将拥有独特的对:

Set<Pair> pairs = new HashSet<Pair>();
..
pairs.add(new Pair(prv, next));
int uniquePairs = pairs.size();

If you use TreeSetand make PairimplementComparable, you'll have a sorted list of pairs

如果您使用TreeSet和 make PairimplementComparable,您将拥有一个排序的对列表

Set<Pair> pairs = new TreeSet<Pair>();
..
System.out.println(pairs);

Further, you can use a combination of Listand Setand apply some logic to figure out exact number of duplicates etc, can also explore removeAlland retainAllfor implementing logic.

此外,你可以使用的组合ListSet和应用一些逻辑弄清楚的副本等确切的数字,也可以探索removeAllretainAll实施的逻辑。

Also, Mapdoesn't seem to be a fit in your use case since a class can wrap required mapping and list or set will help to apply desired logic over multiple pairs.

此外,Map似乎不适合您的用例,因为类可以包装所需的映射,列表或集合将有助于在多个对上应用所需的逻辑。

To get counts of total number of original pairs:

要获得原始对总数的计数:

Set<Pair> pairs = new HashSet<Pair>();
int count =0;
while(..) { //iterating over list of pairs
    pairs.add(new Pair(prv, next));
    count ++;
   }
int uniquePairs = pairs.size();
int totalPairs = count;