Search without iterator for Map [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is it possible to search without iterator without using map.
Map<String,Short> map = new HashMap<String,Short>();
map.put("String2", (short)2);
map.put("String1", (short)1);
map.put("String3", (short)4);
I am looking for a way to get the value based on the key (Return 2 for value String2). Is Map the right one to use in this scenario.
Thanks !

Returning values based on a key is precisely what maps are for.
To retrieve your value you can simply do:
short returnVal = map.get("String2");

Related

Scala Iterable[(String, Long)] to SortedMap[String,Long] or TreeMap[String, Long] sorted in decreasing order of value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
object Test extends App {
val i: Iterable[(String, Long)] = List(("a", 1), ("b", 2))
val sortedMap: SortedMap[String, Long] = i.toList.sortBy(_._2)
}
I don't want to convert Iterable to List/Array etc since it's coming form a jdbc query.
You can't do that. SortedMap sorts by keys, not values.
If you want it sorted by value, you gotta use ListMap, and can't avoid converting to List:
ListMap(i.toList.sortBy(-_._2):_*)
There isn't really too much wrong with converting to list, since you are loading the whole thing in memory anyway. This is faster too, than building a tree one element at a time.

Target Variable based on String in Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I need to allow the user to target a variable using a string name. Example,
int number = 5;
String variableTarget = "number";
I need to target the int, number, and then change its value in later code. Any way?
I cannot think of any way to do it. There seems to be alot of class targeting, but I already have that.
I don't think that there is a way to do this in java. But, to have such a functionality, you can use a map to assign the value to the number key in the Map :
HashMap <String, Integer> myMap = new HashMap<>();
myMap.put("number", 5);
and you can alter the value later using the same put statment :
myMap.put("number", myMap.get("number")+1);
You can use Java Reflections, or create another structure like a HashMap answered by #DodgyCodeException

Why we can't put null key in linkedHashMap in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
We can put single null key in hashMap. but in linkedHashMap we can't pull null key. what is the reason behind this.
You can put a null key in a LinkedHashMap. Example:
Map<String, String> m = new LinkedHashMap<> ();
m.put(null, "a");
System.out.println(m.size());
System.out.println(m.get(null));
prints 1 and a.
Following documentation
This class provides all of the optional Map operations, and permits
null elements.

Java stream alternative to LambdaJ index [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
What is Java stream API alternative to LambdaJ indexing? Let's say I have code like this
List<Product> products = ...
Map<Month, Product> productsOnMonths = Lambda.index(products, Lambda.on(Product.class).getMonth());
Where I know that every product has unique month attribute.
products.stream().collect(Collectors.toMap(Product::getMonth, s -> s));
The difference here is that Collectors.toMap can take a third argument that says how to merge two entries when they are the same; I don't think lambdaj offers that

Getting Resource from strings [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've got many resources with names based on a pattern:
R.drawable.small_house_red
R.drawable.small_house_blue
R.drawable.big_house_black
R.drawable.small_tree_red
I've got 3 strings: size, type, color. I can put all required resources to map, and get them by concated string but is it possible to get them without manually entering all combinations to the map?
You can use this
int id = context.getResources().getIdentifier("imageNameAsString", "drawable", context.getPackageName());

Categories