Does BasicNameValuePair allow duplicate keys? - java

I want to know if BasicNameValuePair allows duplicate keys?

You can only put one key and value in an BasicNameValuePair. So you can't have duplicate keys! Or do you mean if you could have two of them with the same key? Then the answer is yes.

BasicNameValuePair is just a simple wrapper around two Strings (a name and a value). There's no way of adding more than that, so duplicate keys doesn't really make sense. There are no keys, as such, just a single name.

Related

Create Weka Instance with string attribute

I'm trying to convert an ArrayList that is custom code I have inherited to a Weka Instances structure so I can use the Weka IBk classifier on it.
In the Instance the features are represented with a HashMap. So if I'm classifying a film review for example a feature might be a HashMap of ("funny", 2), 2 being the occurrence of the word "funny"
Although there's probably a better way I'm iterating over my instances to try and convert them to Weka Instances.
The problem is I can't instance.setValue("funny", 2) as setValue() requires an int,double input. Is there a way to do this or should I be approaching it a different way?
You can create an attribute per key (get all your distinct keys in a list, sort it to keep the order fixed). The attributes will have numeric values which are the number of occurrences.

Get key using value from a Map containing duplicate keys or values

I have a Map having duplicate values. I now want to get the key using value without iterating through all the Map.Entries. I have more than 500 entries in the map. I do not want to iterate the whole map each time.
I thought of using BiMap of google collections. But, BiMap does not support duplicate keys. Could anyone suggest on what other thirdparty library can be used to solve this?
Update:
The map contains duplicate values and it's loaded from a text file containing key value pairs.
You could use a ListMultimap and then use Multimaps.invertFrom() to get the inverse mapping.
I have a Map
...well, there's your problem! It sounds like the data doesn't quite fit the limitations of a BiMap. Consider maintaining a different ordered collection of your data encapsulated as tuples (or a pair of ordered collections).

Get index or key given the value in java Map

Given the value, I need to get the index or key which this value belongs in a Map without the need to iterate it. I'm using java.
Thanks
Guava has a BiMap, which is a bidirectional map (each key and each value are unique).
If you don't waht to use an external library, you only have 2 options:
use a plain HashMap and you will need to iterate over the keys
use 2 hashmaps, one relating keys to values, and one relating values to keys (which in essence is what a bidirectional map does).
You could always use a BidiMap from commons-collections:
http://commons.apache.org/collections/apidocs/org/apache/commons/collections/BidiMap.html
You have to use some kind of BidirectionalMap. For example this one: http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/BiMap.html
You can't without iterating.
Also, the value could belong to multiple keys, so your question is somewhat moot.
If there was a one-to-one relationship between the keys/values, you could do it by having two maps - one for each direction of the relationship - and looking up the value as a key of the second (reverse) map.

How to store non-unique key value pair in core java 1.4

i need to store non unique key value pair like in an application which can only be compiled with java 1.4. It should look like :
{"key1"=>"value1",
"key2"=>"value2",
"key3"=>"value3",
"key3"=>"value4"
}
thanks in advance
In core java, I think you are stuck with storing arrays or lists as the values of a Map interface (hashmap). You might also consider answers here: Map implementation with duplicate keys
What are the semantics of asking for a duplicate key?

Looking for Java Map implementation that supports getKeysForValue

I'm looking for an implementation of java.util.Map that has a method that will return all they keys mapped to a given value, that is, there are multiple keys map to the same value. I've looked at Google Collections and Apache Commons and didn't notice anything. Of course, I could iterate through the keyset and check each corresponding value or use two maps, but I was hoping there was something available already built.
I don't know if that solution is good for you, but you can implement easily that by using a standard map from keys to values and a MultiMap from values to key.
Of course you'll have to take care of the syncronization of the two structures, IE when you remove a key from the map, you have to remove the key itself from the set of keys mapped to the value in the multimap.
It doesn't seems difficult to implement, maybe a bit heavy from the memory overhead aspect.
What you're looking for here is a bidirectional map, for which there is an implementation in commons collections.
Your value objects could have a property (of type ArrayList maybe) that holds all the keys.
Then you extend HashMap (or whatever Map impl you use) and override put so that when you put and object for a key you also add the key to your object's list of keys.
I can't find a ready made class that supports values with multiple keys. However you could re-implement the Apache Commons DualHashBidiMap using a MultiHashMap in place of one of the HashMaps.

Categories