HashSet vs TreeSet vs LinkedHashSet on basis of adding duplicate value

TreeSet, LinkedHashSet and HashSet in Java are three Set implementation in collection framework and like many others they are also used to store objects. Main feature of TreeSet is sorting, LinkedHashSet is insertion order and HashSet is just general purpose collection for storing object. HashSet is implemented using HashMap in Java while TreeSet is implemented … Read more

Combine stream of Collections into one Collection – Java 8

This functionality can be achieved with a call to the flatMap method on the stream, which takes a Function that maps the Stream item to another Stream on which you can collect. Here, the flatMap method converts the Stream<Collection<Long>> to a Stream<Long>, and collect collects them into a Collection<Long>. Collection<Long> longs = streamOfCollections .flatMap( coll … Read more

How to workaround this JDK limitation?

Use Constraints: import com.google.common.collect.Constraints; … Constraints.constrainedList(new ArrayList(), Constraints.notNull()) from Guava for maximum flexibility. UPDATE: Guava Constraints has been deprecated in Release 15 – apparently without replacement. UPDATE 2: As of now (Guava 19.0-rc2) Constrains is still there and not deprecated anymore. However, it’s missing from the Javadoc. I’m afraid that the Javadoc is right as … Read more

Magento: Set LIMIT on collection

There are several ways to do this: $collection = Mage::getModel(‘…’) ->getCollection() ->setPageSize(20) ->setCurPage(1); Will get first 20 records. Here is the alternative and maybe more readable way: $collection = Mage::getModel(‘…’)->getCollection(); $collection->getSelect()->limit(20); This will call Zend Db limit. You can set offset as second parameter.

How to modify or delete items from an enumerable collection while iterating through it in C#

Iterating Backwards through the List sounds like a better approach, because if you remove an element and other elements “fall into the gap”, that does not matter because you have already looked at those. Also, you do not have to worry about your counter variable becoming larger than the .Count. List<int> test = new List<int>(); … Read more

Why is Collections.addAll supposed to be faster than c.addAll

Let’s take a closer look at the two of them: // a) col.addAll(Arrays.asList(1, 2, 3, 4, 5)); Here’s what happens: varags + autoboxing creates Integer[] Arrays.asList creates a List<Integer> backed by the array addAll iterates over a Collection<Integer> using Iterator<Integer> // b) Collections.addAll(col, 1, 2, 3, 4, 5); Here’s what happens: varargs + autoboxing creates … Read more

Java Modcount (ArrayList)

It allows the internals of the list to know if there has been a structural modification made that might cause the current operation to give incorrect results. If you’ve ever gotten ConcurrentModificationException due to modifying a list (say, removing an item) while iterating it, its internal modCount was what tipped off the iterator. The AbstractList … Read more

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