Why is Java’s AbstractList’s removeRange() method protected?

Yes, because that’s not how you remove a range from outside code. Instead, do this: list.subList(start, end).clear(); This actually calls removeRange behind the scenes.† The OP asks why removeRange is not part of the List public API. The reason is described in Item 40 of Effective Java 2nd ed, and I quote it here: There … Read more

What is the difference between Collections.emptyList() and Collections.EMPTY_LIST

Collections.EMPTY_LIST returns an old-style List Collections.emptyList() uses type-inference and therefore returns List<T> Collections.emptyList() was added in Java 1.5 and it is probably always preferable. This way, you don’t need to unnecessarily cast around within your code. Collections.emptyList() intrinsically does the cast for you. @SuppressWarnings(“unchecked”) public static final <T> List<T> emptyList() { return (List<T>) EMPTY_LIST; }

JUnit 4 compare Sets

You can assert that the two Sets are equal to one another, which invokes the Set equals() method. public class SimpleTest { private Set<String> setA; private Set<String> setB; @Before public void setUp() { setA = new HashSet<String>(); setA.add(“Testing…”); setB = new HashSet<String>(); setB.add(“Testing…”); } @Test public void testEqualSets() { assertEquals( setA, setB ); } } … Read more

C# Sortable collection which allows duplicate keys

Use your own IComparer! Like already stated in some other answers, you should use your own comparer class. For this sake I use a generic IComparer class, that works with anything that implements IComparable: /// <summary> /// Comparer for comparing two keys, handling equality as beeing greater /// Use this Comparer e.g. with SortedLists or … Read more

Converting List to List

Using Google Collections from Guava-Project, you could use the transform method in the Lists class import com.google.common.collect.Lists; import com.google.common.base.Functions List<Integer> integers = Arrays.asList(1, 2, 3, 4); List<String> strings = Lists.transform(integers, Functions.toStringFunction()); The List returned by transform is a view on the backing list – the transformation will be applied on each access to the transformed … Read more

UnmodifiableMap (Java Collections) vs ImmutableMap (Google) [duplicate]

An unmodifiable map may still change. It is only a view on a modifiable map, and changes in the backing map will be visible through the unmodifiable map. The unmodifiable map only prevents modifications for those who only have the reference to the unmodifiable view: Map<String, String> realMap = new HashMap<String, String>(); realMap.put(“A”, “B”); Map<String, … Read more

Combine multiple Collections into a single logical Collection?

With Guava, you can use Iterables.concat(Iterable<T> …), it creates a live view of all the iterables, concatenated into one (if you change the iterables, the concatenated version also changes). Then wrap the concatenated iterable with Iterables.unmodifiableIterable(Iterable<T>) (I hadn’t seen the read-only requirement earlier). From the Iterables.concat( .. ) JavaDocs: Combines multiple iterables into a single … Read more

shortcut for creating a Map from a List in groovy?

I’ve recently came across the need to do exactly that: converting a list into a map. This question was posted before Groovy version 1.7.9 came out, so the method collectEntries didn’t exist yet. It works exactly as the collectMap method that was proposed: Map rowToMap(row) { row.columns.collectEntries{[it.name, it.val]} } If for some reason you are … Read more

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