This question already has answers here:
Sort List in reverse in order
(9 answers)
Closed 5 years ago.
in my app Android I have a set of data I want to bind to a spinner.
But of these I would like a particular value to be seen as the first in the spinner list.
public String [] getDescriptionCategories () {
Set <String> categories = products.keySet ();
String [] result = new String [categorie.size ()];
int i = 0;
for (String cat: categories) {
result [i ++] = cat;
}
return result;
}
The result is ["Altro","Prodotti","Utenti"], but I wish it was ["Utenti", "Prodotti", "Altro"]
How do I set it up?
You should use return Collections.reverse(result);. It will reverse your list. If you want to sort the list by alphabetical order, see this answer. Hope it helps :)
Use Collection class ie sort(List) and reverse(List)
Related
This question already has answers here:
How can I initialize an ArrayList with all zeroes in Java?
(5 answers)
Creating a list with repeating element
(5 answers)
Create an array with n copies of the same value/object?
(7 answers)
Closed 1 year ago.
Is there a way to fast initialize a new ArrayList object with X same objects?
Here is an example code:
private List<String> initStringArrayList(int size, String s) {
List<String> list = new ArrayList<>(size);
while (size-- > 0) {
list.add(s);
}
return list;
}
I want to have the same result, but much faster for large "size" values.
Of course, I could use this code:
private List<String> initStringArrayList(int size, String s) {
String[] array = new String[size];
Arrays.fill(array, s);
return new ArrayList<>(Arrays.asList(array));
}
But the constructor of ArrayList<>() would copy the full array instead of using it internal. That would not be acceptable.
Is there another way to do so? I need an ArrayList as result, not just a list. And it should be of any type, not just for strings.
Thank you for any answer!
Use Collections.nCopies, and copy it into an ArrayList:
private <T> List<T> initStringArrayList(int size, T s) {
return new ArrayList<>(Collections.nCopies(size, s));
}
This assumes that you really do want a mutable List at the end. If you can make do with an immutable list with the item size times, Collections.nCopies(size, s) by itself would work: it is memory-efficient, fast to allocate etc.
This question already has answers here:
What happens when an object is assigned to another object
(4 answers)
Why are two empty ArrayLists with different generic types equal?
(4 answers)
Closed 4 years ago.
I have the below function to check if it is in order
public boolean order(List<String> value) {
List<String> tmp = value;
Collections.sort(tmp);
return tmp.equals(value);
}
Test:
assertTrue(route.order(Arrays.asList("a", "s", "d")));
assertFalse(route.order(Arrays.asList("a", "k", "c")));
but fail at 2nd test, why is it not false?
Here in below line:
List<String> tmp = value;
You are just copying reference of value list argument in tmp list and hence you are sorting on tmp and indirectly value list which is one and the same.
To solve the problem change the assignment of tmp variable to:
List<String> tmp = new ArrayList<>(value);
public boolean order(List<String> value) {
List<String> tmp = new ArrayList<>(value);
Collections.sort(tmp);
return tmp.equals(value);
}
Your asserts are negative to each other.
Both arrays are sorted. Maybe you wanted ("A", "C", "B").
Also, as the fellows said before, your sort is on the original list, you have to copy it first and then to sort.
This question already has answers here:
What is a raw type and why shouldn't we use it?
(16 answers)
Closed 6 years ago.
I have tried this
ScreenDumpParser dump = new ScreenDumpParser();
Map btn_bound = dump.parse();
Iterator iterator = btn_bound.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next().toString();
List<Integer> value = btn_bound.get(key);
System.out.println(key);
}
but this line
List<Integer> value = btn_bound.get(key);
gives error:
Type mismatch: cannot convert from Object to List<Integer>
I need to print all the values along with the key in one single row.
If you want to add a value to your List you should use:
value.add( btn_bound.get(key));
and if the button is a list or something else you must add more then one value to your list with
value.addRange( btn_bound.get(key));
And if you want to get a value:
Object foo = value.get(btn_bound.get(key));
This question already has answers here:
How do I remove repeated elements from ArrayList?
(40 answers)
Closed 9 years ago.
I have a very lengthy ArrayList comprised of objects some of them however, are undoubtedly duplicates. What is the best way of finding and removing these duplicates. Note: I have written a boolean-returning compareObjects() method.
Example
List<Item> result = new ArrayList<Item>();
Set<String> titles = new HashSet<String>();
for( Item item : originalList ) {
if( titles.add( item.getTitle() )) {
result.add( item );
}
}
Reference
Set
Java Data Structures
You mentioned writing a compareObjects method. Actually, you should override the equals method to return true when two objects are equal.
Having said that, I would just return a new list that contains unique elements from the original:
ArrayList<T> original = ...
List<T> uniques = new ArrayList<T>();
for (T element : original) {
if (!uniques.contains(element)) {
uniques.add(element);
}
}
This only works if you override equals. See this question for more information.
Hashset will remove duplicates. Example:
Set< String > uniqueItems = new HashSet< String >();
uniqueItems.add("a");
uniqueItems.add("a");
uniqueItems.add("b");
uniqueItems.add("c");
The set "uniqueItems" will contain the following : a, b, c
This question already has answers here:
How to convert comma-separated String to List?
(28 answers)
Closed 9 years ago.
I have a comma separated String which i need to convert to ArrayList .
I tried this way
import java.util.ArrayList;
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
String CommaSeparated = "item1 , item2 , item3";
ArrayList<String> items = (ArrayList)Arrays.asList(CommaSeparated.split("\\s*,\\s*"));
for(String str : items)
{
System.out.println(str);
}
}
}
Its giving me the Runtime Error as shown
Exception in thread "main" java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
at com.tradeking.at.process.streamer.Test.main(Test.java:14)
as i was trying to convert an List to arrayList by force .
The ArrayList returned by Arrays.asList is not java.util.ArrayList. It's java.util.Arrays.ArrayList. So you can't cast it to java.util.ArrayList.
You need to pass the list to the constructor of java.util.ArrayList class:
List<String> items = new ArrayList<String>(Arrays.asList(CommaSeparated.split("\\s*,\\s*")));
or, you can simply assign the result:
List<String> items = Arrays.asList(CommaSeparated.split("\\s*,\\s*"));
but mind you, Arrays.asList returns a fixed size list. You cannot add or remove anything into it. If you want to add or remove something, you should use the 1st version.
P.S: You should use List as reference type instead of ArrayList.
You can't just cast objects around like they're candy. Arrays.asList() doesn't return an ArrayList, so you can't cast it (it returns an unmodifiable List).
However you can do new ArrayList(Arrays.asList(...));
Does it absolutely need to be an ArrayList? Typically you want to use the most generic form. If you're ok using a List just try:
List<String> items = Arrays.asList(...);
You can still iterate over it the same way you currently are.
Please try to use bellow code.
String[] temp;
/* delimiter */
String delimiter = ",";
/*
* given string will be split by the argument delimiter provided.
*/
temp = parameter.split(delimiter);
ArrayList<String> list = new ArrayList<String>();
for (int l = 0; l < temp.length; l++)
{
list.add(temp[l]);
}
String CommaSeparated = "item1 , item2 , item3";
ArrayList<String> items = new ArrayList(Arrays.asList(CommaSeparated.split("\\s*,\\s*")));
for(String str : items)
{
System.out.println(str);
}