You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by fer knjige <fe...@gmail.com> on 2009/04/06 06:53:58 UTC

Can I use Set instead of List?

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?

Posted by Nathan Maves <na...@gmail.com>.
I took this one step further
ArrayList<Integer> list = new ArrayList<Integer>();

list.add(1);
list.add(1);
list.add(2);
list.add(3);

HashSet<Integer> set = new HashSet<Integer>();
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 <br...@gmail.com>wrote:

> 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 <br...@gmail.com>wrote:
>
>> 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 <
>> iloetzsch@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 <ferknjige@gmail.com
>>> > <ma...@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
>>> >     <ma...@gmail.com>>
>>> >
>>> >         return new LinkedHashSet(queryForList(...));
>>> >
>>> >         Larry
>>> >
>>> >
>>> >         On Sun, Apr 5, 2009 at 10:53 PM, fer knjige <
>>> ferknjige@gmail.com
>>> >         <ma...@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?

Posted by Brandon Goodin <br...@gmail.com>.
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 <br...@gmail.com>wrote:

> 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 <
> iloetzsch@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 <ferknjige@gmail.com
>> > <ma...@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
>> >     <ma...@gmail.com>>
>> >
>> >         return new LinkedHashSet(queryForList(...));
>> >
>> >         Larry
>> >
>> >
>> >         On Sun, Apr 5, 2009 at 10:53 PM, fer knjige <
>> ferknjige@gmail.com
>> >         <ma...@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?

Posted by Brandon Goodin <br...@gmail.com>.
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 <iloetzsch@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 <ferknjige@gmail.com
> > <ma...@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
> >     <ma...@gmail.com>>
> >
> >         return new LinkedHashSet(queryForList(...));
> >
> >         Larry
> >
> >
> >         On Sun, Apr 5, 2009 at 10:53 PM, fer knjige <ferknjige@gmail.com
> >         <ma...@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?

Posted by Ingmar Lötzsch <il...@asci-systemhaus.de>.
> 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 <ferknjige@gmail.com
> <ma...@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
>     <ma...@gmail.com>>
> 
>         return new LinkedHashSet(queryForList(...));
> 
>         Larry
> 
> 
>         On Sun, Apr 5, 2009 at 10:53 PM, fer knjige <ferknjige@gmail.com
>         <ma...@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?

Posted by Nathan Maves <na...@gmail.com>.
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 <fe...@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 <la...@gmail.com>
>
> return new LinkedHashSet(queryForList(...));
>>
>> Larry
>>
>>
>> On Sun, Apr 5, 2009 at 10:53 PM, fer knjige <fe...@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?

Posted by fer knjige <fe...@gmail.com>.
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 <la...@gmail.com>

> return new LinkedHashSet(queryForList(...));
>
> Larry
>
>
> On Sun, Apr 5, 2009 at 10:53 PM, fer knjige <fe...@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?

Posted by Larry Meadors <la...@gmail.com>.
return new LinkedHashSet(queryForList(...));

Larry


On Sun, Apr 5, 2009 at 10:53 PM, fer knjige <fe...@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?

Posted by Richard Yee <ry...@cruzio.com>.
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 <fe...@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!
>
>