immutability

Immutable Object with ArrayList member variable - why can this variable be changed?

与世无争的帅哥 提交于 2019-12-02 15:56:47
I have got one class with various member variables. There is a constructor and there are getter-methods, but no setter-methods. In fact, this object should be immutable. public class Example { private ArrayList<String> list; } Now I noticed the following: when I get the variable list with a getter-method, I can add new values and so on - I can change the ArrayList . When I call the next time get() for this variable, the changed ArrayList is returned. How can this be? I didn't set it again, I just worked on it! With a String this behaviour isn't possible. So what is the difference here? Just

Persistent data structures in Java

痞子三分冷 提交于 2019-12-02 14:19:27
Does anyone know a library or some at least some research on creating and using persistent data structures in Java? I don't refer to persistence as long term storage but persistence in terms of immutability (see Wikipedia entry ). I'm currently exploring different ways to model an api for persistent structures. Using builders seems to be a interesting solution: // create persistent instance Person p = Builder.create(Person.class) .withName("Joe") .withAddress(Builder.create(Address.class) .withCity("paris") .build()) .build(); // change persistent instance, i.e. create a new one Person p2 =

How to make a mutable object to immutable? (not at creation) [duplicate]

橙三吉。 提交于 2019-12-02 14:14:00
问题 This question already has answers here : Make immutable Java object (15 answers) Closed last year . I have a use case where I need to create a (mutable)object first, and at certain scenario I need to make it immutable(I don't wanna make it immutable upon creation). Is there a good way to achieve it? The requirement is to change it from mutable to immutable at some time, using final will not work. 回答1: An object cannot be mutable and immutable at the same time. What you can do is you can have

Remove a property in an object immutably

江枫思渺然 提交于 2019-12-02 14:02:25
I am using Redux. In my reducer I'm trying to remove a property from an object like this: const state = { a: '1', b: '2', c: { x: '42', y: '43' }, } And I want to have something like this without having to mutate the original state: const newState = { a: '1', b: '2', c: { x: '42', }, } I tried: let newState = Object.assign({}, state); delete newState.c.y but for some reasons, it deletes the property from both states. Could help me to do that? How about using destructuring assignment syntax? const original = { foo: 'bar', stack: 'overflow', }; // If the name of the property to remove is

Closing over immutable variable and accumulate values across multiple iterations as a lambda expression - Java 8

陌路散爱 提交于 2019-12-02 11:47:22
问题 WebTarget in Jersey client is implemented as a immutable object, and any operations to change the state returns a new WebTarget. To add query params to it, which is coming in as a Map<> the following code was written. public WebTarget webTarget(String path, Map<String, String> queryMap) { WebTarget webTarget = client.target(this.address.getUrl()).path(path); if (queryMap != null) queryMap.entrySet().forEach(e -> webTarget.queryParam(e.getKey(), e.getValue())); return webTarget; } The problems

Does immutability guarantee thread safety?

醉酒当歌 提交于 2019-12-02 09:57:17
问题 Well, consider the immutable class Immutable as given below: public final class Immutable { final int x; final int y; public Immutable(int x,int y) { this.x = x; this.y = y; } //Setters public int getX() { return this.x; } public int getY() { return this.y; } } Now I am creating an object of Immutable in a class Sharable whose object is going to be shared by multiple threads: public class Sharable { private static Immutable obj; public static Immutable getImmutableObject() { if (obj == null)

Usefulness of immutable objects when the state of a program constantly changes

旧时模样 提交于 2019-12-02 07:32:22
问题 I know that immutable objects always have the same state, the state in which they are actually created. Their invariants are establised by the constructor and since their state does not change after construction , those invariants always hold good and this is why they are safe to publish in a multi threaded environment. This is all fine but since we live in a dynamic world , where the state of program changes continuously , what benefits do such objects give us if we construct the state of

Strings Immutability

此生再无相见时 提交于 2019-12-02 05:12:02
问题 I was told that strings in java can not be changed.What about the following code? name="name"; name=name.replace('a', 'i'); Does not it changes name string? Also, where is the implementation of the replace(); compareTo(); equals(); provided? I am just using these functions here, but where actually are they implemented? 回答1: String.replace() returns a new String. "name" is a reference to a String object, so it can be reassigned to point to name.replace(), but it will be pointing to a new

Usefulness of immutable objects when the state of a program constantly changes

瘦欲@ 提交于 2019-12-02 04:34:06
I know that immutable objects always have the same state, the state in which they are actually created. Their invariants are establised by the constructor and since their state does not change after construction , those invariants always hold good and this is why they are safe to publish in a multi threaded environment. This is all fine but since we live in a dynamic world , where the state of program changes continuously , what benefits do such objects give us if we construct the state of our program through immutable objects? "what benefits do such objects give us" you already answered that.

Are tuples really immutable in Python? [duplicate]

荒凉一梦 提交于 2019-12-02 04:20:29
问题 This question already has answers here : Append to a list defined in a tuple - is it a bug? [duplicate] (4 answers) Closed 4 years ago . One question that I faced today, which actually tested the immutability of the tuples in Python: Interviewer : Are tuples immutable in Python? Me : Yes Interviewer : So what does print(t1) here print? t1 = (4, 5) t1 = t1 + (91, 10) print(t1) Me : (4, 5, 91, 10) Interviewer : How does immutability of tuple then define this behavior? Me : It's got nothing to