Quantcast
Channel: Sleepless Dev
Viewing all articles
Browse latest Browse all 217

Boon Remix: How to Clone Collection in Java - Deep copy of ArrayList and HashSet with Boon

$
0
0

How to Clone Collection in Java - Deep copy of ArrayList and HashSet with Boon

Please visit and read this article. This is a remix of this: How to Clone Collection in Java - Deep copy of ArrayList and HashSet.
Boon has many utility methods to convert objects into maps, lists, JSON, etc. It also has many collection utility methods.
Programmer, none that I have ever met, have mistakenly used copy constructors by by various collection classes, as a way to clone Collection e.g. List, Set, ArrayList, HashSet or any other implementation. Or they write a lot of boiler plate code and spend a lot of time doing things to copy lists.
The key point is to remember is that the built-in collection lib does not do a deep copy.
If you have code using the original collection modifies an employee, that change could be reflected in the cloned collection. Similarly if an employee is modified in cloned collection, it will also appeared as modified in original collection.
This is less than desirable. A solution to avoid this problem is deep cloning of collection, which means recursively cloning object, until you reached to primitive or Immutable. But that takes a lot of work. Boon has an easier solution. Boon has a copy function that does a deep copy for you. In fact Boon has hundreds of such utility functions. Don't waste your time with cloning, use Boon and develop Java apps with a smile on your face.

deep copy using Set

Set<Employee>original,copy;

original=set(
newEmployee("Joe","Manager"),
newEmployee("Tim","Developer"),
newEmployee("Frank","Developer"));

copy=deepCopy(original);

setCollectionProperty(original,"designation","staff");


puts("Original Set after modification",original);

puts("Copy of Set after modification",copy);


Output
Original Set after modification [Joe:staff, Tim:staff, Frank:staff] 
Copy of Set after modification [Joe:Manager, Tim:Developer, Frank:Developer]

The above code is functionally equivalent to the article How to Clone Collection in Java - Deep copy of ArrayList and HashSet. But it is smaller and we did not have to implement the clone method. The deepCopy will even copy other properties which are instances of other domain objects even if those domain objects are in lists or arrays.

deep copy using List

List<Employee>original,copy;

original=list(
newEmployee("Joe","Manager"),
newEmployee("Tim","Developer"),
newEmployee("Frank","Developer"));

copy=deepCopy(original);

setCollectionProperty(original,"designation","staff");

OriginalListaftermodification[Joe:staff,Tim:staff,Frank:staff]
CopyofListaftermodification[Joe:Manager,Tim:Developer,Frank:Developer]
This just scratches the surface of what Boon can do.
Stop by the Boon wiki. Book Wiki
/*
* Copyright 2013-2014 Richard M. Hightower
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* __________ _____ __ .__
* \______ \ ____ ____ ____ /\ / \ _____ | | _|__| ____ ____
* | | _// _ \ / _ \ / \ \/ / \ / \\__ \ | |/ / |/ \ / ___\
* | | ( <_> | <_> ) | \ /\ / Y \/ __ \| <| | | \/ /_/ >
* |______ /\____/ \____/|___| / \/ \____|__ (____ /__|_ \__|___| /\___ /
* \/ \/ \/ \/ \/ \//_____/
* ____. ___________ _____ ______________.___.
* | |____ ___ _______ \_ _____/ / _ \ / _____/\__ | |
* | \__ \\ \/ /\__ \ | __)_ / /_\ \ \_____ \ / | |
* /\__| |/ __ \\ / / __ \_ | \/ | \/ \ \____ |
* \________(____ /\_/ (____ / /_______ /\____|__ /_______ / / ______|
* \/ \/ \/ \/ \/ \/
*/

packagecom.examples;

importorg.junit.Test;

importjava.util.List;
importjava.util.Set;

