473,473 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Set Collection Needed ?

I have numerous collections (currently ArrayLists) which contain
objects of the same type. I need some way of aggregating these lists
into one collection with duplicate objects only appearing once in the
final collection (set type collection).

Currently i'm using an Hashtable, entering the object as key while
checking beforehand if the key exists (avoid duplicates).

Whats the best way to do this? The technique should be as efficient as
possible as it is contained within a block of code that loops
frequently.
Nov 16 '05 #1
5 3392
Hey try overriding the ArrayList,HashTable function Add method when u are adding objects into the collection in this overridden method check for the whether the object already exists. I will write this code for you, in the mean time even u try doing the same
--
Do not mistake that i am crazy bout .net it is just that it is filled with so many bugs that it ends up in u cleaning up the mess day and night.

And hence u breathe .net all the time.
"Sean Hearne" wrote:
I have numerous collections (currently ArrayLists) which contain
objects of the same type. I need some way of aggregating these lists
into one collection with duplicate objects only appearing once in the
final collection (set type collection).

Currently i'm using an Hashtable, entering the object as key while
checking beforehand if the key exists (avoid duplicates).

Whats the best way to do this? The technique should be as efficient as
possible as it is contained within a block of code that loops
frequently.

Nov 16 '05 #2
Here is the code as promised, object oriented concepts are gr8 dont u agree
using System;

using System.Collections;

/// <summary>
/// Summary description for Class1.
/// </summary>
public class CrapObj
{
int i, j;
string strName;
public CrapObj(int i, int j, string strName)
{
this.i = i;
this.j = j;
this.strName = strName;
}
public void DisplayCrap()
{
Console.WriteLine("i: "+i.ToString()+" ,j: "+j.ToString()+" ,name: "+strName);
}
public bool Equals(CrapObj obj)
{
if(obj.i == this.i && obj.j == this.j && obj.strName == this.strName)
return true;
return false;
}
}
public class BreatheArrayList:ArrayList
{
/// <summary>
/// Going strong
/// </summary>
public BreatheArrayList()
{
}
public int Add(CrapObj val)
{
if(this.Count!=0)
{
for(int i = 0;i<this.Count;i++)
{
if(val.Equals((CrapObj)this[i]))
{
return -1;
}
}

}
return Add((object)val);
}
public override int Add(object val)
{
return base.Add (val);
}

}

class CheckWhetherItBreathes
{
public CheckWhetherItBreathes()
{
}
public static void Main()
{
CrapObj obj1 = new CrapObj(3,1,"obj1");
CrapObj obj2 = new CrapObj(2,1,"obj2");
CrapObj obj3 = new CrapObj(1,1,"obj3");
CrapObj duplicate = new CrapObj(3,1,"obj1");
BreatheArrayList obj = new BreatheArrayList();
obj.Add(obj1);
obj.Add(obj2);
obj.Add(obj3);
obj.Add(duplicate);
for(int i =0; i < obj.Count; i++)
{
CrapObj objCheck = (CrapObj)obj[i];
objCheck.DisplayCrap();
}
}
}

--
Do not mistake that i am crazy bout .net it is just that it is filled with so many bugs that it ends up in u cleaning up the mess day and night.

And hence u breathe .net all the time.
"breathedotnet" wrote:
Hey try overriding the ArrayList,HashTable function Add method when u are adding objects into the collection in this overridden method check for the whether the object already exists. I will write this code for you, in the mean time even u try doing the same
--
Do not mistake that i am crazy bout .net it is just that it is filled with so many bugs that it ends up in u cleaning up the mess day and night.

And hence u breathe .net all the time.
"Sean Hearne" wrote:
I have numerous collections (currently ArrayLists) which contain
objects of the same type. I need some way of aggregating these lists
into one collection with duplicate objects only appearing once in the
final collection (set type collection).

Currently i'm using an Hashtable, entering the object as key while
checking beforehand if the key exists (avoid duplicates).

Whats the best way to do this? The technique should be as efficient as
possible as it is contained within a block of code that loops
frequently.

Nov 16 '05 #3
i have optimised the code by removing
public int Add(CrapObj val) in the BreatheArrayList class i am using casting to do the same, if u need explanations for the code plz let me know
-----------------start-----------------
using System;

using System.Collections;

/// <summary>
/// Summary description for Class1.
/// </summary>
public class CrapObj
{
int i, j;
string strName;
public CrapObj(int i, int j, string strName)
{
this.i = i;
this.j = j;
this.strName = strName;
}
public void DisplayCrap()
{
Console.WriteLine("i: "+i.ToString()+" ,j: "+j.ToString()+" ,name: "+strName);
}
public bool Equals(CrapObj obj)
{
if(obj.i == this.i && obj.j == this.j && obj.strName == this.strName)
return true;
return false;
}
}
public class BreatheArrayList:ArrayList
{
/// <summary>
/// Going strong
/// </summary>
public BreatheArrayList()
{
}
public override int Add(object val)
{
CrapObj obj = (CrapObj) val;
if(this.Count!=0)
{
for(int i = 0;i<this.Count;i++)
{
if(obj.Equals((CrapObj)this[i]))
{
return -1;
}
}

}
return base.Add (val);
}

}

