Some common datastructures for Java
This is a collection of some common datastructures that describe everyday use cases. It is mainly intended for API providers who don't want to redeclare somewhat trivial datastructures all the time.
Overview
This library is hosted in the Maven Central Repository. You can use it with the following coordinates:
<dependency>
<groupId>net.markenwerk</groupId>
<artifactId>commons-datastructures</artifactId>
<version>1.3.1</version>
</dependency>
Consult the usage description and Javadoc for further information.
Datastructures
Box
This library provides the generic Box
, which is an mutable container class that holds a single value.
Foo value = ...
// creates a container that holds value
Box<Foo> box = new Box<>(value);
// returns the contained value
box.getValue();
// update the contained value
box.setValue(new Foo(...));
Wrapper
This library provides the generic Wrapper
, which is an immutable container class that holds a single value.
Foo value = ...
// creates a container that holds value
Wrapper<Foo> wrapper = new Wrapper<>(value);
// returns the contained value
wrapper.getValue();
Optional
This library provides the generic Optional
, which is an immutable container class that may hold a single value.
Foo value = ...
// creates a container that holds no value
Optional<Foo> emptyOptional = new Optional<>();
// creates a container that holds value
Optional<Foo> optional = new Optional<>(value);
// returns the contained value
optional.getValue();
// returns whether the optional has a value
optional.hasValue();
Pair
This library provides the generic Pair
, which is an immutable container class that holds two values of similar type.
Foo first = ...
Foo second = ...
// creates a container that holds first and second
Pair<Foo> pair = new Pair<>(first, second);
// returns the contained values
pair.getFirst();
pair.getSecond();
// creates new pair with one changed value
pair.withFirst(new Foo(...));
pair.withSecond(new Foo(...));
Tuple
This library provides the generic Tuple
, which is an immutable container class that holds two values of different type.
Foo first = ...
Bar second = ...
// creates a container that holds first and second
Tuple<Foo, Bar> tuple = new Tuple<>(first, second);
// returns the contained values
tuple.getFirst();
tuple.getSecond();
// creates new tuple with one changed value
tuple.withFirst(new Foo(...));
tuple.withSecond(new Bar(...));
Triple
This library provides the generic Triple
, which is an immutable container class that holds three values of different type.
Foo first = ...
Bar second = ...
Baz third = ...
// creates a container that holds first, second and third
Triple<Foo, Bar, Baz> triple = new Triple<>(first, second, third);
// returns the contained values
triple.getFirst();
triple.getSecond();
triple.getThird();
// creates new triple with one changed value
triple.withFirst(new Foo(...));
triple.withSecond(new Bar(...));
triple.withThird(new Baz(...));
Entry
This library provides the generic Entry
, which is an immutable container class that a key-value-pair.
Key key = ...
Value value = ...
// creates a container that holds key and value
Entry<Key, Value> entry = new Entry<>(key, value);
// returns the contained values
entry.getKey();
entry.getValue();
Either
This library provides the generic Either
, which is one of two distinct immutable container class that hold a single value; Left
and Right
.
Foo foo = ...
Bar bar = ...
// creates a left container that holds foo
Either<Foo, Bar> left = new Left<>(foo);
// creates a right container that holds bar
Either<Foo, Bar> right = new Right<>(bar);
// returns true
left.isLeft();
right.isRight();
// returns the contained values
left.getLeft();
right.getRight();
// returns false
left.isRight();
right.isLeft();