Hi there,

so I'm currently studying Java at my University and as of right now we're working on Linked Lists, Array lists, Array Deques, etc. As part of this lesson, we're being told to write our own versions, both recursive and iterative, of the predefined java functions for its corresponding data structure (such as addLast, contains, removeLast, etc).

I am finding this to be a little difficult as I tend to struggle with what the optimal logic would look like, but I was wondering in what situations would I be in where I would every need to create my own version as opposed to just using the predefined functions? Is this a common course of action?

I know that we're being taught this in order to have a better understanding of the logic behind the data structures, but I am just curious as to where I would absolutely need to edit the base Java functions for specific use.

Thank you!

EDIT: Just noticed the spelling error in the title. How embarrassing!

Dani AI

Generated

As noted, reimplementing the Java collection APIs is an excellent learning exercise. As pointed out, it is uncommon to ship a home-grown replacement for java.util in production — the standard implementations are heavily tested and well tuned. Still, there are concrete, practical reasons a developer might write a custom version:

  • Tight performance or memory constraints (real-time systems, microservices with extreme GC sensitivity) where object allocation patterns or primitive-only storage matter.
  • Specialized semantics: bounded behavior, automatic eviction, identity-based equality, or additional invariants (e.g., “no duplicates ever” plus FIFO eviction).
  • Concurrency/latency needs that the standard concurrent collections don’t meet (custom wait-free or single-producer/single-consumer queues).
  • Instrumentation, auditing, or security wrappers that enforce validation or emit metrics at every mutation.
  • Integration with legacy systems or persistence layers that require unusual serialization or versioning.
  • UI or domain-specific components (echoing ’s custom JSpinner): sometimes a domain control needs a backing container with bespoke behavior.

When customization is required, favor composition/wrapping over wholesale reimplementation. A small wrapper can enforce rules while delegating everything else:

public class BoundedUniqueDeque<E> implements Deque<E> {
  private final Deque<E> d = new ArrayDeque<>();
  private final int max;
  public BoundedUniqueDeque(int max) { this.max = max; }
  @Override
  public void addLast(E e) {
    if (d.contains(e)) return;         // skip duplicates
    if (d.size() == max) d.removeFirst(); // bounded eviction
    d.addLast(e);
  }
  // delegate remaining methods to 'd' (IDE can generate)
}

Tips and cautions: measure before optimizing; write thorough unit and concurrency tests; document invariants clearly; prefer existing, well-maintained libraries (Guava, Eclipse Collections, fastutil) when they meet the need. The educational value of reimplementing core methods is high, but in production only replace standard implementations when profiling and requirements justify the extra maintenance cost.

Recommended Answers

All 4 Replies

In real life its rare to very rare that you write your own versions of Java API classes like those. Even if there is some small optimisation you could do for some highly specific application, you would have to offset that against the disadvantages of using "roll your own" code rather than the highly designed, tested, documented, supported API versions.

Obviously, as training exercises they are really good - lots of methods and arrays and thread-related issues to learn about and practice - plus there's no need to spend ages defining and understanding some artificial problem description.

What speling misteak?

Thanks for the info!

And the mistake was "Javan" in the title. :P

Fixed title; you can always try to get the title edited by "flagging" it as bad post.

Member Avatar for Member #647493

I've never had the need to rewrite predefined functions.
But rewriting redefined components, is a different story.
I have found it neccessary to write my own "JSpinner".
And, this week, I plan to write my own "VCR type" controler panel.
I'm sure there are some out there,
but I havent found any that are as flexible/reusable as I would like.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.