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.
The duplicates are not the reason. ArrayList is not a subtype of Set.
And how does the query
select * from Products
produce duplicates?
Lists accept
I took a few seconds and wrote this
ArrayList list = new ArrayList();
list.add(a);
list.add(b);
list.add(c);
list.add(d);
list.add(e);
list.add(f);
HashSet set = new HashSet();
set.addAll(list);
Wrote this too and it worked:
ArrayList list = new ArrayList();
list.add(a);
list.add(b);
list.add(c);
list.add(d);
list.add(e);
list.add(f);
HashSet set = new HashSet(list);
System.out.println(set.size());
Brandon
On
I took this one step further
ArrayListInteger list = new ArrayListInteger();
list.add(1);
list.add(1);
list.add(2);
list.add(3);
HashSetInteger set = new HashSetInteger();
set.addAll(list);
System.out.println(list.size());
System.out.println(set.size());
output:
4
3
So java will remove all
No, but you can create a set from the list though. If you need to
remove duplicates, why don't you change the query to be unique?
-Richard
Sent from my iPhone
On Apr 5, 2009, at 9:53 PM, fer knjige ferknj...@gmail.com wrote:
Hi,
I have a simple query:
select * from Products.
I want to
return new LinkedHashSet(queryForList(...));
Larry
On Sun, Apr 5, 2009 at 10:53 PM, fer knjige ferknj...@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
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.mead...@gmail.com
return new LinkedHashSet(queryForList(...));
Larry
On Sun, Apr 5, 2009 at 10:53 PM, fer knjige
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