On 2012 Feb 1, at 3:07 AM, Thomas Koch wrote:
> OK, I found a solution (obviously not the best one...): lucene.Set is
> representing a java.util *interface* Set<E> which of course cannot be
> instantiated. HashSet is an implementing class, and can be instantiated. You
> can add elements via the add() method to the set then. Example:
> 
> def get_lucene_set(python_list):
>    """convert python list into lucene.Set (Java.util.set interface)
>          using the HashSet class (java.util) wrapped in lucene.HashSet
>    """
>    hs = lucene.HashSet()
>    for el in python_list:
>        hs.add(el)
>    return hs
> 
> However I'm still looking for a more elegant constructor that would allow to
> create a HashSet from a python set (or list). Is that available/possible?

Arrays.asList converts java arrays to java lists, and you can pass a python 
sequence to it.  From there, all of the collection constructors can be passed 
other collections.

>>> lucene.Arrays.asList('abc')
<List: [a, b, c]>
>>> lucene.HashSet(lucene.Arrays.asList('abc'))
<HashSet: [b, c, a]>

> The same holds for lists like the ArrayList (from java.util too) which
> implements the Collection interface:
> 
> Example:
>>>> l =range(3)
>>>> l
> [0, 1, 2]
>>>> a = lucene.ArrayList(l)
>  Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
>  lucene.InvalidArgsError: (<type 'ArrayList'>, '__init__', ([0, 1, 2],))
> 
> using the for-in-do obj.add "trick" allows to generate a 'filled' instance
> here as well : <ArrayList: [0, 1, 2]>
> but wouldn't it be nice to be able to create an instance more "pythonic"?
> 
> I'm not a Java expert (nor do I know much about the Collections API), so
> maybe it's even impossible in Java to create an instance of a
> List,Vector,HashSet (whatever) and passing some literals (like Strings) -
> who knows...  So if anyone has a better idea how to do this in PyLucene
> please let me know ,-)
> 
> regards,
> Thomas
> 
> 

Reply via email to