php $_POST array: what in java? - java

I'm "translating" a PHP class in Java.
I've a function that takes a key and checks if it's in the $_POST array.
How could I do a similar thing from a class Java method?

Java doesn't support associative arrays. If you're using a HashMap:
HashMap hMap = new HashMap();
//(...)
ht.put("key","One");
ht.containsKey("key"); // returns true
If you're using a hashtable:
Hashtable ht = new Hashtable();
//(...)
ht.containsKey("key");
Unless you mean how to access POST parameters, then check this question and this documentation

Look up request.getParameter()
http://www.roseindia.net/jsp/GetParameterMethodOfRequest.shtml

Related

Hashmap not allowing integer as value

My situation demands to add Integer to the Hashmap value because i need to sort the list based on the integer. I am doing like below
Map hmInspStatus = new HashMap();
hmInspStatus.put("Name",Integer.parseInt(strIRName.substring(2,strIRName.length())));
System is throwing an error message saying i can't add an integer to a HashMap. I referred some of the posts in the site and suggested to use a HashSet, but is it possible to add Key, value to HashSet?
Can anybody help me in achieving what i am looking for?
Thanks
Modern Java uses generic data structures. With the generic types given, Java will handle autoboxing of the primitive type.
Map<String, Integer> hmInspStatus = new HashMap<String, Integer>();
hmInspStatus.put("Name",Integer.parseInt(strIRName.substring(2,strIRName.length())));
Update: OP is using Java 1.3. This version not only does not support generics, it also does not support autoboxing. In that case, you have to skip the generics and use manual boxing, or directly construct the Integer from the String.
Map hmInspStatus = new HashMap();
hmInspStatus.put("Name", new Integer(strIRName.substring(2,strIRName.length())));
Do:
Map hmInspStatus = new HashMap();
hmInspStatus.put("Name",(Integer)Integer.parseInt(strIRName.substring(2,strIRName.length())));

How to declare and assign the values to a hashMap directly

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" ]

Java - getting value from an array using string

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>.

Does Java support associative arrays? [duplicate]

This question already has answers here:
Java associative-array
(15 answers)
Closed 6 years ago.
I'm wondering if arrays in Java could do something like this:
int[] a = new int[10];
a["index0"] = 100;
a["index1"] = 100;
I know I've seen similar features in other languages, but I'm not really familiar with any specifics... Just that there are ways to associate values with string constants rather than mere numeric indexes. Is there a way to achieve such a thing in Java?
You can't do this with a Java array. It sounds like you want to use a java.util.Map.
Map<String, Integer> a = new HashMap<String, Integer>();
// put values into the map
a.put("index0", 100); // autoboxed from int -> Integer
a.put("index1", Integer.valueOf(200));
// retrieve values from the map
int index0 = a.get("index0"); // 100
int index1 = a.get("index1"); // 200
I don't know a thing about C++, but you are probably looking for a Class implementing the Map interface.
What you need is java.util.Map<Key, Value> interface and its implementations (e.g. HashMap) with String as key
To store things with string keys, you need a Map. You can't use square brackets on a Map. You can do this in C++ because it supports operator overloading, but Java doesn't.
There is a proposal to add this syntax for maps, but it will be added for Java 8 at the earliest.
Are you looking for the HashMap<k,v>() class? See the javadocs here.
Roughly speaking, usage would be:
HashMap<String, int> a = new HashMap<String,int>();
a.put("index0", 100);
etc.
java does not have associative arrays yet. But instead you can use a hash map as an alternative.

Does Java support variable variables?

Such as in PHP:
<?php
$a = 'hello';
$$a = 'world';
echo $hello;
// Prints out "world"
?>
I need to create an unknown number of HashMaps on the fly (which are each placed into an arraylist). Please say if there's an easier or more Java-centric way. Thanks.
The best you can do is have a HashMap of HashMaps. For example:
Map<String,Map<String,String>> m = new HashMap<String,Map<String,String>>();
// not set up strings pointing to the maps.
m.put("foo", new HashMap<String,String>());
Its not called variable variables in java.
Its called reflection.
Take a look at java.lang.reflect package docs for details.
You can do all such sorts of things using reflection.
Bestoes,
jrh.
Java does not support what you just did in PHP.
To do something similar you should just make a List<Map<>> and store your HashMaps in there. You could use a HashMap of HashMaps.
A 'variable variable' in Java is an array or List or some sort of data structure with varying size.
No. You would do something like
List<Map<String,String> myMaps = new ArrayList<Map<String,String>>()
and then in your loop you would do:
Map<String,String> newMap = new Hashtable<String,String>();
//do stuff with newMap
myMaps.add(newMap);

Categories

Resources