How to initialize a vector with an array in Java? [duplicate] - java

This question already has answers here:
Java Convert Object[] Array to Vector
(6 answers)
Closed 9 years ago.
Do I have to push my elements one by one? I tried something like
String[] array;
array=...
Vector<String> vector = new Vector<String>(array);
but my eclipse marks this as an error.

Vector doesn't have a constructor that accepts an array directly.
Assuming that array is of type String[], you could do
Vector<String> vector = new Vector<String>(Arrays.asList(array));
Better to use ArrayList as it doesn't have the overhead of having synchronized methods. You could use
List<String> list = new ArrayList<>(Arrays.asList(array));
This will produce a mutable collection also.

That can't work, since, as the documentation shows, there is no Vector constructor taking an array as argument.
If you just want a non-modifiable list, use
List<String> list = Arrays.asList(array);
If you really want a Vector (but you should use ArrayList instead, because Vector is obsolete), use
Vector<String> vector = new Vector<String>(Arrays.asList(array));

I'm not 100% sure what you mean by 'one by one'. If you want to add an existing collection to a Vector, you could use this.
If you want to do it one by one, you need to iterate through the items, and call Vector's 'add' method.
for(String item: array) {
vector.add(item);
}

Related

I cannot add in a List [duplicate]

This question already has answers here:
UnsupportedOperationException when trying to remove from the list returned by Array.asList
(6 answers)
Closed 3 years ago.
I have next code:
List<String> str=Arrays.asList("cat","tiger","dog","mouse");
str.add("horse");
It compiles, but at runtime I have UnsupportedOperationException. Why does it happen?
Arrays.asList(String...) creates an unmodifiable array. Wrap it with another ArrayList like so:
List<String> str = new ArrayList<>(Arrays.asList("cat","tiger","dog","mouse"));
str.add("horse");
this called backed list .
backed list it's created when you convert an array to list but keep in your mind the element in the array is linked with the element in the list so you cant add or delete any thing and you are using varargs its similar to array ,
Arrays.asList() returns a list that is fixed-size and backed by the array you pass so you can't add or remove elements because that would mean changing the array as well. (note that if you have a look at the source you'll find that Arrays.asList() will return an instance of java.util.Arrays.ArrayList which you should not confuse with java.util.ArrayList which you probably already know).
Instead you'd need to create another list, e.g via calling new ArrayList<String>( Arrays.asList(...)) which effectively makes a copy of the passed list.
A Java 8+ way might be this:
List<String> str = Stream.of( "cat","tiger","dog","mouse" ).collect( Collectors.toList() );

Benefits of creating a List using Arrays.asList() [duplicate]

This question already has answers here:
Why does Arrays.asList() return its own ArrayList implementation
(6 answers)
Closed 5 years ago.
Referring to Difference between Arrays.asList(array) vs new ArrayList<Integer>(Arrays.asList(ia)) in java
I was curious as in what's the exact purpose of Arrays.asList() method.
When we create a new List from it, say for example -
Integer[] I = new Integer[] { new Integer(1), new Integer(2), new Integer(3) };
List<Integer> list1 = Arrays.asList(I);
List<Integer> list2 = ((List<Integer>) Arrays.asList(I));
We cannot perform most of the the regular operations on it like .add(), .remove(). Thus, I was not able add an iterator to it to avoid concurrent modification.
Oracle docs state
public static List asList(T... a)
Returns a fixed-size list backed by the specified array. (Changes to
the returned list "write through" to the array.) This method acts as
bridge between array-based and collection-based APIs, in combination
with Collection.toArray(). The returned list is serializable and
implements RandomAccess.
It works well with creating a new List. List<Integer> list3 = new ArrayList<>(Arrays.asList(I));
So, why this and what are its advantages and disadvantages?
Not being able to call add, remove, etc is the exact difference. If you don't need those methods, Arrays.asList gives you a perfectly fine view of the array as a List (for APIs that take collections rather than arrays). If you need to change the "shape" of the list, then new ArrayList<>(Arrays.asList(myArray)) is the way to go.

