AgentStack
SKILL verified MIT Self-run

Java Collections & Streams

skill-ngxtm-devkit-java-collections-streams · by ngxtm

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

No reviews yet
0 installs
17 views
0.0% view→install

Install

$ agentstack add skill-ngxtm-devkit-java-collections-streams

✓ scanned · ✓ verified — works with Claude Code, Cursor, and more.

Security review

✓ Passed

No issues found. Passed automated security review. · v0.1.0 How review works →

  • Prompt-injection patterns
  • Secret / credential exfiltration
  • Dangerous shell & filesystem operations
  • Untrusted network calls
  • Known-malicious package signatures

What it can access

  • Network access No
  • Filesystem access No
  • Shell / process execution No
  • Environment & secrets No
  • Dynamic code execution No

From automated source analysis of v0.1.0. “Used” means the capability is present in the source — more access means more to trust, not that it’s unsafe.

Are you the author of Java Collections & Streams? Claim this listing to set pricing, connect Stripe payouts, and keep 70% of every sale.
Sign up to claim

About

Java Collections & Streams Standards

Collections Framework

// 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

// 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

// 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

// 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.

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

Reviews

No reviews yet — be the first.

Versions

  • v0.1.0 Imported from the upstream source.