About Deep Copy and Serialization - java

I have read a lot about deep copying and serialization in Java List, Map, etc, but I did not find good answers to a some questions. I care for time and performance, so I am looking for a compromise. I list my questions below so that you can address the ones you have answers to.
What is better in terms of performance, deepcopy by looping over a list or using serialization? I have read a post (sorry I lost the link) that says looping is 4 times better than java serialization. Does this mean that using a 3rd party like Kryo can be better than looping?
I am not sure how serialization works in different 3rd party libraries, but what happens if I have many subclass levels, would serialization be better than looping?
Is there any library in Java that copies raw memory? For instance, a library that uses memcopy()-like functions in C. That would be much faster since there is no need to care about classes logic (of course it needs some handling for non-contiguous memory data). I am aware that Java is object oriented :), but this wont violate rules I think.
If I want to implement deepCopy(List<?>) and deepCopy(Map<?,?>) in Java, can I put them in a MyTools class that I have; or there is some neater way to do it in Java?

Looping will always be faster than serialization unless you are only serializing primitives.
In third party libraries, they handle subclass and all kinds in general. They do this by using sun.misc.Unsafe.class
Yes, actually you can copy memory using Unsafe class. Check Unsafe class implementation.
You can have MyTools or let's say Utils class do to that for you.
According to your needs, there is not way of deep copying for list, maps and other generics. For this reason, you may end up using a good serialization library like Kryo.

Related

Is it good practice in Java to implement the clone method using serialization?

I saw several tutorials online where serialization and subsequent deserialization is used to implement deep cloning in Java.
My feeling is, that this is a solution that is fast to implement and therefore widespread but might have caveats that I currently don't see.
Is that way of implementing clone() good style? Isn't it slow? Should deep cloning really be done that way? What better ways exist?
Is it good practice in Java to implement the clone method using
serialization?
If you use serialization to clone an object you have to necessarily unserialize the serialized object to create the cloned object. It makes two operations where the second seems be an overhead as it should not be performed if you implement the cloning operation at the hand or with a mapper API (ex: SpringBean, Common apache, ModelMapper, Dozzer...).
So it has without no doubt an impact on the performance. If you do this processing very occasionally, I don't think that it should be a problem (even if it seems to be a useless overhead and you have alternative ways) but if you use it often I think that it may have a cost.
Besides, why implementing Clonable to clone an object by using serialization instead of forgetting Cloneable that is a clumsy API and using directly the deserialization mechanism ?

Difference on concatenation for immutable list between java Guava and Scala

I am trying spark with java and I get stuck by the immutable collections in java.
As I understand in Scala when two immutable lists are combined, no deep copy happens. However the available immutable list in java, like guava, does the defensive copy. (correct me if I am wrong)
So simply my questions are:
Is there some Java immutable list which has the same behavior as
scala immutable list ?
If the answer of the first question is NO, what's the
general(standard) way to use scala immutable collection in java code
?
Thanks very much.
Scala Lists is a so-called persistent collection with the persistent referring to the fact, that no defensive copying happens. If you google for Java persistent collection you should find several links to get you started. In addition to that, there are a number of libraries that aim to bring the essence of functional programming to Java. As persistent collections are inherently functional, those frameworks often include their own collections implementation. The two libraries I've worked with and can recommend are Javaslang and Functional Java but there are more out there than these two (e.g. pccollections, jOOλ, …).
As for using Scala collections from Java, I've always found this to be somewhat awkward. I would suggest to write a Scala class/object, that gives you easy (as from-Java-easy) access to the scala.collection.JavaConverters, so that you can expose your scala.collection.immutable.List as a java.util.List and work with that interface.

Scala types/collections when interfacing with Java and vice versa

When interfacing with a scala library in java, or a java library in scala, are there certain types or collections that don't map efficiently such that you have to perform "expensive" operations to perform a conversion?
e.g. memory wise you might have to hold 2 copies of a collection?
Well, that depends on how you want to handle it - if you use the Java class's API from Scala or the other way around, there is no run-time overhead, but you'll pay a convenience cost.
When you use JavaConversions to access Java collections through a Scala API or the other way around, you pay a small cost for the conversion (a Wrapper object is created), but the underlying collection is never copied.
You can inspect the code for all this here, as usual for the Scala standard library it's all very readable and easy to understand.

Google Collections equivalent to Apache Commons Collections ArrayUtils.toObject and ArrayUtils.toPrimitive

