I need to make an int array using Strings instead of ints.
EX: int["number2"] = 0; instead of int[2] = 0;
Does anyone know how to do this?
Thanks for your time.
you could use a HashMap - see here for more info!
Java doesn't support associative arrays, but you could use a HashMap:
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("key1", 25);
map.put("key2", 4589);
map.get("key1") will return 25.
You are not looking for an array but for an associative array.
In Java, in practice, every class that implements Map can be used as an associative container, since they can map keys to values (TreeMap<K,V>, HashMap<K,V>, and so on)
This syntax looks very like a map in Groovy, In Java, you could use something like a Map<String, Integer>.
Related
Is there a java structure like HashMap but that allows to repeat the key as well as the value ?
For example:
HashMap<Integer, String> hmap = new HashMap<>();
map.put(1, "Sport");
map.put(1, "Football");
map.put(2, "Football");
map.put(1, "Handball");
And as a result:
<1,"Sport">
<1,"Football">
<2,"Football">
<3,"Handball">
Any help would be most appreciated.
Thanks,
Nadhmanovic
There is not a structure like this built into the language. But you can use Guava's Multimap.
would using the
Map<Key, List<Value>>
help?
A List holding a object that has a Integer and a String can work for you and will also keep the order added.
But if you want to find all elements with the same key you will need a for, or a structure like Map<Integer, List<String>>
You can use MultiMap<Character,Boolean> because it allows duplicate key which exist in org.apache.commons.collections package.
or
You can use ArrayList and add the objects of the Class to the same key.
Hope this helps!
I would like to initialize an Array of pairs but my way is not fully correct.
Here how I first wrote it:
Pair<String, Integer>[] pair = new Pair[5];
It is accepted and it works but there is still the following warning:
"Unchecked assignment: 'android.util.Pair[]' to 'android.util.Pair<Java.lang.String, Java.lang.Integer>[]'...
I already tried to do like this:
Pair<String, Integer>[] pair = new Pair<String, Integer>[5];
but it doesn't work.
It is because of the nature of generics.
My suggestion is to drop the idea of using arrays directly, and use a List<Pair<String, Integer>> instead. Under the hood, it uses an array anyway, but a List is more flexible.
List<Pair<String, Integer>> list = new ArrayList<Pair<String, Integer>>();
// You don't have to know its size on creation, it may resize dynamically
or shorter:
List<Pair<String, Integer>> list = new ArrayList<>();
You can then retrieve its elements using list.get(index) whereas you would use list[index] with an array.
You can not create an array of generified type, in this case Pair. That's why your first solution works, because you did not specify the concrete type of Pair.
Technically, you can create an array, Generic arrays in Java, but it's not reccomended.
Like String s="sample" in java.How to declare and assign values to a hashMap in one step. Also is it possible to assign more set of values at a time using put function in hashMap.
Yes, it is possible. you can use the below code
HashMap<String,String> instruments = new HashMap<String, String>() {
{
put("test","test");
put("test1","test1");
}
};
Use a library like Google Guava which has lots of utilities to instantiate HashMaps. It is also possible doing anonymous inheritance like this:
Map<String, Object> map = new HashMap<String, Object>() {{
put("Test", "Test1");
put("Test", "Test1");
}};
But I wouldn't recommend it.
such constructs do not exist in good ol' java.
On one hand, you can use property files format for that. You can save your map as a something-separated key-value pairs in a string or a file, and read them in a loop filling your map with each pair.
on the other hand, if you really need that + possible type-checking, you can look at modern dynamic JVM languages, like Groovy or Scala.
there you can use the code as it is:
def map = [ a:1, b:23, c:"aasdasd" ]
So this should be really simple since I know it's possible (I just don't understand 'Set' very much).
So basically there is this TreeMap, let's call it aTree. So I need to do something like:
somethingHereProbably = aTree.keySet();
somethingHereProbably.toStringArray();
You can do
Map<String, Object> map = ...
String[] strings = map.keySet().toArray(new String[map.size()]);
This works for any kind of map, including TreeMap
I want to store a data structure thats a table with one column Strings and another column of Ints.
List<Entry<String, Integer>> li = new LinkedList<Entry<String, Integer>>();
would do the job for a list but I would rather have the performance of and need the memory of an array.
I tried
Entry<String, Integer>[] = new Entry<String, Integer>[10];
but that doesn't seem to work.
Is Entry the right datatype to use?
Write a class that represents your tuple and use an array (or a List) of that type. Don't eschew classes, they are your friends!
And what exactly do you mean by "the performance of an array"? Arrays are not necessarily faster than List implementations.
Think of inserting an element at the position 0: A LinkedList can do it in O(1). To get the same effect in an array, you'd have to do an O(n) operation (recreating the array and copying all existing values).
If you really need an array of generic types, you can do this:
Entry<String, Integer>[] array = new Entry[10];
It gives you a compilation warning though. You can read more about it here:
http://www.devarticles.com/c/a/Java/Wildcards-Arrays-and-Generics-in-Java/2/
I don't know what is not working, but:
you have to give a name to your array.
you can't construct arrays with generic types
don't forget Entry is an interface.
So, this:
Entry<String, Integer>[] = new Entry<String, Integer>[10];
Should be this:
Entry<String, Integer>[] entries = new Entry[10];
Hope this helps!
Maybe you can just use ArrayList, shouldn't be much difference in performance compared to a plain array.
Use this,
List<Entry<String, Integer>> li = new ArrayList<Entry<String, Integer>>();