Java: is there a map function?

Since Java 8, there are some standard options to do this in JDK: Collection<E> in = … Object[] mapped = in.stream().map(e -> doMap(e)).toArray(); // or List<E> mapped = in.stream().map(e -> doMap(e)).collect(Collectors.toList()); See java.util.Collection.stream() and java.util.stream.Collectors.toList().

Are list-comprehensions and functional functions faster than “for loops”?

The following are rough guidelines and educated guesses based on experience. You should timeit or profile your concrete use case to get hard numbers, and those numbers may occasionally disagree with the below. A list comprehension is usually a tiny bit faster than the precisely equivalent for loop (that actually builds a list), most likely … Read more

Is there a difference between foreach and map?

Different. foreach iterates over a list and performs some operation with side effects to each list member (such as saving each one to the database for example) map iterates over a list, transforms each member of that list, and returns another list of the same size with the transformed members (such as converting a list … Read more

JavaScript “new Array(n)” and “Array.prototype.map” weirdness

I had a task that I only knew the length of the array and needed to transform the items. I wanted to do something like this: let arr = new Array(10).map((val,idx) => idx); To quickly create an array like this: [0,1,2,3,4,5,6,7,8,9] But it didn’t work because: (see Jonathan Lonowski’s answer a few answers beneath) The … Read more

Getting a map() to return a list in Python 3.x

Do this: list(map(chr,[66,53,0,94])) In Python 3+, many processes that iterate over iterables return iterators themselves. In most cases, this ends up saving memory, and should make things go faster. If all you’re going to do is iterate over this list eventually, there’s no need to even convert it to a list, because you can still … Read more

map function for objects (instead of arrays)

There is no native map to the Object object, but how about this: var myObject = { ‘a’: 1, ‘b’: 2, ‘c’: 3 }; Object.keys(myObject).forEach(function(key, index) { myObject[key] *= 2; }); console.log(myObject); // => { ‘a’: 2, ‘b’: 4, ‘c’: 6 } But you could easily iterate over an object using for … in: var … Read more

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