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 Stream: divide into two lists by boolean predicate

Collectors.partitioningBy: Map<Boolean, List<Employee>> partitioned = Collectors.partitioningBy(Employee::isActive)); The resulting map contains two lists, corresponding to whether or not the predicate was matched: List<Employee> activeEmployees = partitioned.get(true); List<Employee> formerEmployees = partitioned.get(false); There are a couple of reasons to use partitioningBy over groupingBy (as suggested by Juan Carlos Mendoza): Firstly, the parameter of groupingBy is a Function<Employee, … Read more

How to limit the maximum size of a Map by removing oldest entries when limit reached [closed]

You can use LinkedHashMap like this You can remove by LRU or FIFO. public static <K, V> Map<K, V> createLRUMap(final int maxEntries) { return new LinkedHashMap<K, V>(maxEntries*10/7, 0.7f, true) { @Override protected boolean removeEldestEntry(Map.Entry<K, V> eldest) { return size() > maxEntries; } }; }

How to compare two Dictionaries in C#

If you’ve already checked that the keys are the same, you can just use: var dict3 = dict2.Where(entry => dict1[entry.Key] != entry.Value) .ToDictionary(entry => entry.Key, entry => entry.Value); To explain, this will: Iterate over the key/value pairs in dict2 For each entry, look up the value in dict1 and filter out any entries where the … Read more