class CheckWhetherItBreathes
{
public CheckWhetherItBreathes()
{
}
public static void Main()
{
CrapObj obj1 = new CrapObj(3,1,"obj1");
CrapObj obj2 = new CrapObj(2,1,"obj2");
CrapObj obj3 = new CrapObj(1,1,"obj3");
CrapObj duplicate = new CrapObj(3,1,"obj1");
BreatheArrayList obj = new BreatheArrayList();
obj.Add(obj1);
obj.Add(obj2);
obj.Add(obj3);
obj.Add(duplicate);
for(int i =0; i < obj.Count; i++)
{
CrapObj objCheck = (CrapObj)obj[i];
objCheck.DisplayCrap();
}
}
}
-----------------End-----------------

--
Do not mistake that i am crazy bout .net it is just that it is filled with so many bugs that it ends up in u cleaning up the mess day and night.

And hence u breathe .net all the time.
"breathedotnet" wrote:
Here is the code as promised, object oriented concepts are gr8 dont u agree
using System;

using System.Collections;

/// <summary>
/// Summary description for Class1.
/// </summary>
public class CrapObj
{
int i, j;
string strName;
public CrapObj(int i, int j, string strName)
{
this.i = i;
this.j = j;
this.strName = strName;
}
public void DisplayCrap()
{
Console.WriteLine("i: "+i.ToString()+" ,j: "+j.ToString()+" ,name: "+strName);
}
public bool Equals(CrapObj obj)
{
if(obj.i == this.i && obj.j == this.j && obj.strName == this.strName)
return true;
return false;
}
}
public class BreatheArrayList:ArrayList
{
/// <summary>
/// Going strong
/// </summary>
public BreatheArrayList()
{
}
public int Add(CrapObj val)
{
if(this.Count!=0)
{
for(int i = 0;i<this.Count;i++)
{
if(val.Equals((CrapObj)this[i]))
{
return -1;
}
}

}
return Add((object)val);
}
public override int Add(object val)
{
return base.Add (val);
}

}

class CheckWhetherItBreathes
{
public CheckWhetherItBreathes()
{
}
public static void Main()
{
CrapObj obj1 = new CrapObj(3,1,"obj1");
CrapObj obj2 = new CrapObj(2,1,"obj2");
CrapObj obj3 = new CrapObj(1,1,"obj3");
CrapObj duplicate = new CrapObj(3,1,"obj1");
BreatheArrayList obj = new BreatheArrayList();
obj.Add(obj1);
obj.Add(obj2);
obj.Add(obj3);
obj.Add(duplicate);
for(int i =0; i < obj.Count; i++)
{
CrapObj objCheck = (CrapObj)obj[i];
objCheck.DisplayCrap();
}
}
}

--
Do not mistake that i am crazy bout .net it is just that it is filled with so many bugs that it ends up in u cleaning up the mess day and night.

And hence u breathe .net all the time.
"breathedotnet" wrote:
Hey try overriding the ArrayList,HashTable function Add method when u are adding objects into the collection in this overridden method check for the whether the object already exists. I will write this code for you, in the mean time even u try doing the same
--
Do not mistake that i am crazy bout .net it is just that it is filled with so many bugs that it ends up in u cleaning up the mess day and night.

And hence u breathe .net all the time.
"Sean Hearne" wrote:
I have numerous collections (currently ArrayLists) which contain
objects of the same type. I need some way of aggregating these lists
into one collection with duplicate objects only appearing once in the
final collection (set type collection).

Currently i'm using an Hashtable, entering the object as key while
checking beforehand if the key exists (avoid duplicates).

Whats the best way to do this? The technique should be as efficient as
possible as it is contained within a block of code that loops
frequently.

Nov 16 '05 #4
Would it not be better to use Hashtable as the base class? There could
be alot of checking involved.

I'll have in region of 50+ arraylists, each containing 20+ objects and
any number of these lists will have to be combined every few seconds.

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #5
Yeah Sean u could do that , i just wanted to show how we could use abstraction and extend features of .Net. Instead of an array list goahead and use HashTable, do not forget to post the code

Happy coding, keep smiling.
--
Do not mistake that i am crazy bout .net it is just that it is filled with so many bugs that it ends up in u cleaning up the mess day and night.

And hence u breathe .net all the time.
"Sean Hearne" wrote:
Would it not be better to use Hashtable as the base class? There could
be alot of checking involved.

I'll have in region of 50+ arraylists, each containing 20+ objects and
any number of these lists will have to be combined every few seconds.

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matthew Hood | last post by:
Here's the situation. I am developing an ASP.NET web application. Most of my forms will be accessing a database (MS Access) for either record creation/deletion/updating or for list lookups. The...
2
by: Marc Scheuner [MVP ADSI] | last post by:
Folks, Has anyone done this before? (I'm sure some of you guys have) I'd like to make sure our business objects and collections thereof are created in such a way that we can connect them to a...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
16
by: Ben Hannon | last post by:
Hi, I'm writting a COM Class in VB.NET to be used in a VB6 project (Tired of the VB6 hassles with cloning and serializing an object). All my classes I need cloneable/serializable are now in a...
6
by: Michael D. Ober | last post by:
In VB 6, the loop iterator v in the following code must be a variant. dim v as variant dim c as new collection for each v in collection ... next v What is the general translation in VB 7.1...
14
by: Rich | last post by:
Yes, I need to store some values in an array type collection object that can hold 3 or more parameters per index. I have looked at the collection object, hashtable object and would prefer not to...
19
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the...
22
by: Tom Wright | last post by:
Hi all I suspect I may be missing something vital here, but Python's garbage collection doesn't seem to work as I expect it to. Here's a small test program which shows the problem on python 2.4...
3
by: jacob navia | last post by:
Abstract: Continuing the discussion about abstract data types, in this discussion group, a string collection data type is presented, patterned after the collection in C# and similar languages...
158
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.