Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have seen some posts regarding comparing two lists in Java, but none of them relate to ordered lists - I care about values at certain indices being equal.
Instead of writing brute-force code to iterate through both lists, do JUnit or Hamcrest have any util methods to compare ordered "expected" list with ordered "actual" list?
In my case they are both lists of String, but how would this be achieved if they were lists of customObj ?
I want to see all differences, not just stop after the first difference.
AssertJ library or Hamcrest amtchers will help you with the meaningful comparison.
junit's assertEquals works on lists, if the members of the list have equals implemented. String has a working equals method, so assertEquals will work for you
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I need to program an application in Java language that has a pre-defined number of categories,
but the exact items in each category are unknown. Which one of the following data
structures I would use and why? Array, singly linked list, or doubly linked list
I'd go with a map that contains a List, the implementation is irrelevant.
Map<Category, Collection<Item>> categoryMap
If the categories are ordered, use a TreeMap
If they're not ordered, use a Hashmap
If the items are known to be unique I'd have the Collection be a Hashset;
If the items are unique and ordered, I'd use a Treeset;
If they're non-unique, I'd use an ArrayList.
I see no reason to use a LinkedList of any variety if the number of entries are known. The reason to use LinkedLists is if there's to be many insertion/removal operations.
In almost all use cases an ArrayList is the superior implementation to use. The only real downside to an ArrayList is if it's gets modified often, especially added to as it uses a "double up" method for resizing the internal array. Basically, if it needs more room, it doubles the size of the existing array and then copy the items from the old array into the new array. That can cause memory issue for large lists, but you'd need to be dealing with thousands of entries.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a declaration as List<List<byte[]>>, i.e. inner list is list of byte arrays and there are N such lists. I want to identify common byte arrays across all the lists. How would I do that? There is a discussion here, but I donot know if it works for byte arrays
Use the solution you've linked, but as arrays don't have hashCode overriden, you have to wrap them in ByteBuffer, using ByteBuffer.wrap. To extract the array from a ByteBuffer, call array on it.
You can use equals(byte[] a, byte[] a2), as suggested here.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I need to count the frequency of an element in arrays.
I used the method
Collections.frequency(Arrays.asList(arr),element);
but I get zero all the times
any ideas ?!
If you are ArrayList consists of elements of custom type
example person bean, or employee object.
Make sure you have overridden equals() method and hash() methods
if you have not overridden these methods that Collection method wont work.
You need to give details about "arr" & element. However, I did came across this some time back when I tried to use an array of primitives such as int[], converting them to a List using Arrays.asList()
There is nothing like List of "int". An Integer would work however, Integer arr[] = {1,1,1,1,3,3,4,5,5,5,6};
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Java two collections contains any
Large two collections A and B contain best best approach in these
1) Collections.disjoint(A, B)
2) org.springframework.util.CollectionUtils
CollectionUtils.containsAny(A,B)
Looking at their respective source codes, it looks like Collections.disjoint is smarter about when its arguments are Sets and have fast contains implementations, and CollectionUtils is a little smarter about checking if either collection is empty, but that's generally a smaller win.
Based on that, I'd use Collections.disjoint, which I'd tend to do anyway just because it's built-in.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
As we have many Sorting algorithms,I wanted to select the proper sorting algorithm for my case.
For Ex:Insertion sort is best for Small case of numbers ,whereas Merge sort is best suited for Large case of numbers.
I dont know what is that small range of numbers means .i.e 1-100 or 1-1000 or so.
Probably what is the best case for sorting a list of numbers where the same set of numbers are present present repeatedly.I am planning to store it in a hash and then store the elements accordingly .
Whether doing in through hash is a better way or Using some sorting algorithm is the best way
But as here it contains the same data again and again
If you add some elements to already sorted array(list), then you have only small number of inversions. In this case insertion sort will work rather fast.
Alternatives - natural merge sort or TimSort(if implementation is available for your language) - these ones will behave good in all cases, including unsorted arrays.