Prosty sposób na porównanie 2 ArrayLists
Mam 2 arraylists obiektu string.
List<String> sourceList = new ArrayList<String>();
List<String> destinationList = new ArrayList<String>();
Mam pewną logikę, w której muszę przetworzyć listę źródłową i skończyć z listą docelową. Lista docelowa będzie zawierać dodatkowe elementy dodane do listy źródeł lub usunięte z listy źródeł.
Moje oczekiwane wyjście to 2 ArrayList łańcuchów, gdzie pierwsza lista powinna mieć wszystkie łańcuchy usunięte ze źródła, a druga lista powinna mieć wszystkie łańcuchy nowo dodane do źródła.
Wszelkie prostsze sposób, aby to osiągnąć?
9 answers
Konwertuj listy na Collection
i użyj removeAll
Collection<String> listOne = new ArrayList(Arrays.asList("a","b", "c", "d", "e", "f", "g"));
Collection<String> listTwo = new ArrayList(Arrays.asList("a","b", "d", "e", "f", "gg", "h"));
List<String> sourceList = new ArrayList<String>(listOne);
List<String> destinationList = new ArrayList<String>(listTwo);
sourceList.removeAll( listTwo );
destinationList.removeAll( listOne );
System.out.println( sourceList );
System.out.println( destinationList );
Wyjście:
[c, g]
[gg, h]
[EDIT]
Other way (more clear)
Collection<String> list = new ArrayList(Arrays.asList("a","b", "c", "d", "e", "f", "g"));
List<String> sourceList = new ArrayList<String>(list);
List<String> destinationList = new ArrayList<String>(list);
list.add("boo");
list.remove("b");
sourceList.removeAll( list );
list.removeAll( destinationList );
System.out.println( sourceList );
System.out.println( list );
Wyjście:
[b]
[boo]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-03 09:54:18
To powinno sprawdzić, czy dwie listy są sobie równe, najpierw sprawdza podstawowe wartości (nulls i lengths), a następnie sortuje i używa kolekcji.equals metoda sprawdzająca, czy są równe.
public boolean equalLists(List<String> a, List<String> b){
// Check for sizes and nulls
if (a == null && b == null) return true;
if ((a == null && b!= null) || (a != null && b== null) || (a.size() != b.size()))
{
return false;
}
// Sort and compare the two lists
Collections.sort(a);
Collections.sort(b);
return a.equals(b);
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-17 10:08:59
Konwertuj List
na String
i sprawdź, czy ciągi są takie same, czy nie
import java.util.ArrayList;
import java.util.List;
/**
* @author Rakesh KR
*
*/
public class ListCompare {
public static boolean compareList(List ls1,List ls2){
return ls1.toString().contentEquals(ls2.toString())?true:false;
}
public static void main(String[] args) {
ArrayList<String> one = new ArrayList<String>();
ArrayList<String> two = new ArrayList<String>();
one.add("one");
one.add("two");
one.add("six");
two.add("one");
two.add("two");
two.add("six");
System.out.println("Output1 :: "+compareList(one,two));
two.add("ten");
System.out.println("Output2 :: "+compareList(one,two));
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-03 09:53:47
ODPOWIEDŹ znajduje się w@DKU-rajkumar post.
ArrayList commonList = CollectionUtils.retainAll(list1,list2);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:18:25
Najprostszym sposobem jest iteracja list źródłowych i docelowych jeden po drugim tak:
List<String> newAddedElementsList = new ArrayList<String>();
List<String> removedElementsList = new ArrayList<String>();
for(String ele : sourceList){
if(destinationList.contains(ele)){
continue;
}else{
removedElementsList.add(ele);
}
}
for(String ele : destinationList){
if(sourceList.contains(ele)){
continue;
}else{
newAddedElementsList.add(ele);
}
}
Chociaż może to nie być zbyt efektywne, jeśli Twoje listy źródłowe i docelowe mają wiele elementów, ale z pewnością jest prostsze.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-03 09:44:53
Jeśli twoim wymogiem jest utrzymanie zamówienia reklamowego oraz sprawdzenie zawartości dwóch arraylist, powinieneś wykonać następujące czynności:
List<String> listOne = new ArrayList<String>();
List<String> listTwo = new ArrayList<String>();
listOne.add("stack");
listOne.add("overflow");
listTwo.add("stack");
listTwo.add("overflow");
boolean result = Arrays.equals(listOne.toArray(),listTwo.toArray());
To zwróci true.
Jeśli jednak zmienisz kolejność na przykład:
listOne.add("stack");
listOne.add("overflow");
listTwo.add("overflow");
listTwo.add("stack");
boolean result = Arrays.equals(listOne.toArray(),listTwo.toArray());
Zwróci false, ponieważ kolejność jest inna.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-17 03:48:55
O ile dobrze to rozumiem, to chyba najłatwiej pracować z 4 listami: - Twój żródło - Your destinationList - Usunięta lista - A newlyAddedItemsList
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-03 09:18:52
private int compareLists(List<String> list1, List<String> list2){
Collections.sort(list1);
Collections.sort(list2);
int maxIteration = 0;
if(list1.size() == list2.size() || list1.size() < list2.size()){
maxIteration = list1.size();
} else {
maxIteration = list2.size();
}
for (int index = 0; index < maxIteration; index++) {
int result = list1.get(index).compareTo(list2.get(index));
if (result == 0) {
continue;
} else {
return result;
}
}
return list1.size() - list2.size();
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-21 07:36:56
boolean isEquals(List<String> firstList, List<String> secondList){
ArrayList<String> commons = new ArrayList<>();
for (String s2 : secondList) {
for (String s1 : firstList) {
if(s2.contains(s1)){
commons.add(s2);
}
}
}
firstList.removeAll(commons);
secondList.removeAll(commons);
return !(firstList.size() > 0 || secondList.size() > 0) ;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-04 11:10:03