How to convert an Iterator to a Stream?

One way is to create a Spliterator from the Iterator and use that as a basis for your stream: Iterator<String> sourceIterator = Arrays.asList(“A”, “B”, “C”).iterator(); Stream<String> targetStream = StreamSupport.stream( Spliterators.spliteratorUnknownSize(sourceIterator, Spliterator.ORDERED), false); An alternative which is maybe more readable is to use an Iterable – and creating an Iterable from an Iterator is very easy … Read more

Iterator invalidation rules for C++ containers

C++03 (Source: Iterator Invalidation Rules (C++03)) Insertion Sequence containers vector: all iterators and references before the point of insertion are unaffected, unless the new container size is greater than the previous capacity (in which case all iterators and references are invalidated) [23.2.4.3/1] deque: all iterators and references are invalidated, unless the inserted member is at … Read more

How to iterate (keys, values) in JavaScript? [duplicate]

tl;dr In ECMAScript 2017, just call Object.entries(yourObj). In ECMAScript 2015, it is possible with Maps. In ECMAScript 5, it is not possible. ECMAScript 2017 ECMAScript 2017 introduced a new Object.entries function. You can use this to iterate the object as you wanted. ‘use strict’; const object = {‘a’: 1, ‘b’: 2, ‘c’ : 3}; for … Read more

How to build a basic iterator?

Iterator objects in python conform to the iterator protocol, which basically means they provide two methods: __iter__() and __next__(). The __iter__ returns the iterator object and is implicitly called at the start of loops. The __next__() method returns the next value and is implicitly called at each loop increment. This method raises a StopIteration exception … Read more

Calling remove in foreach loop in Java [duplicate]

To safely remove from a collection while iterating over it you should use an Iterator. For example: List<String> names = …. Iterator<String> i = names.iterator(); while (i.hasNext()) { String s = i.next(); // must be called before you can call i.remove() // Do something i.remove(); } From the Java Documentation : The iterators returned by … Read more

How can I iterate over files in a given directory?

Python 3.6 version of the above answer, using os – assuming that you have the directory path as a str object in a variable called directory_in_str: import os directory = os.fsencode(directory_in_str) for file in os.listdir(directory): filename = os.fsdecode(file) if filename.endswith(“.asm”) or filename.endswith(“.py”): # print(os.path.join(directory, filename)) continue else: continue Or recursively, using pathlib: from pathlib import … Read more

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