ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nathan Maves <nathan.ma...@gmail.com>
Subject Re: Can I use Set instead of List?
Date Tue, 07 Apr 2009 05:18:37 GMT
This is a fundamental java issue.  You will never be able to cast a list to
a set because a list allows duplicates whereas a set dose not.  There is no
way for java to know which of the duplicate elements it should use.  If you
guarantee that you have unique result from your DB(i.e. use the unique
keyword), then you can programatically create a set from a list.
off the top of my head....

List<Product> products = (List<Product>)queryForList("Products.selectAll");
HashSet<Product> productsSet = new HashSet<Product>(products.size());
for(Product p:products) {
  productsSet.add(p);
}



On Mon, Apr 6, 2009 at 10:53 PM, fer knjige <ferknjige@gmail.com> wrote:

> Unfortunately this doesn't work. It returns following error:
>
> Exception in thread "main" java.lang.ClassCastException:
> java.util.ArrayList.
>
> Solution?
>
> 2009/4/6 Larry Meadors <larry.meadors@gmail.com>
>
> return new LinkedHashSet(queryForList(...));
>>
>> Larry
>>
>>
>> On Sun, Apr 5, 2009 at 10:53 PM, fer knjige <ferknjige@gmail.com> wrote:
>> > Hi,
>> >
>> > I have a simple query:
>> >
>> > select * from Products.
>> >
>> > I want to have results in Set not in List. How can I do it since
>> > method 'queryForList' returns only List?
>> >
>> > Thanks in advance!
>> >
>> >
>> >
>>
>
>

Mime
View raw message