List.addAll throwing UnsupportedOperationException when trying to add another list [duplicate]

This question already has answers here:
Java List.add() UnsupportedOperationException
(8 answers)
Closed 3 years ago.
List.addAll throwing UnsupportedOperationException when trying to add another list.
List<String> supportedTypes = Arrays.asList("6500", "7600"};
and in loop I am doing,
supportedTypes.addAll(Arrays.asList(supportTypes.split(","))); //line 2
reading supportTypes from a file.
But line 2 throws a UnsupportedOperationException, but I am not able to determine why?
I am adding another list to a list, then why this operation is unsupported?
Arrays.asList returns a fixed sized list backed by an array, and you can't add elements to it.
You can create a modifiable list to make addAll work :
List<String> supportedTypes = new ArrayList<String>(Arrays.asList("6500", "7600", "8700"));
This error also happens when the list is initialized with Collections.emptyList(), which is immutable:
List<String> myList = Collections.emptyList();
Instead, initialize it with a mutable list. For example
List<String> myList = new ArrayList<>();
Arrays.asList returns a fixed-size list.
If you to want be able to add elements to the list, do:
List<String> supportedTypes = new ArrayList<>(Arrays.asList("6500", "7600"});
supportedTypes.addAll(Arrays.asList(supportTypes.split(",")));
Problem is that Arrays.asList method returns instance of java.util.Arrays.ArrayList which doesn't support add/remove operations on elements. It's not surprizing that addAll method throws exception because add method for java.util.Arrays.ArrayList is defined as:
public void add(int index, E element) {
throw new UnsupportedOperationException();
}
Related question:
Arrays.asList() Confusing source code
From the documentation:
Arrays.asList returns a fixed-size list backed by the specified array.
In my case this exception occured when I called adapter.addAll(items), where adapter was a custom ArrayAdapter. This CustomAdapter had a parameter of type Array instead of ArrayList.

How to quickly and conveniently create a one element arraylist [duplicate]

This question already has answers here:
Initialization of an ArrayList in one line
(34 answers)
Closed 6 years ago.
Is there a Utility method somewhere that can do this in 1 line? I can't find it anywhere in Collections, or List.
public List<String> stringToOneElementList(String s) {
List<String> list = new ArrayList<String>();
list.add(s);
return list;
}
I don't want to re-invent the wheel unless I plan on putting fancy rims on it.
Well... the type can be T, and not String. but you get the point. (with all the null checking, safety checks...etc)
Fixed size List
The easiest way, that I know of, is to create a fixed-size single element List with Arrays.asList(T...) like
// Returns a List backed by a varargs T.
return Arrays.asList(s);
Variable size List
If it needs vary in size you can construct an ArrayList and the fixed-sizeList like
return new ArrayList<String>(Arrays.asList(s));
and (in Java 7+) you can use the diamond operator <> to make it
return new ArrayList<>(Arrays.asList(s));
Single Element List
Collections can return a list with a single element with list being immutable:
Collections.singletonList(s)
The benefit here is IDEs code analysis doesn't warn about single element asList(..) calls.
Collections.singletonList(object)
the list created by this method is immutable.
You can use the utility method Arrays.asList and feed that result into a new ArrayList.
List<String> list = new ArrayList<String>(Arrays.asList(s));
Other options:
List<String> list = new ArrayList<String>(Collections.nCopies(1, s));
and
List<String> list = new ArrayList<String>(Collections.singletonList(s));
ArrayList(Collection) constructor.
Arrays.asList method.
Collections.nCopies method.
Collections.singletonList method.
With Java 7+, you may use the "diamond operator", replacing new ArrayList<String>(...) with new ArrayList<>(...).
Java 9
If you're using Java 9+, you can use the List.of method:
List<String> list = new ArrayList<>(List.of(s));
Regardless of the use of each option above, you may choose not to use the new ArrayList<>() wrapper if you don't need your list to be mutable.
With Java 8 Streams:
Stream.of(object).collect(Collectors.toList())
or if you need a set:
Stream.of(object).collect(Collectors.toSet())
The other answers all use Arrays.asList(), which returns an unmodifiable list (an UnsupportedOperationException is thrown if you try to add or remove an element). To get a mutable list you can wrap the returned list in a new ArrayList as a couple of answers point out, but a cleaner solution is to use Guava's Lists.newArrayList() (available since at least Guava 10, released in 2011).
For example:
Lists.newArrayList("Blargle!");
Very simply:
Arrays.asList("Hi!")
Seeing as Guava gets a mention, I thought I would also suggest Eclipse Collections (formerly known as GS Collections).
The following examples all return a List with a single item.
Lists.mutable.of("Just one item");
Lists.mutable.with("Or use with");
Lists.immutable.of("Maybe it must be immutable?");
Lists.immutable.with("And use with if you want");
There are similar methods for other collections.
Yet another alternative is double brace initialization, e.g.
new ArrayList<String>() {{ add(s); }};
but it is inefficient and obscure. Therefore only suitable:
in code that doesn't mind memory leaks, such as most unit tests and other short-lived programs;
and if none of the other solutions apply, which I think implies you've scrolled all the way down here looking to populate a different type of container than the ArrayList in the question.

