How can I print HashMap<String, ArrayList<Integer>>? [duplicate] - java

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));

Related

How to map String variable to name of an integer? [duplicate]

This question already has answers here:
Get variable by name from a String
(6 answers)
Closed last year.
Suppose I have a String Variable like this in Java:
String cat = "felix";
int felix = 6;
Is there some way in Java that I could compare the contents of cat - "felix" to the name of the int variable felix?
There is no way to do it with the method you are doing, but you can use hashmap:
Map<String,Integer> myMap = new HashMap<>();
myMap.put("felix", 6);
myMap.put("another key", -4);
int value = myMap.get("felix");
System.out.println(value);//prints 6
value = myMap.get("abc");//throws null pointer exception

function to check string list in order give negative result [duplicate]

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.

how to get key and how to add more values? [duplicate]

This question already has answers here:
HashMap with multiple values under the same key
(21 answers)
Closed 4 years ago.
public static void main(String[] args) {
// write your code here
LinkedHashMap<String,String>category1=new LinkedHashMap();
category1.put("action","die hard");
Scanner s=new Scanner(System.in);
String answer=s.nextLine();
if (category1.containsKey(answer))
System.out.println(category1.get("action"));
if (category1.containsValue(answer))
System.out.println(category1.keySet());
How to get the key when the user answer with it's specific value, and how to add more values to one key?
1. The map collection, does not support multiple values under the same key, it will override whatever was stored there before.
2. However, you can change it from <String,String> to <String,List<String>>, thus gaining the ability to accumulate the answers from the client into the list. The key will refer to only one object, the list of Strings, but the list itself can hold many values.
3. In order to add more Strings to the list, you will need to retrieve the list by the desired key, and then add your new String to it.
Here is some code that implements the idea:
private void test(){
Map<String, List<String>> categories = new HashMap<>();
String answerFromClient = "Some text";
List<String> actionAnswers = categories.get("action");
if (actionAnswers == null){
actionAnswers = new ArrayList<>();
actionAnswers.add(answerFromClient);
categories.put("action",actionAnswers);
}
else{
actionAnswers.add(answerFromClient);
}
}

How set the first element in a spinner? [duplicate]

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)

How to apply a math function to each object of an ArrayList [duplicate]

This question already has answers here:
How to lowercase every element of a collection efficiently?
(11 answers)
Closed 6 years ago.
I'm having trouble figuring out how to apply a math operation to each item of an ArrayList. The ArrayList will be user inputted so there's no telling how many items would be within it. Is there a method that might aid in doing this?
Use ListIterator -> https://docs.oracle.com/javase/7/docs/api/java
/util/ListIterator.html
Unlike plain Iterator, ListIterator will allow you to store newly computed value back to the list
ArrayList<Integer> source = ...
ListIterator<Integer> iter = source.listIterator();
while( iter.hasNext() )
{
Integer value = iter.next();
Integer newValue = Integer.valueOf( value.intValue() * 2 );
iter.set(newValue);
}
as #puhlen says, in java 8, use stream and lambda expression
List liste = new ArrayList<>();
liste.stream().forEach(o->{
//apply your math on o
});
stream provides you many other functionnalities to filter, order, collect... use it if you're on java8
or as #neil-locketz says in java before 8 use a foreach loop
List<type> liste = new ArrayList<>();
for(type o : liste){
//math on object o here
}

Categories