DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Writing DTOs With Java8, Lombok, and Java14+
  • Redefining Java Object Equality
  • Addressing Memory Issues and Optimizing Code for Efficiency: Glide Case
  • Singleton: 6 Ways To Write and Use in Java Programming

Trending

  • Avoiding AI Debt as a Team
  • Google Cloud KMS vs. AWS KMS: Java Encryption Guide
  • The Three Tiers of Agentic Incident Response: When to Trust AI Autonomy
  • New York City Taxi Trips: Putting Neo4j and DuckDB to Work

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. Deep Clone Collection Objects in Java [Snippets]

Deep Clone Collection Objects in Java [Snippets]

Check out this post on how to deep clone an object in Java 8 using the Object.clone method and new Java streams.

By 
Yogen Rai
·
Jul. 18, 18 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
35.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Deep cloning of an object has always been something that Java developers work on constantly. There are a lot of articles that talk about the different ways to clone objects deeply and obviously. The preferred method is to use  Copy Constructors since it overcomes the design issues of  Object.clone() and provides better control over object construction.

Clone an Object

For example, if I want to have a deep copy of the Dog object, then I would use constructor as:

class Dog {
    private String name;
    private int age;

    public Dog(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

    //copy constructor to create the copy of the Dog object
    public Dog(Dog dog) {
        this.name = dog.name;
        this.age = dog.age;
    }
    // getters and setters
}


Some of the notable advantages of using them over Objects.clone() are:

  • This doesn't force us to implement any interface or throw an exception, but we can surely do it if it is required.
  • This allows us to have complete control over object creation; we can write our initialization logic in it.

Clone a Collection

With new feature Streams introduced in Java 8, it has been the easiest ever to clone the collections, because when a stream is created, the instance will not modify its source. This allows the creation of multiple instances from a single source.     

For example, if the  List of Dogscan be cloned as below:

Dog dog1 = new Dog("Puppy", 4);
Dog dog2 = new Dog("Tom", 5);
Dog dog3 = new Dog("Hen", 3);
Dog dog4 = new Dog("Jen", 7);
List<Dog> dogs = new ArrayList<>();
dogs.add(dog1);
dogs.add(dog2);
dogs.add(dog3);
dogs.add(dog4);

//clone with java 8
List<Dog> clonedList = dogs.stream().map(Dog::new).collect(Collectors.toList());


Now, any modification you make to the original collection doesn't affect the cloned collection and vice-versa. 

//clone with java 8
List<Dog> clonedList = dogs.stream().map(Dog::new).collect(Collectors.toList());

assertThat(dogs, is(clonedList));

// modify original list
dogs.remove(0);
// make sure cloned list remains unaffected
assertEquals(3, dogs.size());
assertEquals(4, clonedList.size());

// modify cloned list
clonedList.add(new Dog("New", 3));
// make sure original list remains unaffected
assertEquals(3, dogs.size());
assertEquals(5, clonedList.size());

// modify element in dogs
dog2.setName("Very New Name");

assertEquals("Very New Name", dogs.get(0).getName());
assertEquals("Tom", clonedList.get(1).getName());

 

The source code for the example presented above is available on GitHub.

Object (computer science) Clone (Java method) Java (programming language)

Published at DZone with permission of Yogen Rai. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Writing DTOs With Java8, Lombok, and Java14+
  • Redefining Java Object Equality
  • Addressing Memory Issues and Optimizing Code for Efficiency: Glide Case
  • Singleton: 6 Ways To Write and Use in Java Programming

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook