readonly keyword does not make a List ReadOnly?

The modifier readonly means that the value cannot be assigned except in the declaration or constructor. It does not mean that the assigned object becomes immutable. If you want your object to be immutable, you must use a type that is immutable. The type ReadOnlyCollection<T> that you mentioned is an example of a immutable collection. … Read more

Guava: Set + Function = Map?

Creating a Map from a Set and a Function Here are two classes that should each do the job. The first just shows a map view of the set, while the second can write values back to the set through a special interface. Call Syntax: Map<K,V> immutable = new SetBackedMap<K,V>(Set<K> keys, Function<K,V> func); Map<K,V> mutable … Read more

Java: Why are wrapper classes needed?

Because Java collections can only store Object References (so you need to box primitives to store them in collections). Read this short article on Autoboxing for more info. If you want the nitty gritty details, it pretty much boils down to the following: Local Primitives are stored on the Stack. Collections store their values via … Read more

Shallow copy of a hashset

Use the constructor: HashSet<type> set2 = new HashSet<type>(set1); Personally I wish LINQ to Objects had a ToHashSet extension method as it does for List and Dictionary. It’s easy to create your own of course: public static HashSet<T> ToHashSet<T>(this IEnumerable<T> source) { if (source == null) { throw new ArgumentNullException(“source”); } return new HashSet<T>(source); } (With … Read more

How to convert String into Hashmap in java

This is one solution. If you want to make it more generic, you can use the StringUtils library. String value = “{first_name = naresh,last_name = kumar,gender = male}”; value = value.substring(1, value.length()-1); //remove curly brackets String[] keyValuePairs = value.split(“,”); //split the string to creat key-value pairs Map<String,String> map = new HashMap<>(); for(String pair : keyValuePairs) … Read more

Update Counter collection in python with string, not letter

You can update it with a dictionary, since add another string is same as update the key with count +1: from collections import Counter c = Counter([‘black’,’blue’]) c.update({“red”: 1}) c # Counter({‘black’: 1, ‘blue’: 1, ‘red’: 1}) If the key already exists, the count will increase by one: c.update({“red”: 1}) c # Counter({‘black’: 1, ‘blue’: … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)