Remove elements from collection while iterating

Let me give a few examples with some alternatives to avoid a ConcurrentModificationException. Suppose we have the following collection of books List<Book> books = new ArrayList<Book>(); books.add(new Book(new ISBN(“0-201-63361-2”))); books.add(new Book(new ISBN(“0-201-63361-3”))); books.add(new Book(new ISBN(“0-201-63361-4”))); Collect and Remove The first technique consists in collecting all the objects that we want to delete (e.g. using an … Read more

How can you iterate over the elements of an std::tuple?

I have an answer based on Iterating over a Tuple: #include <tuple> #include <utility> #include <iostream> template<std::size_t I = 0, typename… Tp> inline typename std::enable_if<I == sizeof…(Tp), void>::type print(std::tuple<Tp…>& t) { } template<std::size_t I = 0, typename… Tp> inline typename std::enable_if<I < sizeof…(Tp), void>::type print(std::tuple<Tp…>& t) { std::cout << std::get<I>(t) << std::endl; print<I + 1, … Read more

How to iterate over a JavaScript object?

For most objects, use for .. in : for (let key in yourobject) { console.log(key, yourobject[key]); } With ES6, if you need both keys and values simultaneously, do for (let [key, value] of Object.entries(yourobject)) { console.log(key, value); } To avoid logging inherited properties, check with hasOwnProperty : for (let key in yourobject) { if (yourobject.hasOwnProperty(key)) … Read more