Print ArrayList data using Stream - stream

I am using below code, but it does not prints the list member line by line, rather prints the entire list.
ArrayList aList=new ArrayList<>();
aList.add("Pink");
aList.add("Green");
aList.add("Red");
aList.add("Yellow");
Stream.of(aList).forEach(s -> System.out.println(s));
output that i get
[Pink, Green, Red, Yellow]
I require to print each of these elements in a new line.

Write it like this :
aList.forEach(System.out::println);
Stream.of(T... values) takes an array as parameter, for Collections you can call the forEach method directly on the instance (like above).

Related

Accessing array elements in lua does not work well

When I have a function that return multiple values I can either store them in seperat values or use the {} operator to get an array.
To access the values I can either define a variable to store the value or access the array via array[index]. When using a temp var to print the value I code:
function myTest()
return "abc", "def", "geh";
end
a = {myTest()};
v = a[2];
print(v);
which works very well. But when printing the "indexed array converted return value" from the function with
function myTest2()
return "abc", "def", "geh";
end
print({myFunction2()}[2]);
nothing gets printed.
Can someone explain me why?
The form:
{myFunction2()}[2]
is not syntactically valid. I get an unexpected symbol error for that.
You can write it like:
({myFunction2()})[2]
and then it works as expected.
Just don't. When you want to immediately access the Nth return value of a function, use (select(N, ...)), which does not create a new table (and thus creates less work for the GC)
function myTest2()
return "abc", "def", "geh";
end
print( (select(2, myFunction2())) );
Note that enclosing a list of values in () truncates it to the first value; this is necessary because select(N, ...) returns the Nth and all following values. (select(N, ...)) returns only the Nth value.

How to convert py2neo.database.cursor class to a dictionary or list in python?

Using graph.run() py2neo v3 to connect to neo4j DB: How can I convert an instance of py2neo.database.Cursor class to a dictionary or list in python?
Was simple in py2neo v2 using py2neo.cypher.core.RecordList class which is what graph.cypher.execute() the equivalent to graph.run would have returned if using the previous version...
Looks like you could do with the data method:
http://py2neo.org/v3/database.html#py2neo.database.Cursor.data
This is designed for use with libraries like Pandas where you need to extract the entire result.
As pointed out by Nigel Small, you can use the .data() method to convert it into a list.
Here is how it works, let's take for example this line of code calling graph.run, whose result is saved into tags variable:
tags = graph.run(query)
the result is a py2neo.database.Cursor class (containing a dictionary):
tags
-------------------------------
['py2neo', 'python', 'neo4j']
And by applying the .data() method, it becomes:
tags = graph.run(query).data()
whose result is a list (containing a dictionary):
[{'tags': ['py2neo', 'python', 'neo4j']}]
You can just cast it to a list :
result = session.run("MATCH (n) RETURN n")
records = list(result)

List of functions

Following definitions print already the result of the functions in the table:
function plus1(zahl) print(zahl+1) end
function plus2(zahl) print(zahl+2) end
function plus3(zahl) print(zahl+3) end
-- already prints out 6,5,10
local tfunc={plus1(5),plus2(3),plus3(7)}
how can I avoid this?
how can I iterate through the functions with given parameters in the table? I would like to call the functions like:
tfunc[1]
to print out 6. But it does not work.
#pschulz, thanks to show me the way :
local tfunc = {{plus1,5},{plus2,3},{plus3,7}}
tfunc[i][1](tfunc[i][2])
allows to iterate with index i through the different functions with different args. The trick is tables with function name and args inside the table.
In you table, you are currently storing nothing (or three nil). The table takes the return value of the function and since you are returning nothing, it gets nil.
What you have to do is store functions:
local tfunc = {
plus1,
plus2,
plus3
}
No you can call your functions like this:
tfunc[1](5)
On iterating: If i understand correctly, you want to do the following:
local tfunc = {
plus1,
plus2,
plus3
}
local tvalues = { 5, 3, 7 }
for i, func in ipairs(tfunc) do
func(tvalues[i])
end
So you have to save your values in another table. There are more elegant ways to do this, i suggest you have a reading on closures.
local tfunc={plus1(5),plus2(3),plus3(7)}
Here you call the functions inside the table constructor.
So once you create your table you will print those values. As your function does not return anything tfunc remains empty.
You can either store the function call as a string and let Lua execute that string or you can save the function in the table without calling it. Then you need some way to get the function parameter zahl into your function call.
To me what you want to do makes no sense.
If you want to call it like tfunc[1] it will always print 6. So why not just call print(6)?
If you want to add 3 to a number you want to print, just call print(number + 3) e.g.
Your code will remain more readable and easier to understand if you don't move simple arrithmetics into an extra function.

Polymer Dart #observable different behaviour

In the following Polymer Dart code:
#observable Map hisMap = toObservable(new Map()); // Line 1
#observable String hisString = ''; // Line 2
Why does Line 1 need toObservable and Line 2 doesn't need it?
Line 1
If you don't use toObservable only changes to hisMap will be recognized. For example when you assign another map to hisMap but not changes to values inside the map.
With toObservable the content of the map is observed as well and adding, removing, replacing of items in the map produces change events.
Line2
The only way this value can change is when you assign another string to hisString. Strings are immutable, maps are not.

Issue with using int.parse() in Dart

I'm currently teaching myself the Dart language, and my first app doesn't seem to be working right. Here's the code that's causing trouble:
usrLoc = int.parse(query("#txtLoc").text);
When I try to run the app, it opens fine, but when I click the button that triggers this (and three other similar parses), the debugger stops and tells me "Source not found" for int._native_parse(), int._parse(), and int.parse().
Any help would be greatly appreciated.
The text property for the specified element #txtLoc returns an empty string.
The parse method requires that:
The source must be a non-empty sequence of base- radix digits, optionally prefixed with a minus or plus sign ('-' or '+').
You can specify an onError named argument in your call to parse, which takes a callback that handles the invalid input. E.g., if you want the parse call to return the value 42 for all invalid input, you can do this:
usrLoc = int.parse(query("#txtLoc").text, onError: (val) => 42);
If you really expect the element to have some text, you can store the result of query("#txtLoc").text into a separate variable and verify the value. It would also be interesting to check what the real element type is or which tag is marked with id #txtLoc.
If you want to get the content of an input element, you should use the value property instead of text:
query("#txtLoc").value

Resources