Compare two List objects
import java.util.List;
public class CollectionUtil
{
public static boolean sameElements(List firstList, List secondList)
{
if (firstList.size() != secondList.size())
{
return false;
}
for (int index = 0; index < firstList.size(); index++)
{
// Check if the element is also in the second list.
if (!secondList.contains(firstList.get(index)))
{
return false;
}
}
// They heve the same elements.
return true;
}
}
Home
Java Book
Runnable examples
Java Book
Runnable examples
Collection List:
- Create a list from an array
- Add element to a list at specified index
- Append one list to another list
- Insert a list at Specified Index
- Compare two List objects
- Convert Collection to List
- Convert a Queue to a List
- Convert List to array
- Convert List to a Set
- Copy List to Vector
- Copy one List to Another List
- Fill all elements in a list
- Fill n Copies of Specified Object
- Find maximum element in a List
- Find Minimum element in a List
- Get element by index
- Get Enumeration over List
- Get Synchronized List from a List
- Get Sub List
- Loop a list by Iterator
- Loop a list by generic Iterator
- Loop a list with using ListIterator
- Loop through a ist in reverse direction using ListIterator
- Remove an element by value and by index from List
- Remove all elements from a List
- Remove duplicate items from a List
- Remove item from a List while looping
- Remove range of elements from a List
- Remove user object from a list
- Replace an Element of List
- Replace all occurrences of specified element in a List
- Replace an element in a List using ListIterator
- Reverse a List
- Reverse order of all elements in a List
- Rotate elements of a list
- Search a List for an item with contains method
- Search elements of a List
- Binary Search a Sorted List
- Binary Search for a non-existent element in a List
- Set to change(replace) the value in a list
- Shuffle elements in a List
- Shuffle(repeatable) a List
- Sort a list
- Sort a List in descending order using comparator
- Swap elements of List