Compare every item to every other item in ArrayList
I'm having trouble with what I thought should be a pretty simple problem.
I need to compare every item in an arrayList with every other i开发者_运维知识库tem in the the list without comparing items to themselves. It's not as simple as calling an equals() comparison, it involves some custom logic that I've omitted from my code below. Also the ArrayList should not be changed in any way.
The problem I seem to be having is that once I get into the second loop, I don't know if I have another object to compare to (since its a variable sized list).
for(int i =0; i< list.size(); i++){
//get first object to compare to
String a = list.get(i).getA();
Iterator itr = list.listIterator(i + 1 ); // I don't know if i + 1 is valid
while(itr.hasNext()){
// compare A to all remaining items on list
}
}
I think I probably going about this the wrong way, I'm open to suggestions or tips on how to do this better.
for (int i = 0; i < list.size(); i++) {
for (int j = i+1; j < list.size(); j++) {
// compare list.get(i) and list.get(j)
}
}
What's the problem with using for
loop inside, just like outside?
for (int j = i + 1; j < list.size(); ++j) {
...
}
In general, since Java 5, I used iterators only once or twice.
This code helped me get this behaviour: With a list a,b,c, I should get compared ab, ac and bc, but any other pair would be excess / not needed.
import java.util.*;
import static java.lang.System.out;
// rl = rawList; lr = listReversed
ArrayList<String> rl = new ArrayList<String>();
ArrayList<String> lr = new ArrayList<String>();
rl.add("a");
rl.add("b");
rl.add("c");
rl.add("d");
rl.add("e");
rl.add("f");
lr.addAll(rl);
Collections.reverse(lr);
for (String itemA : rl) {
lr.remove(lr.size()-1);
for (String itemZ : lr) {
System.out.println(itemA + itemZ);
}
}
The loop goes as like in this picture: Triangular comparison visual example
or as this:
| f e d c b a
------------------------------
a | af ae ad ac ab ·
b | bf be bd bc ·
c | cf ce cd ·
d | df de ·
e | ef ·
f | ·
total comparisons is a triangular number (n * n-1)/2
In some cases this is the best way because your code may have change something and j=i+1 won't check that.
for (int i = 0; i < list.size(); i++){
for (int j = 0; j < list.size(); j++) {
if(i == j) {
//to do code here
continue;
}
}
}
The following code will compare each item with other list of items using contains() method.Length of for loop must be bigger size() of bigger list then only it will compare all the values of both list.
List<String> str = new ArrayList<String>();
str.add("first");
str.add("second");
str.add("third");
List<String> str1 = new ArrayList<String>();
str1.add("first");
str1.add("second");
str1.add("third1");
for (int i = 0; i<str1.size(); i++)
{
System.out.println(str.contains(str1.get(i)));
}
Output is true true false
You can do it like this.
for (int i = 0; i < arrayList.size(); i++) {
for (int j = 0; j < arrayList.size(); j++) {
if (i!=j && arrayList.get(i).YourObjectItem().equals(arrayList.get(j).YourObjectItem())) {
//Your code will be here
}
}
}
Old question, but I just wanted to provide a functional version from the Kaleb Brasee's one, (which, BTF is far more faster and heap friendly than this) for functional programming purists.
IntStream.range(0, list.size())
.forEach(i -> compare(list.get(i),
list.subList(i + 1, list.size())));
...
private void compare(final Element element, final List<Element> list) {
list.stream()
//whatever
}
精彩评论