-> supplier.get()); } public static void main(String[] args) { Function<String, List<String>> dict = defaultDict(new HashMap<>(), ArrayList::new); dict.apply("foo").add("bar"); } PHP PHP arrays are natively autovivificious. $arr = array(); $arr["express"][3] = "stand and deliver"; However, this only applies to assignment, and not array access. JavaScript ES6 introduces a new class that can be used to implement autovivification. With other features of JavaScript, this can be reduced to a single line of code: var tree = () => new Proxy({}, { get: (target, name) => name in target ? target[name] : target[name] = tree() }); // Test: var t = tree(); t.first.second.third = 'text'; console.log(t.first.second.third); // or t['first']['second']['third'] C# C#, using indexers and C# 4.0 dynamics, class Tree { private IDictionary<string, object> _dict = new Dictionary<string, object>(); public dynamic this[string key] { get { return _dict.ContainsKey(key) ? _dict[key] : _dict[key] = new Tree(); } set { _dict[key] = value; } } } // Test: var t = new Tree(); t["first"]["second"]["third"] = "text"; Console.WriteLine(t["first"]["second"]["third"]); DynamicObject can be used for implementing different syntaxes also, using System; using System.Collections.Generic; using System.Dynamic; class Tree : DynamicObject { private IDictionary<object, object> dict = new Dictionary<object, object>(); // for t.first.second.third syntax public override bool TryGetMember(GetMemberBinder binder, out object result) { var key = binder.Name; if (dict.ContainsKey(key)) result = dict[key]; else dict[key] = result = new Tree(); return true; } public override bool TrySetMember(SetMemberBinder binder, object value) { dict[binder.Name] = value; return true; } // for t["first"]["second"]["third"] syntax public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result) { var key = indexes[0]; if (dict.ContainsKey(key)) result = dict[key]; else dict[key] = result = new Tree(); return true; } public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value) { dict[indexes[0]] = value; return true; } } // Test: dynamic t = new Tree(); t.first.second.third = "text"; Console.WriteLine(t.first.second.third); // or, dynamic t = new Tree(); t["first"]["second"]["third"] = "text"; Console.WriteLine(t["first"]["second"]["third"]); See also Evaluation strategy Variable Vivification Notes References External links perl561delta: File and directory handles can be autovivified Autovivification in Perl: An In-Depth Tutorial Autovivification in Ruby - emulate Perl's autovivification A Use of the Y Combinator in Ruby - Implements autovivification in Ruby with the Y Combinator. Hash#autonew in the Ruby gem "facets" adds autovivification on hash reads The Ruby gem "xkeys" facilitates nested structure traversal and autovivifies on array or hash writes Perl Evaluation strategy Management cybernetics Articles with example Perl code Articles with example Python (programming language) code Articles with