Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail From: Jan Burse Newsgroups: comp.lang.java.programmer Subject: Immutable Datastructures with good Sharing Date: Sat, 05 Nov 2011 04:03:55 +0100 Organization: albasani.net Lines: 46 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.albasani.net rk2h5eqMDVvnJltUPJOscRlp9sFY+Whv7R+3BZNYBUYq1L1+0Nb7fbV2YDK1FCUQEziRaNzIyysCpjwnzRKQeA== NNTP-Posting-Date: Sat, 5 Nov 2011 03:03:59 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="RNISN2rCNIdykSKYlFxpj53iYt0wpOsXuMJGcV+VSry18WZjdtot7T9U3meuwR0+WOdBTgyjNdDpbHqhPehHyXchko7+y9/ASskpoWZ+5hI/VFhCR6GmEtUy5A1RPa4R"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20110928 Firefox/7.0.1 SeaMonkey/2.4.1 Cancel-Lock: sha1:VmcCqQoLC4dxBkXX0mE35TIfVxU= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:9560 Dear All, Value objects are some well known immutable datastructures, when an operation is applied, typically simply a new object is created. So for example doing: 3 = 1 + 2 With java.lang.Integer objects would involve creating a new Integer with value left.intValue() + right.intValue(). But instead of call the constructor, one can also call Integer.valueOf(), and there you have some sharing. Namely the class Integer caches the value objects for small values (at least the last time I looked into the Oracle source, it was there...). More elaborate sharing is seen in the String class by the substring() operation. This operation does reuse the original character array and only encapsulates the offset and the length in a new object. I wouldnt recomende that to get one character from a 1 MB long string, since it will prevent the original character array from GC. But in some situation this is quite handy. I am now looking for immutable datastructures with a similar sharing. In the String class example, the operation was substring() that produced a new immutable object. I am looking for: Some stack class, where: (Easy) pop() creates a new immutable stack push() creates a new immutable stack With good sharing. Some queue class, where: (Hard?) enqueue() creates a new immutable queue dequeue() creates a new immutable queue With good sharing. What is your favorite datastructure? Bye