immutability

Should custom key objects be immutable ?If yes , then why?

随声附和 提交于 2019-12-13 06:26:16
问题 Okay I want to have custom user defined objects as keys in my HashMap instead of say String . Should the candidate objects be immutable ? I read somewhere that the best practice is to make them immutable but I can not figure out the reason myself . 回答1: If you have a mutable key in a HashMap, then it will end up in the wrong bucket, which totally breaks the Map. insert key, hashCode() is called, bucket assigned change key, hashCode changes, no longer matches the bucket look up by (new) key,

Thread safety for classes with non-final members with no mutator methods

会有一股神秘感。 提交于 2019-12-13 05:19:50
问题 For the following class, what are the implications of making mNumSides final? Is "thread-safety" affected? class Shape { private int mNumSides; public Shape(int numSides) { mNumSides = numSides; } public int getNumSides() { return mNumSides; } } 回答1: Absolutely. final keyword guarantees that all the threads see the same value of mNumSides at all the times. More information on final and its impact on the Memory Model here. Without using final the object might be inconsistently published to

java String's immutability

可紊 提交于 2019-12-13 05:18:57
问题 If I run, String s1="abc"; then are there any differences between: String s2=new String(s1); and String s2="abc"; Here's what I'm confused about: The Head First Java says:"If there's already a String in the String pool with the same value, the JVM doesn't create a duplicate, it simply refers your reference variable to the existing entry. " Which at my point of view is that the s1 has already created "abc",s2 just refers to it. Am I right?? 回答1: When you write String s2="abc"; and "abc" is

Why does Swift's reduce function throw an error of 'Type of expression ambigious without more context' when all types are properly defined?

感情迁移 提交于 2019-12-13 05:13:01
问题 var nums = [1,2,3] let emptyArray : [Int] = [] let sum1 = nums.reduce(emptyArray){ $0.append($1)} let sum2 = nums.reduce(emptyArray){ total, element in total.append(element) } let sum3 = nums.reduce(emptyArray){ total, element in return total.append(element) } For all three approaches I'm getting the following error: Type of expression ambiguous without more context But looking at documentation and the method signature of reduce: func reduce<Result>(_ initialResult: Result, _

Multiple references in separate lists; Python

ぐ巨炮叔叔 提交于 2019-12-13 05:07:58
问题 I'm trying to basically create references to plot multiple relationships and store them in a list or possibly a dictionary. Basically: variable1 = 10 //in this case, 'ref' denotes that the variable should be a reference) listA = [variable1(ref), variable2, variable3] listB = [variable1(ref), variable4, variable5] for i in listA: i = i + 10 for i in listB: i = i + 10 print listA[0] //Should print 30 print listB[0] //Should print 30 How can I split two references to the same variable into two

Overloading a method in an immutable class

走远了吗. 提交于 2019-12-13 04:39:08
问题 Say that I have an immutable Point class with x and y parameters, and an add method defined like this: class Point: Point add(int x, int y): return new Point(this.x + x, this.y + y); Since it's immutable, it returns a new Point. This is all well and good until we have a class that extends Point and redefines add . class ColoredPoint extends Point: ColoredPoint add(int x, int y): return new ColoredPoint(this.x + x, this.y + y, this.width, this.height) We have to write the new definition

Immutable Java class with non-final member

家住魔仙堡 提交于 2019-12-13 03:55:27
问题 I still have some problems grasping the idea of immutability in Java. I understand that it differs from the const -ness in C++ and that a final class that only has final members of classes that are immutable themselves is immutable. E.g. the following class is immutable: public final class A { final String x; final int y; public A(String x, String y) { this.x = x; this.y = y; } } Is there some formal definition besides the guidelines presented here and similar stuff somewhere else? Consider

Making a list subclass hashable [closed]

半城伤御伤魂 提交于 2019-12-13 02:49:32
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . I want to derive a class from list , add a few instance attributes to it, and make it hashable. What is a good (fast and neat) way to

Is it possible to access a reference of a struct from a List<T> to make changes?

混江龙づ霸主 提交于 2019-12-13 02:36:09
问题 I have a struct which I put in a List<T> , I want to edit some value in that struct at a specific position. Is this at all possible without making a copy of the struct, editing the copy, and replacing the entry in the List? 回答1: No, to be able to do it you need reference to element of inner array which is not provided by List / IList . You can do that with unsafe code and arrays if you have to. 回答2: From J.Richter's "CLR via C#", 3rd edition: Value types should be immutable: that is, they

Immutable Lists in Scala

老子叫甜甜 提交于 2019-12-13 01:27:58
问题 I am just trying to figure out how immutable things like a List are working, and how I can add things to it? I am very sorry for asking such dumb questions, but why is here my list always empty when printing it out? var end = false val list = List() while (!end) { val input = scala.io.StdIn.readLine("input:") if (input == "stop" ) end = true else input :: list } println(list) } Sorry for my inconvenience and this rather stupid question! 回答1: I am just trying to figure out how immutable things