How do I iterate over multiple lists in parallel in Java? [duplicate]

This question already has answers here:
How to most elegantly iterate through parallel collections?
(8 answers)
Closed 7 years ago.
I want to create a function to iterate over multiple lists. Now I know these lists have exactly the same size (and they can have different types too), for example:
List<Integer> list1 = getList1();
List<String> list2 = getList2();
List<Long> list3 = getList3();
list1.size() == list2.size(); // returns true
list2.size() == list3.size(); // returns true
And I want to be able to call a function which takes 3 elements at the same slice in each of these lists so for exemple:
int calculate(int elemList1, String elemList2, long elemList3) {...}
// iterator over the lists in parallel {
int ret = calculate(elemList1, elemList2, elemList3);
// }
I would like to do the equivalent of what I saw discussed in guava here but doesn't look implemented yet: http://code.google.com/p/guava-libraries/issues/detail?id=677
They talk about doing Iterators.interleave or Iterators.zip and I would like to do something similar but I haven't been able to, so can someone please help me a bit? Thanks!
I would prefer to not have to get the size of one list and iterate over them by index, because in the future i can have lists of different sizes so i would like to use only 1 way to do this.
A compound Iterator might be a cool idea, e.g.:
Iterator<Array<?>> compoundIterator = createIterator(List1, List2, List3);
Then inside the implementation, you would create iterators for each of the lists, then loop through the items and put them into an array, then your consumption of that stuff would look something like:
while (compoundIterator.hasElements()){
Array[] elements = compountIterator.nextElement();
calculate(elements[0], elements[1], elements[2]);
}
What's nice about this solution is you are hiding all those details about whether one list ran out or not (of course you have to decide what you want to do if one does, but that could be wrapped inside as well).
I don't think you are really saying in parallel, since the values are being used to invoke a method. If you want the same elements from each list, concurrently skipping through different lists on different threads does you no good.
You just need to do a for loop and then call list1.get(i), list2.get(i), list3.get(i).
You can create a new thread and iterate your list there. Spawn multiple of this therad and you can iterate your list in parallel.
If you want to pass List of any template type, you can just specify your method parameter as List, although this might result in compiler warnings. Other thing you can try is pass the list as List<T extends Object> and do a runtime check of type T and action accordingly
However if by 'parallel' you are not referring to multithreading / concurrency -- instead just want to be able to iterate your 3 lists in one single loop, then something like this will do (warning code is rough example only -- not tested / complying with coding standard):
List list1 = ...
List list2 = ...
List list3 = ...
for(int i=0,j=0,k=0; i<list1.size() && j<list2.size() && k<list3.size(); ++i,++j,++k)
{
Object elemOfList1 = list1.get(i);
Object elemOfList2 = list2.get(j);
Object elemOfList3 = list3.get(k);
// do something here
}

Categories