Set Collection Needed ?

  • Thread starter Thread starter Sean Hearne
  • Start date Start date
S

Sean Hearne

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.
 
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
 
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))
{
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;
objCheck.DisplayCrap();
}
}
}
 
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))
{
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;
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 said:
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))
{
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;
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 said:
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
 
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!
 
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.
 
Back
Top