I am converting a List<City> into a Map<CityType, Set<City>>.
City within it has fields like CityIdentifier, State, Latitude etc.
CityIdentifier within it has a cityName & cityType.
For the above conversion, for the Collectors.groupingBy, I need a function like City::getCityType. Can I use the getter from CityIdentifier i.e. something like City::getCityIdentifier.getCityType?
You can't use a method reference in this example.
Use a lambda expression instead:
city -> city.getCityIdentifier().getCityType()
And the full pipeline:
Map<CityType, Set<City>> map =
list.stream()
.collect(Collectors.groupingBy(city -> city.getCityIdentifier().getCityType(),
Collectors.toSet()));
Related
I'm working on a Wordclock with an Arduino so we're in C++. I want to save the corresponding Pixels and length of the words in a type so I can call a function that takes the Word to display as a property.
So here is what I came up with:
(Pseudocode)
type WORD
property1 = pixelPosition (integer)
property2 = wordLength (also integer)
I also would like to implement all the values in a compact form like in a enum like this:
word1= (123, 3);
Instead of this:
word1.pixelPos = 123;
word1.length = 3;
Unfortunately I didnt find a way to set two values in an enum. Is there a way I can create a custom type and set it to be the kind of type I want to enum?
I want to make a data class which can accept both list and mutable-list and if the list is instance of MutableList then directly make it a property else if it is a List then convert it into a MutableList and then store it.
data class SidebarCategory(val title: String, val groups: MutableList<SidebarGroup>) {
constructor(title: String, groups: List<SidebarGroup>) :
this(title, if (groups is MutableList<SidebarGroup>) groups else groups.toMutableList())
}
In the above code Platform declaration clash: The following declarations have the same JVM signature error is thrown by the secondary constructor of the class (2nd line).
How should I approach this? Should I use a so called fake constructor (Companion.invoke()) or is there any better work-around?
List and MutableList are mapped to the same java.util.List class (mapped-types), so from JMV it will look like SidebarCategory has two identical constructors.
Instead of List, you can use Collection in the second constructor.
Use Collection instead of List, and then make an init block that sets it equal to a mutable list, as so:
data class SidebarCategory(val title: String, groups: Collection<SidebarGroup>) {
val groups = mutableListOf<>(groups)
}
When using a predicate, we can have:
filterPredicate = [NSPredicate predicateWithFormat:#"(ANY names.firstName contains[c] %#), nameToSearchFor];
This use of ANY allows us to find any object where any of the objects in the names collection has a firstName containing the desired text.
Is it possible to do something similar with filter expressions in Swift 3? In other words, something like:
allPeople.filter { $0.(ANY)names.firstName.contains(searchString) };
(The above ANY syntax is made up for illustration).
Perhaps could be done by nesting a reduce that concatenates all the firstNames, then see if my target string is contained in that?
allPeople.filter { $0.names.contains(where: { $0.firstName.contains(searchString) }) }
I'm writing an application that allows the user to configure the output using templates. For example:
Variables:
name = "BoppreH"
language = "Python"
Template:
My name is {name} and I like {language}.
Output:
My name is BoppreH and I like Python.
This works fine for simple data, like strings and numbers, but I can't find a good syntax for lists, more specifically for their delimiters.
fruits = ["banana", "apple", "watermelon"]
I like {???}.
I like banana, apple, watermelon.
In this case the desired delimiter was a comma, but how can the user specify that? Is there some template format with this feature?
I'm more concerned about making the syntax simple to understand, regardless of language or library.
Implement filters, and require their use for non-scalar types.
I like {fruits|join:", "}.
Typically, a list contains an unknown number of members, and sometimes variables/placeholders of its own. An example might be listing the name of a person along with their phone numbers. The desired output might look something like this:
John Doe
555-1212
555-1234
In a templating system that supported this, you'd need two types of variables: One that designated a placeholder for a value (like the curly braces you're using now), and another to denote the start and end of the list. So, something like this:
{name}
{{phone_numbers}}{phone}{{/phone_numbers}}
Your array of values might look like this:
values = [names: "John Doe", phone_numbers: [ phone: "555-1212", phone: "555-1234"]]
Each value in the "phone_numbers" array would create a new instance of everything that existed between {{phone_numbers}} and {{/phone_numbers}}, placing the values contained within those two "tags".
I'm using jsoncpp parser (http://jsoncpp.sourceforge.net) to parse JSON data.
So, if we have the following JSON:
{ "name": "Joseph", "age": 20 }
How can I get the property name name and value Joseph, ... after age and 20?
OK, we can do universally this:
string e = root.get(propertyName, defaultValue).asString();
But really what we want is something like this:
string e = root.get(name, "Mark").asString();
Now, variable e is Joseph, it works. But I have to take/write "name". I do not want to QUERY (without questioning the function I want to get "name" (name of property) and "Joseph" (value of property)).
After it would be best to store in a field (C/C++ for example):
property[name][0] = "Joseph"
property[age][0] = 20
How can I do that? Or any other ideas?
You can get all the member names of a Json::Value object using its getMemberNames() function. That returns an object that you can iterate over using .begin() and .end(), like any other standard library container. (In fact, the return type is an alias for std::vector<std::string>.)
After you have the member names, you should be able to iterate through them and use .get(std::string &, const ValueType &) as you already are doing to get the values for each of the object's keys.
Note that JSON objects are inherently unordered, so you can't necessarily rely on that name list having any ordering whatsoever. If you want an ordered object, you should be using JSON arrays, not JSON objects.