Adding elements to a set on creation

Since Java 7, to instantiate a single-element, immutable Set, you can use: Collections.singleton(thing); Returns an immutable set containing only the specified object. The returned set is serializable. — Javadoc reference: Collections.singleton(T) In Java 8 you can instantiate a Set containing any number of your objects with the following, which is an adaptation of this answer: … Read more

Which implementation of Map should I use if my map needs to be small more than fast?

There is no standard small implementation of Map in Java. HashMap is one of the best and most flexible Map implementations around, and is hard to beat. However, in the very small requirement area — where heap usage and speed of construction is paramount — it is possible to do better. I have implemented SmallCollections … Read more

Why does EnumSet have many overloaded “of” methods?

Varargs methods create an array. public static void foo(Object… args) { System.out.println(args.length); } This works, because of the implicit array creation. EnumSet is a class designed to be very, very fast, so by creating all the extra overloads they can skip the array creation step in the first few cases. This is especially true since … Read more

How to store a collection of custom objects to an user.config file?

The way to add custom config (if you require more than just simple types) is to use a ConfigurationSection, within that for the schema you defined you need a ConfigurationElementCollection (set as default collection with no name), which contains a ConfigurationElement, as follows: public class UserElement : ConfigurationElement { [ConfigurationProperty( “firstName”, IsRequired = true )] … Read more

Testing if a collection contains objects based on a particular property

You could use LINQ: Assert.That(people.Any(p => p.Name == “joe”)); or, if you want to be explicit about there being exactly one person with each name: Assert.That(people.Count(p => p.Name == “joe”), Is.EqualTo(1)); If you want a better error message than “Assertion failed, expected true, was false”, you could create your own assert method. For several collection-related … Read more

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