This question already has answers here:
Why not always use ArrayLists in Java, instead of plain ol' arrays?
(5 answers)
Closed 8 years ago.
I've played around with arrays and arrayLists. Sorting is a breeze with arrayLists but I can not say the same with arrays. You have to make a for loop and filter the results and it's a pain. As far as I can see arrayList are just a better version of arrays. So why use arrays? Is there something they can do better.
Sorting with arrays and ArrayLists is equally easy: there are already implementations for sorting both of them:
Sorting ArrayLists
Sorting Arrays
The reasons for using one of the other are more of memory concerns, speed concerns or because your problems is better suited to be solved with one or the other.
Related
This question already has answers here:
What is difference between Collection.stream().forEach() and Collection.forEach()?
(5 answers)
Closed 5 years ago.
Starts from java 8 to iterate throug list I can use both:
List list = new ArrayList();
1. list.forEach(...)
2. list.stream().forEach(...)
Is it any advantages of using second case? To convert list to stream?
There are no advantages of using the second case, unless you have a parallel stream. There is a disadvantage, namely that Stream.forEach() doesn't guarantee to respect encounter order. A more accurate (but still unnecessary) equivalent would be Stream.forEachOrdered().
No, in theory, the second option is worse than the first one - you pay the cost of instantiation/garbage-collection and calling a Stream instance and don't really get any benefit in return.
Additionally, in theory the iteration order of Stream.forEach() isn't deterministic.
This question already has answers here:
Is there a common Java utility to break a list into batches?
(21 answers)
Closed 6 years ago.
I have a list that may be over 1000 strings, however I do not know how many exactly.
What is the best way to split this list into smaller lists without loosing any members of the list?
For example If I have a list of 1323 members, how can I best split it into 3 almost evenly sized lists?
I have seen the Guava and Commons way of splitting lists by the partition function, but that function will split the list into given size of chunks and not given number of groups (sub-lists).
Guava has a function Lists.partition which will do this for you
Usage:
Lists.partition(mylist, mylist.size()/3);
This question already has answers here:
Java: Difference Between a collection and 'Data Structure' [closed]
(3 answers)
Closed 7 years ago.
I want to learn data structures and algorithms in java but I'm confused if collections are data structures or if there is something else . A brief explanation on data structures would help.
Thanks
Collections are data structures! Data structures are just advanced ways to hold simple data (simple being integers, strings etc.) data structures being things like Arrays, Collections.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In Java, how can I test if an Array contains a certain value?
I want to check for a string S whether it is present in string array or not. Is there any direct method to do so ?
EDIT
If the answer excludes the use of List then that would be better.
There are a couple of ways to accomplish this using the Arrays utility class.
If the array is not sorted:
java.util.Arrays.asList(theArray).indexOf(o)
If the array is sorted, you can make use of a binary search for performance:
java.util.Arrays.binarySearch(theArray, o)
See here Where is Java's Array indexOf?
You can convert it to List and check: Arrays.asList(arrayVariable).contains(S)
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Generating all permutations of a given string
I have an array of arbitrary length in Java, and I would like to generate all possible permutations of them. The easy way to do this for a fixed length would be a series of nested for loops, but because the array is of unknown length, that is not an option here. Is there a straightforward way to accomplish this in Java?
Use a recursive function, instead of loops. Each time you call the method should be on a smaller portion of the array and stop when length = 0. This link should help you design your function.
It may or may not be optimal as far as performance goes, but if you're looking for a way to do it with writing relatively little code and having it be clear and maintainable, you want a recursive method rather than nested loops.