Re: Can I use Set instead of List?

2009-04-07 Thread Ingmar Lötzsch
 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)
{
ArrayListInteger intList = new ArrayListInteger();
Integer zero = Integer.valueOf(0);
intList.add(zero);
intList.add(zero);
SetInteger intSet = new HashSetInteger(intList);
System.out.println(intSet);
SetInteger intSet2 = new HashSetInteger();
intSet2.addAll(intList);
System.out.println(intSet2);

String[][] productDataList =
{
{1, Product 1},
{1, Product 1},
{2, Product 2},
{2, Product 2}
};

ArrayListProduct productList = new ArrayListProduct();
for (String[] productData : productDataList)
{
Product product = new Product();
Integer id = Integer.valueOf(productData[0]);
product.setId(id);
product.setName(productData[1]);
productList.add(product);
}
SetProduct productSet = new HashSetProduct(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
 
 ListProduct products = (ListProduct)queryForList(Products.selectAll);
 HashSetProduct productsSet = new HashSetProduct(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!



Re: Can I use Set instead of List?

2009-04-07 Thread Brandon Goodin
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)
{
ArrayListInteger intList = new ArrayListInteger();
Integer zero = Integer.valueOf(0);
intList.add(zero);
intList.add(zero);
SetInteger intSet = new HashSetInteger(intList);
System.out.println(intSet);
SetInteger intSet2 = new HashSetInteger();
intSet2.addAll(intList);
System.out.println(intSet2);

String[][] productDataList =
{
{1, Product 1},
{1, Product 1},
{2, Product 2},
{2, Product 2}
};

ArrayListProduct productList = new ArrayListProduct();
for (String[] productData : productDataList)
{
Product product = new Product();
Integer id = Integer.valueOf(productData[0]);
product.setId(id);
product.setName(productData[1]);
productList.add(product);
}
SetProduct productSet = new HashSetProduct(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
 
  ListProduct products =
 (ListProduct)queryForList(Products.selectAll);
  HashSetProduct productsSet = new HashSetProduct(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!




Re: Can I use Set instead of List?

2009-04-07 Thread Brandon Goodin
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 Tue, Apr 7, 2009 at 8:57 AM, Brandon Goodin brandon.goo...@gmail.comwrote:

 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)
{
ArrayListInteger intList = new ArrayListInteger();
Integer zero = Integer.valueOf(0);
intList.add(zero);
intList.add(zero);
SetInteger intSet = new HashSetInteger(intList);
System.out.println(intSet);
SetInteger intSet2 = new HashSetInteger();
intSet2.addAll(intList);
System.out.println(intSet2);

String[][] productDataList =
{
{1, Product 1},
{1, Product 1},
{2, Product 2},
{2, Product 2}
};

ArrayListProduct productList = new ArrayListProduct();
for (String[] productData : productDataList)
{
Product product = new Product();
Integer id = Integer.valueOf(productData[0]);
product.setId(id);
product.setName(productData[1]);
productList.add(product);
}
SetProduct productSet = new
 HashSetProduct(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
 
  ListProduct products =
 (ListProduct)queryForList(Products.selectAll);
  HashSetProduct productsSet = new HashSetProduct(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 

Re: Can I use Set instead of List?

2009-04-07 Thread Nathan Maves
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 duplicates that it finds.


On Tue, Apr 7, 2009 at 7:59 AM, Brandon Goodin brandon.goo...@gmail.comwrote:

 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 Tue, Apr 7, 2009 at 8:57 AM, Brandon Goodin 
 brandon.goo...@gmail.comwrote:

 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)
{
ArrayListInteger intList = new ArrayListInteger();
Integer zero = Integer.valueOf(0);
intList.add(zero);
intList.add(zero);
SetInteger intSet = new HashSetInteger(intList);
System.out.println(intSet);
SetInteger intSet2 = new HashSetInteger();
intSet2.addAll(intList);
System.out.println(intSet2);

String[][] productDataList =
{
{1, Product 1},
{1, Product 1},
{2, Product 2},
{2, Product 2}
};

ArrayListProduct productList = new ArrayListProduct();
for (String[] productData : productDataList)
{
Product product = new Product();
Integer id = Integer.valueOf(productData[0]);
product.setId(id);
product.setName(productData[1]);
productList.add(product);
}
SetProduct productSet = new
 HashSetProduct(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
 
  ListProduct products =
 (ListProduct)queryForList(Products.selectAll);
  HashSetProduct productsSet = new HashSetProduct(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 

Re: Can I use Set instead of List?

2009-04-06 Thread Richard Yee
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 have results in Set not in List. How can I do it since
method 'queryForList' returns only List?

Thanks in advance!




Re: Can I use Set instead of List?

2009-04-06 Thread Larry Meadors
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 in advance!





Re: Can I use Set instead of List?

2009-04-06 Thread fer knjige
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 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!
 
 
 



Re: Can I use Set instead of List?

2009-04-06 Thread Nathan Maves
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

ListProduct products = (ListProduct)queryForList(Products.selectAll);
HashSetProduct productsSet = new HashSetProduct(products.size());
for(Product p:products) {
  productsSet.add(p);
}



On Mon, Apr 6, 2009 at 10:53 PM, fer knjige 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

 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 in advance!
 
 
 





Can I use Set instead of List?

2009-04-05 Thread fer knjige
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!