Skip to main content

Collections

BBj gives you two families of collections: its own BBjVector (a dynamic list created through BBjAPI) and direct access to the entire Java Collections Framework. For dynamic lists, BBjVector is the BBj-native choice. For key-value storage, use java.util.HashMap directly -- BBjHashMap is deprecated.

At a Glance

CollectionCreationUse Case
BBjVectorBBjAPI().makeVector()Dynamic list (BBj native)
java.util.HashMapnew HashMap()Key-value pairs
java.util.ArrayListnew ArrayList()Dynamic list (Java native)
DIM arraydim a$[10]Fixed-size typed array
VECTOR()vector(array[])Convert DIM array to BBjVector

For Java, Python, and C# Developers

TaskJavaPythonC#BBj
Create listnew ArrayList()[]new List<T>()BBjAPI().makeVector()
Add itemlist.add(item)list.append(item)list.Add(item)vec!.add(item)
Get by indexlist.get(i)list[i]list[i]vec!.get(i)
Iterate listfor (var x : list)for x in list:foreach (var x in list)for i=0 to vec!.size()-1 ... next i
Create mapnew HashMap(){}new Dictionary<K,V>()new HashMap() (Java)
Get by keymap.get(k)d[k]d[k]map!.get(k)
Check containsmap.containsKey(k)k in dd.ContainsKey(k)map!.containsKey(k)
Collection sizelist.size()len(list)list.Countvec!.size()

BBj uses BBjVector for ordered lists and java.util.HashMap for key-value maps. Since BBj runs on the JVM, you can use Java collection classes directly. For the complete cross-language reference, see BBj for Java, Python, and C# Developers.

Complete Runnable Examples

This chapter's code snippets illustrate individual concepts. For complete, runnable programs you can open directly in the BBj IDE, see the sample files in samples/06-collections/:

  • bbjvector_basics.bbj -- BBjVector creation, adding items, and iteration
  • arraylist_usage.bbj -- java.util.ArrayList creation and iteration
  • hashmap_iterator.bbj -- java.util.HashMap with keySet().iterator()
  • array_to_vector.bbj -- DIM array to BBjVector conversion with VECTOR()

See Running Samples for setup instructions.