I need PHP object similar to HashMap in Java, but I didn't find when I googled, so if someone knows how I can mimic HashMaps in PHP, help would be appreciated.
Answers
Dictionary
is probably the closest. System.Collections.Generic.Dictionary
implements the System.Collections.Generic.IDictionary
interface (which is similar to Java's Map
interface).
Some notable differences that you should be aware of:
- Adding/Getting items
- Java's HashMap has the
put
andget
methods for setting/getting itemsmyMap.put(key, value)
MyObject value = myMap.get(key)
- C#'s Dictionary uses
[]
indexing for setting/getting itemsmyDictionary[key] = value
MyObject value = myDictionary[key]
- Java's HashMap has the
null
keys- Java's
HashMap
allows null keys - .NET's
Dictionary
throws anArgumentNullException
if you try to add a null key
- Java's
- Adding a duplicate key
- Java's
HashMap
will replace the existing value with the new one. - .NET's
Dictionary
will replace the existing value with the new one if you use[]
indexing. If you use theAdd
method, it will instead throw anArgumentException
.
- Java's
- Attempting to get a non-existent key
- Java's
HashMap
will return null. - .NET's
Dictionary
will throw aKeyNotFoundException
. You can use theTryGetValue
method instead of the[]
indexing to avoid this:
MyObject value = null; if (!myDictionary.TryGetValue(key, out value)) { /* key doesn't exist */ }
- Java's
Dictionary
's has a ContainsKey
method that can help deal with the previous two problems.
In bytecode you have a nop
instruction, but there's no nop
statement in the Java language.
You can add an extra ;
on a line by itself and the code will still compile, but that's not much more meaningful than adding an empty line.
Another "does nothing" statement could be:
assert true;
which has no side-effects what so ever, and can be turned off when executing the program.
As it turns out, assert true
does not seem to generate any bytecode instructions, which causes break-points on assert true to be skipped all together. Eclipse is however able to break on a statement such as
assert Boolean.TRUE;
which is quite similar.
Never used any of those, but they look interesting..
Take a look at Gearman as well.. more overhead in systems like these but you get other cool stuff :) Guess it depends on your needs ..
If for any String s crunch(s) returns a reproducible String that the attacker cannot guess, the DDoS attack has effectively been prevented right?
Basically, this is what you do when you salt password hashes (although for slightly different reasons). It doesn't prevent collision attacks entirely (if you have a hash function mapping arbitrary-length input to a fixed-length output, hashes can always collide), but using a secret salt should make such attacks harder.
A quick'n'dirty implementation could look something like this:
public class SaltedHashMap<V> {
private final Map<String, V> map = new HashMap<>();
private final String salt;
public SaltedHashMap(String salt) {
this.salt = salt;
}
public V get(String key){
return map.get(key + salt);
}
public void put(String key, V value) {
map.put(key + salt, value);
}
}
Using a web server as an example, we could use a SecureRandom
to randomize a new salt for every incoming request, meaning that even if you managed to figure out a collision-generating input for one request, it would be extremely unlikely to work for other requests.
Arrays in PHP can have Key Value structure.