This is an automated email from the ASF dual-hosted git repository.

jbonofre pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-java.git


The following commit(s) were added to refs/heads/main by this push:
     new 1f461ae6f GH-40: Add MapVector usage example (#1475)
1f461ae6f is described below

commit 1f461ae6f0c78e014d553ff1c93cba729c1c903f
Author: Yash chauhan <[email protected]>
AuthorDate: Thu Nov 26 20:52:20 2026 +0530

    GH-40: Add MapVector usage example (#1475)
    
    ## Summary
    
    Add a `MapVector` usage example to the Apache Arrow Java documentation.
    
    This contribution complements the Java Cookbook example in
    
[[apache/arrow-cookbook#419](https://github.com/apache/arrow-cookbook/pull/419)](https://github.com/apache/arrow-cookbook/pull/419)
    by providing a corresponding example directly in the Arrow Java
    documentation.
    
    The example demonstrates:
    - Creating and initializing a `MapVector`.
    - Writing key-value entries using `UnionMapWriter`.
    - Reading map entries using `UnionMapReader`.
    - Working with BIGINT keys and INT values.
    
    Closes #40.
---
 docs/source/vector.rst | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 80 insertions(+)

diff --git a/docs/source/vector.rst b/docs/source/vector.rst
index 199627744..8944d1cc4 100644
--- a/docs/source/vector.rst
+++ b/docs/source/vector.rst
@@ -268,6 +268,86 @@ For example, the code below shows how to build a 
:class:`ListVector` of int's us
      }
   }
 
+Building MapVector
+==================
+
+A :class:`MapVector` stores a collection of key-value pairs at each index.
+Each map entry consists of a non-nullable key and a value that can be nullable.
+The key and value can have different data types.
+
+The following example creates a MapVector with BIGINT keys and INT values.
+The :class:`UnionMapWriter` is used to write entries into the map. Each entry
+must be started with ``startEntry()`` and completed with ``endEntry()``.
+After all entries have been written, ``endMap()`` completes the map.
+
+.. code-block:: Java
+
+    try (BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE);
+      MapVector mapVector = MapVector.empty("map", allocator, false)) {
+
+      mapVector.allocateNew();
+
+      UnionMapWriter writer = mapVector.getWriter();
+
+      // Write the first map: {1L -> 100, 2L -> 200}
+      writer.setPosition(0);
+      writer.startMap();
+
+      writer.startEntry();
+      writer.key().bigInt().writeBigInt(1L);
+      writer.value().integer().writeInt(100);
+      writer.endEntry();
+
+      writer.startEntry();
+      writer.key().bigInt().writeBigInt(2L);
+      writer.value().integer().writeInt(200);
+      writer.endEntry();
+
+      writer.endMap();
+
+      // Write the second map: {3L -> 300}
+      writer.setPosition(1);
+      writer.startMap();
+
+      writer.startEntry();
+      writer.key().bigInt().writeBigInt(3L);
+      writer.value().integer().writeInt(300);
+      writer.endEntry();
+
+      writer.endMap();
+
+      writer.setValueCount(2);
+
+      // Read the maps
+      UnionMapReader reader = mapVector.getReader();
+
+      for (int i = 0; i < mapVector.getValueCount(); i++) {
+        reader.setPosition(i);
+
+        while (reader.next()) {
+          long key = reader.key().readLong();
+          int value = reader.value().readInteger();
+
+          System.out.println(key + " -> " + value);
+        }
+      }
+    }
+
+The output is:
+
+.. code-block:: text
+
+    1 -> 100
+    2 -> 200
+    3 -> 300
+
+The example demonstrates that the key and value writers can use different
+data types. For example, ``bigInt()`` is used for keys while ``integer()``
+is used for values.
+
+The child vectors are initialized by the writer as entries are written,
+so there is no need to manually construct the key and value child fields.
+
 Dictionary Encoding
 ===================
 

Reply via email to