# Java Collections & Streams

> Collections framework, Stream API, functional interfaces, and Optional patterns.

- **Type:** Skill
- **Install:** `agentstack add skill-ngxtm-devkit-java-collections-streams`
- **Verified:** Yes — security-reviewed for prompt injection and unsafe behavior
- **Seller:** [ngxtm](https://agentstack.voostack.com/s/ngxtm)
- **Installs:** 0
- **Category:** [Agent Skills](https://agentstack.voostack.com/c/agent-skills)
- **Latest version:** 0.1.0
- **License:** MIT
- **Upstream author:** [ngxtm](https://github.com/ngxtm)
- **Source:** https://github.com/ngxtm/devkit/tree/main/rules/java/java-collections-streams

## Install

```sh
agentstack add skill-ngxtm-devkit-java-collections-streams
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

# Java Collections & Streams Standards

## Collections Framework

```java
// Choose the right collection
List list = new ArrayList<>();     // Random access, dynamic size
List linked = new LinkedList<>();  // Frequent insertions/deletions
Set set = new HashSet<>();         // Unique elements, O(1) lookup
Set sorted = new TreeSet<>();      // Sorted unique elements
Map map = new HashMap<>();           // Key-value pairs, O(1) lookup
Map linked = new LinkedHashMap<>();  // Insertion order preserved
Map tree = new TreeMap<>();          // Sorted by keys

// Immutable collections (Java 9+)
List immutable = List.of("a", "b", "c");
Set immutableSet = Set.of(1, 2, 3);
Map immutableMap = Map.of("a", 1, "b", 2);

// Unmodifiable wrappers
List unmodifiable = Collections.unmodifiableList(mutableList);
// Or with copyOf (Java 10+)
List copy = List.copyOf(mutableList);
```

## Stream API

```java
// Basic pipeline
List result = items.stream()
    .filter(item -> item.isActive())
    .map(Item::getName)
    .sorted()
    .distinct()
    .collect(Collectors.toList());

// Parallel streams (use with caution)
long count = largeList.parallelStream()
    .filter(this::expensiveCheck)
    .count();

// FlatMap for nested structures
List allTags = posts.stream()
    .flatMap(post -> post.getTags().stream())
    .distinct()
    .toList();  // Java 16+

// Reduce operations
int sum = numbers.stream()
    .reduce(0, Integer::sum);

Optional max = numbers.stream()
    .reduce(Integer::max);
```

## Collectors

```java
// Group by
Map> byStatus = orders.stream()
    .collect(Collectors.groupingBy(Order::getStatus));

// Group by with downstream collector
Map countByStatus = orders.stream()
    .collect(Collectors.groupingBy(
        Order::getStatus,
        Collectors.counting()
    ));

// Partition by predicate
Map> partitioned = users.stream()
    .collect(Collectors.partitioningBy(User::isActive));

// To map
Map byId = users.stream()
    .collect(Collectors.toMap(
        User::getId,
        Function.identity(),
        (existing, replacement) -> existing  // Merge function
    ));

// Joining strings
String csv = items.stream()
    .map(Item::getName)
    .collect(Collectors.joining(", "));

// Statistics
IntSummaryStatistics stats = orders.stream()
    .collect(Collectors.summarizingInt(Order::getQuantity));
```

## Optional

```java
// Return Optional for nullable results
public Optional findById(String id) {
    return Optional.ofNullable(repository.get(id));
}

// Chain operations
String email = findById(id)
    .filter(User::isActive)
    .map(User::getEmail)
    .orElse("unknown@example.com");

// Throw on absence
User user = findById(id)
    .orElseThrow(() -> new UserNotFoundException(id));

// Conditional execution
findById(id).ifPresent(this::sendNotification);

// With fallback computation
User user = findById(id)
    .orElseGet(() -> createDefaultUser());

// Stream of Optional (Java 9+)
List activeUsers = ids.stream()
    .map(this::findById)
    .flatMap(Optional::stream)
    .filter(User::isActive)
    .toList();
```

## Best Practices

1. **Prefer immutable collections** from factories (List.of, Set.of, Map.of)
2. **Size collections** when capacity is known: `new ArrayList<>(100)`
3. **Use appropriate collection** for use case (HashMap vs TreeMap)
4. **Avoid null in collections** - use Optional or empty collections
5. **Stream once** - streams cannot be reused after terminal operation
6. **Parallel streams carefully** - only for CPU-bound, large datasets
7. **Optional for returns only** - never as fields or parameters

## References

- [Stream Pipelines](references/stream-pipelines.md) - Advanced stream patterns
- [Collectors Patterns](references/collectors-patterns.md) - Custom collectors, complex aggregations

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [ngxtm](https://github.com/ngxtm)
- **Source:** [ngxtm/devkit](https://github.com/ngxtm/devkit)
- **License:** MIT

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** no
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** no
- **Dynamic code execution:** no

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: passed — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/skill-ngxtm-devkit-java-collections-streams
- Seller: https://agentstack.voostack.com/s/ngxtm
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
