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); System.out.println(set.size()); Bon Appetit, Brandon On Tue, Apr 7, 2009 at 3:57 AM, Ingmar Lötzsch <iloetz...@asci-systemhaus.de > wrote: > > 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 duplicates. Copy the following code in a class and run the > main method: > > package test; > > import java.util.ArrayList; > import java.util.HashSet; > import java.util.Set; > > public class ListSetConversionTest > { > public static void main(String[] args) > { > ArrayList<Integer> intList = new ArrayList<Integer>(); > Integer zero = Integer.valueOf(0); > intList.add(zero); > intList.add(zero); > Set<Integer> intSet = new HashSet<Integer>(intList); > System.out.println(intSet); > Set<Integer> intSet2 = new HashSet<Integer>(); > intSet2.addAll(intList); > System.out.println(intSet2); > > String[][] productDataList = > { > {"1", "Product 1"}, > {"1", "Product 1"}, > {"2", "Product 2"}, > {"2", "Product 2"} > }; > > ArrayList<Product> productList = new ArrayList<Product>(); > for (String[] productData : productDataList) > { > Product product = new Product(); > Integer id = Integer.valueOf(productData[0]); > product.setId(id); > product.setName(productData[1]); > productList.add(product); > } > Set<Product> productSet = new HashSet<Product>(productList); > System.out.println(productSet); > } > > protected static class Product > { > Integer id; > > String name; > > public Integer getId() > { > return this.id; > } > > public void setId(Integer id) > { > this.id = id; > } > > public String getName() > { > return this.name; > } > > public void setName(String name) > { > this.name = name; > } > > @Override > public String toString() > { > return this.id + " " + this.name; > } > } > } > > You see, that there are (intList, productList) duplicates in both lists. > Both intSets don't contain duplicates, but the productSet does. > > If you want to make the products "unique" in the set, you have to > override the equals() and perhaps the hashcode() methods (I' am not an > expert for this.) > > 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); > > } > > Of course the best way is to avoid duplicates in the query and use the > code from Larry Meadors: > > return new LinkedHashSet(queryForList(...)); > > LinkedHashSet (instead of HashSet) preserves the order. > > > On Mon, Apr 6, 2009 at 10:53 PM, fer knjige <ferknj...@gmail.com > > <mailto:ferknj...@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.mead...@gmail.com > > <mailto:larry.mead...@gmail.com>> > > > > return new LinkedHashSet(queryForList(...)); > > > > Larry > > > > > > On Sun, Apr 5, 2009 at 10:53 PM, fer knjige <ferknj...@gmail.com > > <mailto: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 in advance! > >