< back to profile

cactoos

Object-Oriented Java primitives, as an alternative to Google Guava and Apache Commons

Javarepo ->live ->Apr 3, 2026
cactoos screenshot

Object-Oriented Java Primitives

EO principles respected here DevOps By Rultor.com We recommend IntelliJ IDEA

mvn Javadoc PDD status Maven Central License Test Coverage SonarQube Hits-of-Code Codacy Badge

Project architect: @victornoel

Cactoos is a collection of object-oriented Java primitives.

We are not happy with JDK, Guava, and Apache Commons because they are procedural and not object-oriented. They do their job, but mostly through static methods. Cactoos is suggesting to do almost exactly the same, but through objects.

These are the design principles behind Cactoos.

The library has no dependencies. All you need is this:

Maven:

xml
<dependency>
  <groupId>org.cactoos</groupId>
  <artifactId>cactoos</artifactId>
  <version>0.57.0</version>
</dependency>

Gradle:

groovy
dependencies {
  compile 'org.cactoos:cactoos::0.57.0'
}

Java version required: 1.8+.

StackOverflow tag is cactoos.

Input/Output

More about it here: Object-Oriented Declarative Input/Output in Cactoos.

To read a text file in UTF-8:

java
String text = new TextOf(
  new File("/code/a.txt")
).asString();

To write a text into a file:

java
new LengthOf(
  new TeeInput(
    "Hello, world!",
    new File("/code/a.txt")
  )
).value();

To read a binary file from classpath:

java
byte[] data = new BytesOf(
  new ResourceOf("foo/img.jpg")
).asBytes();

Text/Strings

To format a text:

java
String text = new FormattedText(
  "How are you, %s?",
  name
).asString();

To manipulate text:

java
// To lower case
new Lowered(
  new TextOf("Hello")
);
// To upper case
new Upper(
  new TextOf("Hello")
);

Iterables/Collections/Lists/Sets

More about it here: Lazy Loading and Caching via Sticky Cactoos Primitives.

To filter a collection:

java
Collection<String> filtered = new ListOf<>(
  new Filtered<>(
    s -> s.length() > 4,
    new IterableOf<>("hello", "world", "dude")
  )
);

To flatten one iterable:

java
new Joined<>(
  new Mapped<IterableOf<>>(
    iter -> new IterableOf<>(
      new ListOf<>(iter).toArray(new Integer[]{})
    ),
    new IterableOf<>(1, 2, 3, 4, 5, 6)
  )
);    // Iterable<Integer>

To flatten and join several iterables:

java
new Joined<>(
  new Mapped<IterableOf<>>(
    iter -> new IterableOf<>(
      new Joined<>(iter)
    ),
    new Joined<>(
      new IterableOf<>(new IterableOf<>(1, 2, 3)),
      new IterableOf<>(new IterableOf<>(4, 5, 6))
    )
  )
);    // Iterable<Integer>

To iterate a collection:

java
new And(
  new Mapped<>(
    new FuncOf<>(
      input -> {
        System.out.printf("Item: %s\n", input);
      },
      new True()
    ),
    new IterableOf<>("how", "are", "you", "?")
  )
).value();

Or even more compact:

java
new ForEach<>(
  input -> System.out.printf(
    "Item: %s\n", input
  )
).exec(new IterableOf<>("how", "are", "you", "?"));

To sort a list of words in the file:

java
List<Text> sorted = new ListOf<>(
  new Sorted<>(
    new Mapped<>(
      text -> new ComparableText(text),
      new Split(
        new TextOf(
          new File("/tmp/names.txt")
        ),
        new TextOf("\\s+")
      )
    )
  )
);

To count elements in an iterable:

java
int total = new LengthOf(
  new IterableOf<>("how", "are", "you")
).value().intValue();

To create a set of elements by providing variable arguments:

java
final Set<String> unique = new SetOf<>(
  "one",
  "two",
  "one",
  "three"
);

