English 中文(简体)
对比阵列和缩小差异
原标题:Comparing an array and getting the difference

我如何比较两个阵列,这些阵列可能会有不同的长度,使各阵列之间产生差异?

www.un.org/Depts/DGACM/index_spanish.htm 例如:

Cat cat = new Cat();
Dog dog = new Dog();
Alligator alligator = new Alligator();

Animal animals[] = { cat, dog };
Animal animals2[] = { cat, dog, alligator };

我将如何比较这两个阵列,使之回到<条码>上<0> >。

最佳回答

我想建议澄清你的问题。 目前,每个人都对你实际要求的东西进行猜测。

  • Are the arrays intended to represent sets, or lists, or something in between? In other words, does element order matter, and can there be duplicates?
  • What does "equal" mean? Does new Cat() "equal" new Cat()? Your example suggests that it does!!
  • What do you mean by the "difference"? Do you mean set difference?
  • What do you want to happen if the two arrays have the same length?
  • Is this a once-off comparison or does it occur repeatedly for the same arrays?
  • How many elements are there in the arrays (on average)?
  • Why are you using arrays at all?

如果假设这些阵列是真的,那么你或许应当使用而不是阵列,并利用诸如addAllretainAll等收款业务计算固定差额。

另一方面,如果这些阵列是为了代表名单,那么“推论”的含义并不明确。

如果该守则必须迅速实施,那么你当然需要重新思考你的数据结构。 如果你总是从阵列开始,那么你将无法快速计算“推论”,至少在一般情况下。

最后,如果你将使用任何取决于<代码>等值(目标)的方法(包括任何 Java收集类型)的方法,那么你确实需要清楚地了解你的申请中“平等”的含义。 是否所有<代码>Cat 例平等? 它们是否都不同? 某些<条码>Cat 的情况是平等的,有些则不是? 如果你不说明这一点,执行<代码>等于和<代码>hashCode的方法,则你将取得混淆结果。

问题回答

我建议你将物体放入各套,然后使用各套的交叉点:

// Considering you put your objects in setA and setB

Set<Object> intersection = new HashSet<Object>(setA);
intersection.retainAll(setB);

之后,你可以去除 a. 两套中的任何一套:

setA.removeAll(intersection);
setB.removeAll(intersection);

http://hype-free.blogspot.com/2008/11/calculating-intersection-of-second-java.html” rel=“nofollow noreferer” http://hype-free.blogspot.com/2008/11/culating-intersection-of-second-java.html

您也可使用<代码>。 Set 相反,使用remove all()方法。

或者你可以采用以下简单而缓慢的算法来做:

List<Animal> differences = new ArrayList<Animal>();

    for (Animal a1 : animals) {
       boolean isInSecondArray = false;
       for (Animal a2 : animals2) {
           if (a1 == a2)  {
                isInSecondArray = true;
                break;
           }
       } 

       if (!isInSecondArray)
           differences.add(a1)
    }

然后,differences上的所有物体将载于animals 阵列,但未列入animals 2阵列。 同样,你可以做的是相反的(指animals<2/code>但载于animals的所有物体)。

您不妨研究这一条款,以便了解更多信息:

http://download-llnw.oracle.com/javase/tutorial/collections/interfaces/set.html

正如有人提到的那样,remove All(>>是为此而制定的,但你将两次这样做,以便你能够编制一份双方都不存在的所有清单,然后,你可以把这两个结果结合起来,列出所有分歧。

但这是一种破坏性行动,因此,如果你不想失去信息,就复制<代码>。 Set ,按此操作。

<><>UPDATE:

看来,我对阵列中的假设是错误的,因此,<代码>remove All(>赢得了t work,但根据5点要求,将搜索的物品数量降为问题。

因此,它似乎有<代码>HashMap<String, Hor>是最佳选择,因为它正在快速搜索。

动物是至少一种财产的接口,String name。 执行<代码>Animal的每个类别:<代码>Equals和hashCode的代码。 可在以下网址查阅一些讨论:。 http://www.ibm.com/developerworks/java/library/j-jtp05273.html。 这样,如果你想把动物种类和名称结合起来,那就会被罚款。

So, the basic algorithm is to keep everything in the hashmaps, and then to search for differences, just get an array of keys, and search through to see if that key is contained in the other list, and if it isn t put it into a List<Object>, storing the value there. You will want to do this twice, so, if you have at least a dual-core processor, you may get some benefit out of having both searches being done in separate threads, but then you will want to use one of the concurrent datatypes added in JDK5 so that you don t have to worry about synchronizations in the combined list of differences.

So, I would write it first as a single-thread and test, to get some ideas as to how much faster it is, also comparing it to the original implmemntation. Then, if you need it faster, try using threads, again, compare to see if there is a speed increase.

在进行优化之前,确保你对你已经拥有的一些衡量标准,以便你能够比较和看看一次变化是否会加快速度。

如果你在某个时候做了太多的改动,那么速度就会大大提高,但其他变化可能会导致业绩下降,因此,现在就会出现这种情况。

如果通过使用单位检测和检测,也许每100次,你就可以了解每个变化给你带来的改进。

我不关心我的用法(你也不要这样做,除非你有很好的理由,而且你通过你的简介发现,这部法律是瓶颈。

我所做的事情与功能性回答类似。 I use LINQ set营运人将例外情况列入每个清单:

<><>Edit>:

索里,我没有注意到这一点。 Sorry, I m off in C# la-la land, and they look so similar :





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签