大家好,我有以下java代码:
TimeZoneObj timezone;
for( myObject obj: objectArr)
{
if((obj.getName).equal("timeZone"))
{
timezone=db.getTmezone(obj.getId());
}
}
我可以成功将其转换为lambada,如下所示:
TimeZone timezone = Arrays.stream(objectArr)
.filter(obj -> obj.getName().equals("timeZone")) //filters
.map(obj -> db.getTmezone(obj.getId()))
.findAny().oeElse(new TimeZoneObj);
但是如果我在主代码中有其他内容怎么办:
TimeZoneObj timezone;
CalendarObj calenda;
for( myObject obj: objectArr)
{
if((obj.getName).equal("timeZone"))
{
timezone=db.getTmezone(obj.getId());
}
else if ((obj.getName).equal("calendar"))
{
calendar=db.getCalendar(obj.getId());
}
}
任何人都可以帮我解决如何将上述代码转换为lambda的问题吗?
答案 0 :(得分:2)
我不确定这是你想要的,但你可以用两个流来做到这一点:
Stream<myObject> timezones = Arrays.stream(objectArr).filter(obj -> obj.getName().equals("timeZone"));
Stream<myObject> calendars = Arrays.stream(objectArr).filter(obj -> obj.getName().equals("calendar"));
TimeZone timezone = timezones.map(obj -> db.getTmezone(obj.getId())).findAny().get();
CalendarObj calendar = calendars.map(obj -> db.getCalendar(obj.getId())).findAny().get();
答案 1 :(得分:2)
在@Adi Levin的回答中回答你的问题,是的,lambda并非用于所有目的的解决方案。
话虽如此,你可以试试以下:
创建一个方法将数组元素对象映射到目标对象:
int main()
{
string str = "NULL";
int mappings = 0;
readMappings(mappings);
receiveInput(str);
displayInput(mappings, str);
return 0;
}
void readMappings(int &count)
{
ifstream readMappings; // Creates the function "readMappings"
string filename; // Generates the filename variable to search for mapping document
cout << "Enter the filename of the mapping document: ";
cin >> filename; // User enters filename
readMappings.open(filename); // "readMappings" function opens the given mappings document
while (!readMappings.is_open())
{
cout << "Unsble to open file. Please enter a valid filename: "; // If the user enters an invaled filename, the program will ask again
cin >> filename;
readMappings.open(filename);
}
if (readMappings.good()) // Mapping document is successfully opened
{
readMappings >> count; // Reads first line
}
readMappings.close(); // If everything fails in this function, the document will close
}
void receiveInput(string &input)
{
char correctness;
do {
cout << "\nPlease enter the text you would like to be converted to NATO:\n";
getline(cin, input);
cout << "You are about to convert: \"" << input << "\".\nIs this correct? (Y/N)" << endl;
cin >> correctness;
} while (correctness == 'N' || correctness =='n');
}
然后您可以使用它来收集您所需的对象:
private Object fn(MyObj obj) {
if (obj.getName().equals("timeZone")) {
return db.getTmezone(obj.getId());
} else if (obj.getName().equals("calendar")) {
return db.getCalendar(obj.getId());
} else {
return null;
}
}
所以你最终的列表最多只有2个对象。但你仍然要找出对象的类型和顺序。
答案 2 :(得分:0)
func cropImage(screenshot: UIImage) -> UIImage {
let scale = screenshot.scale
let imgSize = screenshot.size
let screenHeight = UIScreen.mainScreen().bounds.height
let bound = self.view.bounds.height
let navHeight = self.navigationController!.navigationBar.frame.height
let bottomBarHeight = screenHeight - navHeight - bound
let crop = CGRectMake(0, 200, //"start" at the upper-left corner
(imgSize.width - 1) * scale, //include half the width of the whole screen
(imgSize.height - bottomBarHeight - 300) * scale) //include the height of the navigationBar and the height of view
let cgImage = CGImageCreateWithImageInRect(screenshot.CGImage, crop)
let image: UIImage = UIImage(CGImage: cgImage!)
return image
}
我不知道。这可能不是一种不那么黑客的方式。我假设你可以......用三元运算符完成它,但唯一真实的是Adi Levin说,有两个流。