Py4J - A Bridge between Python and Java

### Navigation

- index
- modules |
- next |
- previous |
- Home |
- Download |
- Doc |
- Blog |
- github |
- About »

# 4.3. `py4j.java_collections` — Py4J Collections API

The `py4j.java_collections` module maps Python collection classes with Java collection classes. These classes should practically never be directly instantiated by users: they are automatically used when receiving, for example, a Java list.

## 4.3.1. JavaIterator

class `py4j.java_collections.``JavaIterator`(target_id, gateway_client)

  Maps a Python list iterator to a Java list iterator.

  The JavaIterator follows the Python iterator protocol and raises a StopIteration error when the iterator can no longer iterate.

  `next`()

    This next method wraps the next method in Java iterators.

    The Iterator.next() method is called and if an exception occur (e.g., NoSuchElementException), a StopIteration exception is raised.

## 4.3.2. JavaList

class `py4j.java_collections.``JavaList`(target_id, gateway_client)

  Maps a Python list to a Java list.

  All operations possible on a Python list are implemented. For example, slicing (e.g., list[1:3]) will create a copy of the list on the JVM. Slicing is thus not equivalent to subList(), because a modification to a slice such as the addition of a new element will not affect the original list.

  `append`(value)
  `count`(value)
  `extend`(other_list)
  `index`(value)
  `insert`(key, value)
  `pop`(key=None)
  `remove`(value)
  `reverse`()
  `sort`()

## 4.3.3. JavaMap

class `py4j.java_collections.``JavaMap`(target_id, gateway_client)

  Maps a Python Dictionary to a Java Map.

  All operations possible on a Python dict are implemented.

## 4.3.4. JavaSet

class `py4j.java_collections.``JavaSet`(target_id, gateway_client)

  Maps a Python Set to a Java Set.

  All operations possible on a Python set are implemented.

  `add`(value)
  `clear`()
  `discard`(value)
  `remove`(value)

## 4.3.5. JavaArray

class `py4j.java_collections.``JavaArray`(target_id, gateway_client)

  Maps a Java Array to a Semi-Mutable Sequence: elements inside the sequence can be modified, but the length of the sequence cannot change.

  The backing collection is a Sequence and not a Python array because these arrays only accept primitives whereas Java arrays work for any types.

### Questions/Feedback?

- Try the FAQ -- It answers many questions.
- Search for information in the archives of the py4j-users mailing list, or post a question.
- If you notice errors with this documentation, open a ticket and let us know. Please, only open a ticket for feedback or improvements on the docs. To get help, use the links above!

### Navigation

- index
- modules |
- next |
- previous |
- Home |
- Download |
- Doc |
- Blog |
- github |
- About »