To create a set of elements from an existing iterable:

java
final Set<String> words = new SetOf<>(
  new IterableOf<>("abc", "bcd", "abc", "ccc")
);

To create a sorted iterable with unique elements from an existing iterable:

java
final Iterable<String> sorted = new Sorted<>(
  new SetOf<>(
    new IterableOf<>("abc", "bcd", "abc", "ccc")
  )
);

To create a sorted set from existing vararg elements using a comparator:

java
final Set<String> sorted = new org.cactoos.set.Sorted<>(
  (first, second) -> first.compareTo(second),
  "abc", "bcd", "abc", "ccc", "acd"
);

To create a sorted set from an existing iterable using a comparator:

java
final Set<String> sorted = new org.cactoos.set.Sorted<>(
  (first, second) -> first.compareTo(second),
  new IterableOf<>("abc", "bcd", "abc", "ccc", "acd")
);

Funcs and Procs

This is a traditional foreach loop:

java
for (String name : names) {
  System.out.printf("Hello, %s!\n", name);
}

This is its object-oriented alternative (no streams!):

java
new And(
  n -> {
    System.out.printf("Hello, %s!\n", n);
    return new True().value();
  },
  names
).value();

This is an endless while/do loop:

java
while (!ready) {
  System.out.println("Still waiting...");
}

Here is its object-oriented alternative:

java
new And(
  ready -> {
    System.out.println("Still waiting...");
    return !ready;
  },
  new Endless<>(booleanParameter)
).value();

Dates and Times

From our org.cactoos.time package.

Our classes are divided in two groups: those that parse strings into date/time objects, and those that format those objects into strings.

For example, this is the traditional way of parsing a string into an OffsetDateTime:

java
final OffsetDateTime date = OffsetDateTime.parse("2007-12-03T10:15:30+01:00");

Here is its object-oriented alternative (no static method calls!) using OffsetDateTimeOf, which is a Scalar:

java
final OffsetDateTime date = new OffsetDateTimeOf("2007-12-03T10:15:30+01:00").value();

To format an OffsetDateTime into a Text:

java
final OffsetDateTime date = ...;
final String text = new TextOfDateTime(date).asString();

Our Objects vs. Their Static Methods

CactoosGuavaApache CommonsJDK 8
AndIterables.all()--
FilteredIterables.filter()?-
FormattedText--String.format()
IsBlank-StringUtils.isBlank()-
Joined--String.join()
LengthOf--String#length()
Lowered--String#toLowerCase()
Normalized-StringUtils.normalize()-
OrIterables.any()--
Repeated-StringUtils.repeat()-
Replaced--String#replace()
Reversed--StringBuilder#reverse()
Rotated-StringUtils.rotate()-
Split--String#split()
StickyListLists.newArrayList()?Arrays.asList()
Sub--String#substring()
SwappedCase-StringUtils.swapCase()-
TextOf?IOUtils.toString()-
TrimmedLeft-StringUtils.stripStart()-
TrimmedRight-StringUtils.stripEnd()-
Trimmed-StringUtils.stripAll()String#trim()
Upper--String#toUpperCase()

Questions

Ask your questions related to cactoos library on Stackoverflow with the cactoos tag.

How to Contribute

Just fork the repo and send us a pull request.

Make sure your branch builds without any warnings/issues:

bash
mvn clean verify -Pqulice

To run a build similar to the CI with Docker only, use:

bash
docker run \
  --tty \
  --interactive \
  --workdir=/main \
  --volume=${PWD}:/main \
  --volume=cactoos-mvn-cache:/root/.m2 \
  --rm \
  maven:3-jdk-8 \
  bash -c "mvn clean install -Pqulice; chown -R $(id -u):$(id -g) target/"

To remove the cache used by Docker-based build:

bash
docker volume rm cactoos-mvn-cache

Note: Checkstyle is used as a static code analysis tool with checks list in GitHub precommits.

Contributors

Command Palette

Search hackers, navigate pages