Since everyone praises Google Collections (e.g. in here)
How come I can't find the equivalent of ArrayUtils.toObject() and ArrayUtils.toPrimitive()? is it that unusable? did I miss it?
To be honest I'm not sure if either of those methods should even qualify as a collection-related operation and as such I'd wonder why they're even there in the first place.
To clarify a bit, collections are generically a group of objects with some semantic data binding them together while arrays are just a predetermined set of something. This semantic data may be information about accepting or rejecting nulls, duplicates, objects of wrong types or with unacceptable field values etc.
Most -if not all- collections do use arrays internally, however array itself isn't a collection. To qualify as a collection it needs some relevant magic such as removing and adding objects to arbitrary positions and arrays can't do that. I very much doubt you'll ever see any kind of array support in Google Collections since arrays are not collections.
However since Google Collections is going to be part of Google's Guava libraries which is a general purpose utility class library/framework of sorts, you may find what you want from com.google.common.primitives package, for example Booleans#asList(boolean... backingArray) and Booleans#toArray(Collection<Boolean> collection).
If you absolutely feel they should include equal methods to Apache Commons Collection's .toObject() and .toPrimitive() in there, you can always submit a feature request as new issue.

Java-like Collections in PHP

I'm learning PHP5 (last time I checked PHP was in PHP4 days) and I'm glad to see that PHP5 OO is more Java-alike than the PHP4 one but there's still an issue that makes me feel quite unconfortable because of my Java background : ARRAYS.
I'm reading "Proffesional PHP6" (Wrox) and It shows its own Collection implementation.
I've found other clases like the one in http://aheimlich.dreamhosters.com/generic-collections/Collection.phps based on SPL.
I've also found that there's some kind of Collection in SPL (ArrayObject)
However, I'm surprised because I don't really see people using Collections in PHP, they seem to prefer arrays.
So, isn't it a good idea using Collections in PHP just like people use ArrayList instead of basic arrays in Java? After all, php arrays aren't really like java arrays.
Collections in Java make a lot of sense since it's a strongly typed language. It makes sense to have a collection of say "Cars" and another of "Motorbikes".
However, in PHP, due to the dynamically typed nature, it is quite common to sacrifice the formality of Collections. Arrays are sufficient to be used as generic containers of various object types (Cars, Motorbikes, etc.). Also, the added benefit comes from the fact that arrays can be mutated very easily (which sometimes can be a big disadvantage when proper error checking is absent).
I come from a Java background, and I've found that using a Collections design pattern in PHP does not buy much in the way of advantages (no multi-threading, no optimization of memory allocation, no iterators, etc.).
If you're looking for any of those advantages, its probably better to construct a wrapper class around the array, implementing each feature (iterators, etc.) a la carte.
I am very pro collection objects in PHP, they can be used to add type safety, impliment easy to use search, sort and manipulation functionality, and represent the correct OO approach rather then using arrays and the multitude of useful but procedual functions that operate on them in differing patterns all over the source.
We have various collections that we use for various purposes all neatly inherited promoting type safety, consistent coding standards and a high level of code reuse.
But ultimatley, they are all array's internally!
I suppose really it comes down to choice, but in my object oriented world I like to keep easily repeatable segments of code such as sort and search algorithms in base classes, and I find the object notation more self documenting.
PHP arrays are associative... They're far more powerful than Java's arrays, and include much of the functionality of List<> and Map<>.
What do you mean by "good idea"? They're different tools, using one language in the way you used another usually results in frustration.
I, too, was somewhat dismayed to find no Collection type classes in PHP. Arrays have a couple of real disadvantages in my experience.
First, the number of functions available to manipulate them is somewhat limited. For example, I need to be able to arbitrarily insert and remove items to/from a Collection at a given index position. Doing that with the built-in language functions for arrays in PHP is painful at best.
Second, as a sort of offshoot of the first point, writing clean, readable code that manipulates arrays at any level of complexity beyond simple push/pop and iterator stuff is difficult at best. I often find that I have to use one array to index and keep track of another array in data-intensive apps I create.
I prefer working in a framework (my personal choice is NOLOH). There, I have a real Collection class called ArrayList that has functions such as Add, Insert, RemoveAt, RemoveRange and Toggle. I imagine other PHP frameworks address this issue as well.
A nice implementation of collection in php is provided by Varien Lib, this library is part of Magento code with OSL license. ( more info about Magento license and code reuse here.
Cannot find any source code for the library so the best way is to download magento and then look in /lib/Varien/
Yii has implementation of full java like collections stack
http://www.yiiframework.com/doc/api/1.1/CList
I sometimes use this really simple implementation to give me a rough and ready collection.
Normally the main requirement of a collection is enforcing a group of one type of object, you just have to setup a basic class with a constructor to implement it.
class SomeObjectCollection {
/**
* #var SomeObject[]
*/
private $collection = array();
/**
* #param SomeObject $object1
* #param SomeObject $_ [optional]
*/
function __construct(SomeObject $object1 = null, SomeObject $_ = null)
{
foreach (func_get_args() as $index => $arg) {
if(! $arg instanceof SomeObject) throw new \RuntimeException('All arguments must be of type SomeObject');
$this->collection[] = $arg;
}
}
/**
* #return SomeObject[]
*/
public function getAll()
{
return $this->collection;
}
}

Categories