{"id":13485656,"url":"https://github.com/hrldcpr/pcollections","last_synced_at":"2025-12-29T22:29:24.525Z","repository":{"id":15208613,"uuid":"17937004","full_name":"hrldcpr/pcollections","owner":"hrldcpr","description":"A Persistent Java Collections Library","archived":false,"fork":false,"pushed_at":"2024-03-17T22:04:34.000Z","size":2649,"stargazers_count":765,"open_issues_count":17,"forks_count":78,"subscribers_count":32,"default_branch":"master","last_synced_at":"2024-10-30T20:45:29.037Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hrldcpr.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-03-20T09:28:01.000Z","updated_at":"2024-10-08T01:59:10.000Z","dependencies_parsed_at":"2024-03-17T23:20:54.472Z","dependency_job_id":null,"html_url":"https://github.com/hrldcpr/pcollections","commit_stats":null,"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hrldcpr%2Fpcollections","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hrldcpr%2Fpcollections/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hrldcpr%2Fpcollections/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hrldcpr%2Fpcollections/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hrldcpr","download_url":"https://codeload.github.com/hrldcpr/pcollections/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245910819,"owners_count":20692505,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T18:00:28.946Z","updated_at":"2025-12-29T22:29:24.520Z","avatar_url":"https://github.com/hrldcpr.png","language":"Java","readme":"# PCollections\n\nA Persistent Java Collections Library\n\n[![Maven Central](https://img.shields.io/maven-central/v/org.pcollections/pcollections.svg)](https://mvnrepository.com/artifact/org.pcollections/pcollections/latest)\n[![Javadoc](https://www.javadoc.io/badge/org.pcollections/pcollections.svg)](https://javadoc.io/doc/org.pcollections/pcollections/latest/org.pcollections/org/pcollections/package-summary.html)\n\n### Overview\n\nPCollections serves as a [persistent](https://en.wikipedia.org/wiki/Persistent_data_structure) and immutable analogue of the Java Collections Framework. This includes **efficient**, **thread-safe**, **generic**, **immutable**, and **persistent** stacks, maps, vectors, sets, and bags, **compatible** with their Java Collections counterparts.\n\nPersistent and immutable datatypes are increasingly appreciated as a **simple**, **design-friendly**, **concurrency-friendly**, and sometimes more time- and space-efficient alternative to mutable datatypes.\n\n### Persistent versus Unmodifiable\n\nNote that these immutable collections are very different from the immutable collections returned by Java's [Collections.unmodifiableCollection()](\u003chttps://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/Collections.html#unmodifiableCollection(java.util.Collection)\u003e) and similar methods. The difference is that Java's unmodifiable collections have no producers, whereas PCollections have very efficient producers.\n\nThus if you have an unmodifiable Collection `x` and you want a new Collection `x2` consisting of the elements of `x` in addition to some element `e`, you would have to do something like:\n\n```Java\nCollection x2 = new HashSet(x);\nx2.add(e);\n```\n\nwhich involves copying all of `x`, using linear time and space.\n\nIf, on the other hand, you have a PCollection `y` you can simply say:\n\n```Java\nPCollection y2 = y.plus(e);\n```\n\nwhich still leaves `y` untouched but generally requires little or no copying, using time and space much more efficiently.\n\n### Usage\n\nPCollections are created using producers and static factory methods. Some example static factory methods are [`HashTreePSet.empty()`](\u003chttps://javadoc.io/doc/org.pcollections/pcollections/latest/org.pcollections/org/pcollections/HashTreePSet.html#empty()\u003e) which returns an empty [PSet](https://javadoc.io/doc/org.pcollections/pcollections/latest/org.pcollections/org/pcollections/PSet.html), while `HashTreePSet.singleton(e)` returns a PSet containing just the element `e`, and `HashTreePSet.from(collection)` returns a PSet containing the same elements as `collection`. See [Example Code](#example-code) below for an example of using producers.\n\nThe same `empty()`, `singleton()`, and `from()` factory methods are found in each of the PCollections implementations, which currently include one concrete implementation for each abstract type:\n\n- [HashTreePMap](https://javadoc.io/doc/org.pcollections/pcollections/latest/org.pcollections/org/pcollections/HashTreePMap.html) provides a [PMap](https://javadoc.io/doc/org.pcollections/pcollections/latest/org.pcollections/org/pcollections/PMap.html) implementation, analogous to Java's HashMap.\n- [TreePMap](https://javadoc.io/doc/org.pcollections/pcollections/latest/org.pcollections/org/pcollections/TreePMap.html) provides a\n  [PSortedMap](https://javadoc.io/doc/org.pcollections/pcollections/latest/org.pcollections/org/pcollections/PSortedMap.html) implementation,\n  analogous to Java's TreeMap.\n- [ConsPStack](https://javadoc.io/doc/org.pcollections/pcollections/latest/org.pcollections/org/pcollections/ConsPStack.html) provides a [PStack](https://javadoc.io/doc/org.pcollections/pcollections/latest/org.pcollections/org/pcollections/PStack.html) implementation, analogous to Java's LinkedList.\n- [TreePVector](https://javadoc.io/doc/org.pcollections/pcollections/latest/org.pcollections/org/pcollections/TreePVector.html) provides a [PVector](https://javadoc.io/doc/org.pcollections/pcollections/latest/org.pcollections/org/pcollections/PVector.html) implementation, analogous to Java's ArrayList.\n- [HashTreePSet](https://javadoc.io/doc/org.pcollections/pcollections/latest/org.pcollections/org/pcollections/HashTreePSet.html) provides a [PSet](https://javadoc.io/doc/org.pcollections/pcollections/latest/org.pcollections/org/pcollections/PSet.html) implementation, analogous to Java's HashSet.\n- [TreePSet](https://javadoc.io/doc/org.pcollections/pcollections/latest/org.pcollections/org/pcollections/TreePSet.html) provides a\n  [PSortedSet](https://javadoc.io/doc/org.pcollections/pcollections/latest/org.pcollections/org/pcollections/PSortedSet.html) implementation,\n  analogous to Java's TreeSet.\n- [HashTreePBag](https://javadoc.io/doc/org.pcollections/pcollections/latest/org.pcollections/org/pcollections/HashTreePBag.html) provides a [PBag](https://javadoc.io/doc/org.pcollections/pcollections/latest/org.pcollections/org/pcollections/PBag.html) implementation, which is unordered like a set but can contain duplicate elements.\n\nPCollections are highly interoperable with Java Collections:\n\n- Every PCollection is a java.util.Collection.\n- Every PMap is a java.util.Map.\n- Every PSequence is a java.util.List.\n  - This includes every PStack and every PVector.\n- Every PSet is a java.util.Set.\n- Every PSortedMap is a java.util.SortedMap and java.util.NavigableMap.\n- Every PSortedSet is a java.util.SortedSet and java.util.NavigableSet.\n\nPCollections uses [Semantic Versioning](https://semver.org/), which establishes a strong correspondence between API changes and version numbering.\n\nPCollections is in the [Maven Central repository](https://search.maven.org/search?q=g:org.pcollections), under org.pcollections. Thus the Maven coordinates for PCollections are:\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.pcollections\u003c/groupId\u003e\n    \u003cartifactId\u003epcollections\u003c/artifactId\u003e\n    \u003cversion\u003e5.0.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\nor Gradle:\n\n```groovy\ncompile 'org.pcollections:pcollections:5.0.0'\n```\n\n### Example Code\n\nThe following gives a very simple example of using PCollections, including the static factory method HashTreePSet.empty() and the producer plus(e):\n\n```Java\nimport org.pcollections.*;\n\npublic class Example {\n  public static void main(String... args) {\n    PSet\u003cString\u003e set = HashTreePSet.empty();\n    set = set.plus(\"something\");\n\n    System.out.println(set);\n    System.out.println(set.plus(\"something else\"));\n    System.out.println(set);\n  }\n}\n```\n\nRunning this program gives the following output:\n\n```\n[something]\n[something else, something]\n[something]\n```\n\n### Building from source\n\nTo build the project from source [clone the repository](https://github.com/hrldcpr/pcollections) and then run `./gradlew`\n\n### Related Work\n\n[Clojure](https://clojure.org/reference/data_structures), [Scala](https://docs.scala-lang.org/overviews/collections-2.13/introduction.html), and [kotlinx.collections.immutable](https://github.com/Kotlin/kotlinx.collections.immutable) also provide persistent collections on the JVM, but they are less interoperable with Java. Both [Guava](https://guava.dev/releases/19.0/api/docs/com/google/common/collect/ImmutableCollection.html) and [java.util.Collections](\u003chttps://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/Collections.html#unmodifiableCollection(java.util.Collection)\u003e) provide immutable collections but they are not persistent—that is, they do not provide efficient producers—so they are not nearly as useful. See [Persistent versus Unmodifiable](#persistent-versus-unmodifiable) above.\n","funding_links":[],"categories":["Projects","Java","项目","Memory and concurrency","\u003ca name=\"Java\"\u003e\u003c/a\u003eJava"],"sub_categories":["Data Structures","数据结构"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhrldcpr%2Fpcollections","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhrldcpr%2Fpcollections","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhrldcpr%2Fpcollections/lists"}