Armin,

Yes.  Extracting the key set results in an array wherein the n-th element of 
the key array corresponds to the n-th element of the values array.  That is 
part of how the hash map is handled in Java.  Even if you implemented your own 
sorting algorithm for insertion the value would get inserted with the key and 
the corresponding key and values arrays would still match.  The only caveat 
would be if you decided to manipulate the keys array independently after 
getting it from the HashMap.

Thanks,

Thomas Ginter
801-448-7676
thomas.gin...@utah.edu




On Oct 17, 2013, at 8:43 AM, armin.weg...@bka.bund.de wrote:

> Hi Thomas,
> 
> thanks for your answer. Using HashMap, does the n-th element of keySet() 
> always corresponds to the n-th element of values()? Is this a defined 
> behavior in Java?
> 
> Cheers,
> Armin
> 
> -----Ursprüngliche Nachricht-----
> Von: Thomas Ginter [mailto:thomas.gin...@utah.edu] 
> Gesendet: Mittwoch, 16. Oktober 2013 18:53
> An: <user@uima.apache.org>
> Betreff: Re: HashMap as type feature
> 
> Armin,
> 
> Our team does this with an annotation type designed to store feature vectors 
> for Machine Learning applications.  In this case we use a StringArray feature 
> for the keys and a StringArray feature for the values.  The StringArrays are 
> pulled from a HashMap<String, String> vector variable and inserted into the 
> features with the following code:
> 
> int size = vector.size();
> StringArray keys = new StringArray(jcas, size); StringArray values = new 
> StringArray(jcas, size); keys.copyFromArray(vector.keySet().toArray(new 
> String[size]), 0, 0, size); values.copyFromArray(vector.values().toArray(new 
> String[size]), 0, 0, size);
> 
> Retrieving the values is fairly straightforward.  If you are using a static 
> annotation type it can be as simple as:
> 
> StringArray keys = vector.getKeysArray();
> 
> If you parameterize our annotation type in the annotator you can use the name 
> of the feature to get a Feature object reference then pull the StringArrays 
> like so:
> 
> Type annotationTypeObj = aJCas.getRequiredType("com.my.Annotation"); 
> //parameter is the canonized name of the Annotation type Feature keyFeature = 
> annotationTypeObj.getFeatureByBaseName("keyFeatureName"); //the actual name 
> of the feature storing the key StringArray reference Feature valuesFeature = 
> annotationTypeObj.getFeatureByBaseName("valuesFeatureName"); //the name of 
> the values feature
> 
> //Get a list of the annotation objects in the CAS then iterate through the 
> list, for each annotation 'a' do the following to retrieve the keys and values
> 
> StringArray keys = (StringArray) vector.getFeatureValue(keysFeature);
> StringArray values = (StringArray) vector.getFeatureValue(valuesFeature);
> 
> If necessary you can retrieve a String[] from the StringArray 
> FeatureStructure by calling the .toArray() method such as:
> 
> String[] keysArray = keys.toArray();
> 
> Let me know if you have any questions.
> 
> Thanks,
> 
> Thomas Ginter
> 801-448-7676
> thomas.gin...@utah.edu<mailto:thomas.gin...@utah.edu>
> 
> 
> 
> 
> On Oct 16, 2013, at 9:55 AM, Dr. Armin Wegner 
> <arminweg...@googlemail.com<mailto:arminweg...@googlemail.com>> wrote:
> 
> Hi,
> 
> I'd like to have a type feature that is a list of key-value pairs. The number 
> of pairs is unknown. What's best for this? Is it even possible?
> 
> Thanks,
> Armin
> 

Reply via email to