| RankedBagTRetainAll Method |
Removes any elements that are not in the supplied collection from the bag.
Namespace:
Kaos.Collections
Assembly:
KaosCollections (in KaosCollections.dll) Version: 4.2.0.0
Syntax Exceptions Remarks
Cardinality is respected by this operation so that the occurrences
of each item retained is the number of occurrences of that item in other.
In precise terms,
this operation removes max(n-m,0) occurrences of a given item
where the bag contains n occurrences
and other contains m occurrences.
Examples using System;
using Kaos.Collections;
namespace ExampleApp
{
class RbExample01
{
static void Main()
{
var crayons = new RankedBag<string> (StringComparer.InvariantCultureIgnoreCase)
{ "red", "yellow", "black", "BLACK" };
crayons.Add ("blue");
Console.WriteLine ($"There are {crayons.Count} total crayons:");
foreach (var crayon in crayons)
Console.WriteLine ($" {crayon}");
Console.WriteLine ($"\nThere are {crayons.GetDistinctCount()} distinct colors:");
foreach (var crayon in crayons.Distinct())
Console.WriteLine ($" {crayon}");
Console.WriteLine ($"\nGot 'gold' crayon? {crayons.Contains ("gold")}");
crayons.RetainAll (new string[] { "white", "grey", "Black", "red" });
Console.WriteLine ("\nAfter RetainAll: ");
foreach (var crayon in crayons)
Console.WriteLine ($" {crayon}");
}
}
}
See Also