As the title implies, I'd like to know how to insert different values into my ArrayList, without using too much space for several "add" functions.
ArrayList<Integer> arr = new ArrayList<Integer>(Arrays.asList(3,4));
ArrayCheck.allDivisibleBy(arr, divisor);
I have got an arraylist called arr, and I don't know if this is the right way to add several values (3,4) that way.
Furthermore I would also like to check the values in another method called allDivisbleBy. The function of this method is not relevant though, but I want to check the values and am not sure if "ArrayCheck" is a way to send the array values to the method.
The simplest way is to use Arrays.asList(arr). As expected, that static method returns a List with as it's contents the elements of the array.
I don't know if this is the right way to add several values (3,4) that
way
Yes it is. Else you can use this:
ArrayList<Integer> arr = new ArrayList<Integer>(2);
arr.add(3);
arr.add(4);
I want to check the values and am not sure if "ArrayCheck" is a way to
send the array values to the method
ArrayCheck is not a standard java class.
Check all of your inputs for your condition, put them in a Collection and use method addAll instead of add to add all of collection items at once.
Related
My database returns a Flowable like so:
Flowable<List<Users>> users = MyActivity.myDatabase.myDAO().getUsers();
I would like to quickly and easily convert it to a list so I can count it, iterate through it and perhaps pass it as an ArrayList. What's the best way to do that using Java?
You will have to use a blocking call to actually retrieve the values. However, it's not clear from the question whether you are expecting a single list of users or a list of user lists.
If you are expecting a single List<Users>, you can do this:
List<Users> usersList = users.blockingFirst();
If you are expecting a list of user lists, you would need to do something like this:
List<List<Users>> userLists = users.toList().blockingGet();
In either case, if you specifically need an ArrayList, you will have to construct one and copy over the elements, as there is no telling exactly what kind of List implementation the above codes return. (You can just do, for instance, new ArrayList<>(users.blockingFirst()) in the first case and something similar in the second.)
I am just curious about the Lists class implementation of google guava, we have two methods to create List from Array,
Lists.asList(E first, E[] rest)
Lists.asList(E first, E second, E[] rest)
Why do these methods has first and rest separately? cant it be like Arrays.asList implementation of java ?
The only thing I am able to see is the first and second is nullable and rest is not nullable
Can anyone help to understand this ?
Ok, so the job of the Lists.asList() is not exactly to directly convert an array to a list.
Suppose we have an array, and we want to insert an element to it, we can’t do it as the array size is fixed. One solution to this problem is to allocate an new array of +1 the size of the original array and copy all elements from the original array to the new array. This works but it is highly inefficient.
Guava provides an efficent solution to this problem – Guava’s Lists.asList() method returns an unmodifiable list backed by the original array which also contains the specified element.
source: https://www.techiedelight.com/guava-lists-class-java/
So basically, you can use it to just convert an array by giving the first and/or second parameters (depending upon what method you are using) as null, and giving the "rest" parameter as your array.
This will return your array as list, perhaps with null as the first index (and second as well, depending on what you are using)
But if you want, you can use the same methods to get a list with some specific data appended to your array (at first and second index values)
Hope this was helpful!
The main reason these methods exist is to help you when you write a method with a signature like
void foo(Bar first, Bar... rest);
which is something you'd do when you want to allow the user to call the method as if it were a method with just a varargs parameter, but you want to require that it be called with a minimum of one or two arguments (whereas varargs alone would allow them to call it with zero). In the implementation of such a method, you might want to treat those arguments as a single list. Lists.asList does that without doing any array copying:
void foo(Bar first, Bar... rest) {
List<Bar> bars = Lists.asList(first, rest);
// ...
}
I am sure I missed something in my reading but I can't figure this out. My program gets a list of image files from a directory and then selects files at random to display as part of the game. The problem is, every time I have ran it using a random number generator it has duplicate numbers. In reading I saw Collections and I think that will work for me as it randomly shuffles the list. This eliminates the need for a random number generator. My problem is that I can not figure out how to get it to shuffle the File[].
This is the last thing I tried
comboFile = filePath.listFiles();
List listShuffle = new List();
listShuffle = (List) Arrays.asList(comboFile);
Using list without gets rid of that error but now the Collections.shuffle method is giving me an error. No suitable nethod found for shuffle(java.awt.list)
Collections.shuffle(listShuffle);
How do I turn the File array into a list to shuffle?
The problem seems to be that listShuffle is never used after shuffling it. You are still reading the values from the comboFile array, which is never shuffled.
Also, it should be Arrays.asList() instead of Array.asList().
You're using the wrong type. Change your import to java.util.List instead of java.awt.List. The error is telling you there is no method that accepts the list type you're passing in.
And yes, you probably want to create the list using Arrays.asList() like the other guys suggested.
Please try Arrays.asList() function.
It will make a wrapper of the array. And call shuffle() function, which will change the inside array elements' order.
Using Guava, is it possible to add new items to a Collection instaed of only transforming a Collection?
Imagine e.g. I have
Set<Integer> numbers = Sets.newHashSet(1,2,3);
Now, in addition to the already present numbers, also the double and triple of each number shall be included, therefore 2,4,6 as well as 3,6,9.
Is there something in Guava like
addToSet(numbers, <a function returning a List of values for each element in numbers>)
?
Thanks for any hint!
Why would something like Set.addAll(Collection c) not suffice?
Set<Integer> numbers = Sets.newHashSet(1,2,3);
numbers.addAll(setReturningFunction());
Decorate the Set (could use a ForwardingSet) and then override the add method to add the additional items into the backing delegate.
transformAndConcat does the job, allows returning more than one element, though only one element was passed to the transform function.
I know this is easy and can be done with 2 lines of code, but i am curious to know if there exists any such function
i have a int which tell me the size of list and i need to create a list say
List<Integer> intList;
i can create this by easily iterating through the size something like
for(int i=1 ; i <= size; i++) // started with 1 as i want it from 1
{
fill list
}
but i was just thinking as if there exists any such methods either in Collection API or Apache common
where i can pass the size to get a List with given size
Edit
May i was not able to put question in proper way, i want to get filled my list say
if size=4 than i was thinking abt something
Integer=1
Integer=2
Integer=3
Integer=4
and not an empty list with size 4
i know question do not make much sense, but still its better to clear your questions
Short answer: No
The two-liner you're currently using is already optimal.
The thing here is that List is an interface class and you can't create instances of an interface class. So before you want to construct it you need to know what kind of List you want to create. For the moment let's assume you want an ArrayList. From this moment on you can simply use the correct constructor to initialize your list e.g.
List<Integer> intList = new ArrayList<Integer>(10);
Which constructs an ArrayList of initial capacity 10.
For other kinds of list you can check the Java documentation.
To fill the list with initial data you can do something like this:
int[] myArray = new int[]{ 58,63,67,72,70,63,62,63 };
List<Integer> intList = new ArrayList<Integer>(myArray );
To answer the question after what you've added with your edit: No, there's no such method to fill a list with ascending integers in the standard collections API. You'll have to program a loop yourself and add elements to the list.