importstaticorg.boon.Boon.puts;
importstaticorg.boon.Ok.okOrDie;
importstaticorg.boon.Sets.deepCopy;
importstaticorg.boon.Sets.set;
importstaticorg.boon.Lists.list;
importstaticorg.boon.Lists.deepCopy;
importstaticorg.boon.Str.add;
importstaticorg.boon.core.reflection.BeanUtils.setCollectionProperty;
importstaticorg.boon.core.reflection.BeanUtils.copy;

publicclassDeepCopyRemix{



publicstaticclassEmployee{
privateStringname;
privateStringdesignation;

publicEmployee(Stringname,Stringdesignation){
this.name=name;
this.designation=designation;
}

publicStringgetName(){
returnname;
}

publicvoidsetName(Stringname){
this.name=name;
}

publicStringgetDesignation(){
returndesignation;
}

publicvoidsetDesignation(Stringdesignation){
this.designation=designation;
}

@Override
publicbooleanequals(Objecto){
if(this==o)returntrue;
if(o==null||getClass()!=o.getClass())returnfalse;

Employeeemployee=(Employee)o;

if(designation!=null?!designation.equals(employee.designation):employee.designation!=null)
returnfalse;
if(name!=null?!name.equals(employee.name):employee.name!=null)returnfalse;

returntrue;
}

@Override
publicinthashCode(){
intresult=name!=null?name.hashCode():0;
result=31*result+(designation!=null?designation.hashCode():0);
returnresult;
}

@Override
publicStringtoString(){
returnadd(name,":",designation);
}
}



publicstaticvoidmain2(String...args){

Set<Employee>original,copy;

original=set(
newEmployee("Joe","Manager"),
newEmployee("Tim","Developer"),
newEmployee("Frank","Developer"));

copy=deepCopy(original);

setCollectionProperty(original,"designation","staff");


puts("Original Set after modification",original);

puts("Copy of Set after modification",copy);




Employeeemployee=newEmployee("Joe","Manager");
EmployeecopyEmployee=copy(employee);


okOrDie("not same object",employee!=copyEmployee);

okOrDie("not same object",copy.iterator().next()!=original.iterator().next());
}


publicstaticvoidmain(String...args){

List<Employee>original,copy;

original=list(
newEmployee("Joe","Manager"),
newEmployee("Tim","Developer"),
newEmployee("Frank","Developer"));

copy=deepCopy(original);

setCollectionProperty(original,"designation","staff");

original.iterator().next().setDesignation("staff");

puts("Original List after modification",original);

puts("Copy of List after modification",copy);

okOrDie("not same object",copy.iterator().next()!=original.iterator().next());


}


@Test
publicvoidtest(){
DeepCopyRemix.main();

DeepCopyRemix.main2();
}


}

Boon. Code Java with pleasure.

Thoughts

Thoughts? Write me at richard high tower AT g mail dot c-o-m (Rick Hightower).

Further Reading:

If you are new to boon start here:

Why Boon?

Easily read in files into lines or a giant string with one method call. Works with files, URLs, class-path, etc. Boon IO support will surprise you how easy it is. Boon has Slice notation for dealing with Strings, Lists, primitive arrays, Tree Maps, etc. If you are from Groovy land, Ruby land, Python land, or whatever land, and you have to use Java then Boon might give you some relief from API bloat. If you are like me, and you like to use Java, then Boon is for you too. Boon lets Java be Java, but adds the missing productive APIs from Python, Ruby, and Groovy. Boon may not be Ruby or Groovy, but its a real Boon to Java development.

Core Boon Philosophy

Core Boon will never have any dependencies. It will always be able to run as a single jar. This is not just NIH, but it is partly. My view of what Java needs is more inline with what Python, Ruby and Groovy provide. Boon is an addition on top of the JVM to make up the difference between the harder to use APIs that come with Java and the types of utilities that are built into Ruby, Python, PHP, Groovy etc. Boon is a Java centric view of those libs. The vision of Boon and the current implementation is really far apart.

Contact Info

Viewing all articles
Browse latest Browse all 217

Trending Articles