JJ.Framework.Collections
1.5.6877.41334
Prefix Reserved
dotnet add package JJ.Framework.Collections --version 1.5.6877.41334
NuGet\Install-Package JJ.Framework.Collections -Version 1.5.6877.41334
<PackageReference Include="JJ.Framework.Collections" Version="1.5.6877.41334" />
paket add JJ.Framework.Collections --version 1.5.6877.41334
#r "nuget: JJ.Framework.Collections, 1.5.6877.41334"
// Install JJ.Framework.Collections as a Cake Addin #addin nuget:?package=JJ.Framework.Collections&version=1.5.6877.41334 // Install JJ.Framework.Collections as a Cake Tool #tool nuget:?package=JJ.Framework.Collections&version=1.5.6877.41334
JJ.Framework.Collections
Basic helpers for working with lists of information.
- Handy LINQ-style extension methods.
- Recursive collection extensions.
- A port of a RedBlackTree collection.
Collection Extensions
Many of these methods are variations on the already existing LINQ methods. You might expect these to have been present in the .NET Framework already, but you sometimes miss a few.
Add
- Add multiple items to a collection by means of a comma separated argument list, e.g.
myCollection.Add(1, 5, 12);
- Add multiple items to a collection by means of a comma separated argument list, e.g.
Add
/Remove
- For some polymorphism between
Lists
,Stacks
andQueues
, there are theseAdd
andRemove
extension methods, in place ofPush
,Pop
,Enqueue
andDequeue
.
- For some polymorphism between
AddRange
AddRange
is a member ofList
<T
>. Here are variations forIEnumerable
<T
> andHashSet
<T
>.
AsArray
,AsList
,AsEnumerable
- Converts a single item to an array, list or enumerable. Example:
List
<int
>myInts = 3.AsList();
(The namesToArray
andToList
conflicted with other overloads.)
- Converts a single item to an array, list or enumerable. Example:
Concat
variations with:- A comma separated argument list, e.g.
myCollection.Concat(4, 7, 12);
- A single item, e.g.
myCollection.Concat(myItem);
- Starts with a single item and then adds a collection to it e.g.
myItem.Concat(myCollection);
- A comma separated argument list, e.g.
CrossJoin
- Returns all combinations of items combining one from collection 1 with one from collection 2, one from collection 3, etc.
Distinct
- Variation that takes a key selector that determines what makes an item unique, e.g.
myItems.Distinct(x =
>x.LastName);
For multi-part as keys, use:myItems.Distinct(x =
>new { x.FirstName, x.LastName });
- Variation that takes a key selector that determines what makes an item unique, e.g.
DistinctMany
- Returns distinct arrays. So checks the equality of all items in the arrays and returns the unique arrays. (Actually, not only arrays, any enumerable.) E.g.
severalArrays.DistinctMany();
- Returns distinct arrays. So checks the equality of all items in the arrays and returns the unique arrays. (Actually, not only arrays, any enumerable.) E.g.
Except
variations with:- A single item, e.g.
myCollection.Except(myItem);
- A predicate, e.g.
myCollection.Except(x =
>string.Equals(
"Blah
"));
- The choice to keep duplicates. (The original
Except
method from .NET automatically does a distinct, which is something you do not always want.)
- A single item, e.g.
FirstWithClearException
/SingleOrDefaultWithClearException
/SingleWithClearException
- The regular
First
,Single
orSingleOrDefault
methods will give you an exception for instance when there are no items in the collection or if there are multiple items in the collection. But not a very clear exception, like 'Sequence contains no elements.' TheWithClearException
variations will allow you to do aFirst
,Single
orSingleOrDefault
, but get a clearer exception message e.g. 'Product with key { productNumber = 123 } not unique.'
- The regular
ForEach
- Not all collection types have the
ForEach
method. Here you have an overload forIEnumerable
<T
> so you can use it for more collection types.
- Not all collection types have the
IndexOf
variations for:- Collection types other than
List
<T
>. - One that takes a predicate. E.g.
int index = items.IndexOf(x =
>x.ID == 3);
(like theList
<T
>.FindIndex
method)
- Collection types other than
TryGetIndexOf
- Similar to
IndexOf
, but returns null if the item is not found.
- Similar to
MinOrDefault
/MaxOrDefault
- Same as
Min
andMax
, but instead of crashing when zero items, returns default instead.
- Same as
PeekOrDefault
/PopOrDefault
- Same as
Peek
orPop
forStacks
andQueues
, but instead of crashing when zero items, returns default instead.
- Same as
Product
- Works similar to
Sum
, but instead of adding up all the numbers, all the numbers are multiplied.
- Works similar to
RemoveFirst
/TryRemoveFirst
- Removes the first occurrence that matches the predicate. Example:
myCollection.RemoveFirst(x =
>x.ID == 3);
- Removes the first occurrence that matches the predicate. Example:
Repeat
- Repeats a collection a number of times and returns a new collection. E.g.
new[] { 1, 2 }.Repeat(3);
returns a collection{ 1, 2, 1, 2, 1, 2 }
- Repeats a collection a number of times and returns a new collection. E.g.
ToHashSet
- Yes, you could write
new HashSet(someCollection)
. But there are already theToArray()
andToList()
methods, so why not aToHashSet()
method to make it consistent?
- Yes, you could write
ToNonUniqueDictionary
- Similar to
ToDictionary
, but allows the same key to be present more than once. AGroupBy
can overcome that limitation too, but sometimes it is nice to have the return value be aDictionary
, which might make for a faster lookup too.
- Similar to
TrimAll
- Trims all the strings in the collection.
Union
variations with:- A comma separated argument list, e.g.
myCollection.Union(4, 7, 12);
- A single item, e.g.
myCollection.Union(myItem);
- Starts with a single item and then adds a collection to it e.g.
myItem.Union(myCollection);
- A comma separated argument list, e.g.
Zip
variations:- Without a result, when you just want to process two collections side by side in another way.
- Without a result selector and returns tuples instead.
Recursive Collection Extensions
LINQ methods allow you to process a whole collection of items in one blow. Process a whole tree of items in one blow? For many cases these Recursive Collection Extensions offer a one-line solution.
Enumerate Descendants
This line of code:
var allItems = root.UnionRecursive(x => x.Children);
Gives you a list of all the nodes in a tree structure like the following:
var root = new Item
{
Children = new[]
{
new Item()
new Item
{
Children = new[]
{
new Item()
}
},
new Item
{
Children = new[]
{
new Item(),
new Item(),
}
},
}
};
There is also a SelectRecursive
method:
var allItemsExceptRoot = root.SelectRecursive(x => x.Children);
The difference with UnionRecursive
is that it does not include the root in the result collection.
You can also use a collection as a starting point:
var allItems = myRootItems.UnionRecursive(x => x.Children);
And there are other overloads for specialized use-cases.
Enumerate Ancestors
On top of enumerating descendants you can also enumerate ancestors:
var ancestors = child.SelectAncestors(x => x.Parent);
A variation on that, which includes the child node:
var items = child.SelfAndAncestors(x => x.Parent);
Circular References / Graphs
Circularities in the structure are gracefully handled, so you can also recursively process non-tree graphs, processing all the items in one blow.
KeyValuePairHelper
Converts a single array to KeyValuePair
or Dictionary
, where the first item is a name, the second a value, the third a name, the fourth a value. This can be useful to be able to specify name/value pairs as params
(variable amount of arguments). For instance:
void MyMethod(params object[] namesAndValues)
{
var dictionary = KeyValuePairHelper.ConvertNamesAndValuesListToDictionary(namesAndValues);
// ...
}
Calling MyMethod looks like this:
MyMethod("Name1", 3, "Name2", 5, "Name3", 6);
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
-
- JetBrains.Annotations (>= 2018.2.1)
- JJ.Framework.Common (>= 1.5.0 && < 1.6.0)
- JJ.Framework.Exceptions (>= 1.5.0 && < 1.6.0)
- JJ.Framework.Reflection (>= 1.5.0 && < 1.6.0)
- System.ValueTuple (>= 4.5.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on JJ.Framework.Collections:
Package | Downloads |
---|---|
JJ.Framework.Mathematics
Interpolator Smooth Bezier Cubic Hermite TextPlotter NumberBases ToBase FromBase ToHex FromHex ToLetterSequence FromLetterSequence Randomizer GetRandomItem GetInt32 GetDouble GetSingle IsInRectangle GetCenter AbsoluteDistance IsPowerOf2 LogRatio RoundToSignificantDigits RoundWithStep ScaleLinearly SpeadItems SpreadIntegers SpreadDoubles. Integer variation of Pow and Log. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.5.6877.41334 | 2,245 | 10/31/2018 |
1.4.6870.36368 | 755 | 10/23/2018 |
1.4.6862.41385 | 870 | 10/15/2018 |
1.3.6681.33429 | 1,292 | 4/17/2018 |
1.2.6640.39181 | 934 | 3/7/2018 |
1.1.6637.34225 | 971 | 3/4/2018 |