RockHouse.Collections is a Collection class package that mimics the Java language API and the Apache.Commons.CollectionsAPI. It also adds several proprietary APIs.
The following is an example of a class.
- CollectionUtils
- DictionaryUtils
- EnumerableUtils
- ListUtils
- SetUtils
- Dictionaries.HashMap
- Dictionaries.LinkedHashMap
- Dictionaries.LinkedOrderedDictionary
- Dictionaries.ListOrderedDictionary
- Dictionaries.LruMap/Dictionary
- Dictionaries.ReferenceDictionary
- Dictionaries.WeakHashMap
- Dictionaries.Multi.HashSetValuedDictionary
- Dictionaries.Multi.ListValuedMap/Dictionary
- Sets.LinkedHashSet
- Sets.LinkedOrderedSet
- Sets.ListOrderedSet
etc
var map = new HashMap<string, string>();
if (map.IsEmpty)
{
Console.WriteLine("empty");
}
var get1 = map.Get("a"); // get1 is null
var put1 = map.Put("a", "1"); // put1 is null
var get2 = map.Get("a"); // get2 is 1
var put2 = map.Put("a", "2"); // put2 is 1
var get3 = map.Get("a"); // get3 is 2
Note: There are some incompatibilities with Java. Since null is not allowed in primitive types, the API that returns null substitutes default(V). For example, default(int) is 0.
var list = new List<int>();
ListUtils.Push(list, 1, 2);
var tail = ListUtils.Pop(list); // tail is 2
ListUtils.Unshift(list, 0);
var head = ListUtils.Shift(list); // head is 0
var map = new HashMap<string, int>()
{
{ "a", 1 },
{ "b", 2 },
{ "c", 3 },
};
map["key"] = 10;
var value = map["key"]; // value is 10
var abort = map["not found"]; // KeyNotFoundException
foreach (var entry in map)
{
Console.WriteLine($"key={entry.Key}, value={entry.Value}");
}
var filteredList = map.Where(e => e.Value > 1)
.Select(e => e.Key)
.ToList();
With System.Text.Json
var json = JsonSerializer.Serialize(map);
var restore = JsonSerializer.Deserialize<HashMap<string, int>>(json);
This code is under the Apache License, Version 2.0.