是否可以使用并行流和lambda执行下面的代码,因为它是一个更清晰的代码? 我不知道该怎么做。
public static NavigableSet<String> getDistinctNavigableLowerCaseMakeNames(Loader loader) {
String[] regions = {Japan, Germany, USA};
final CountDownLatch latch = new CountDownLatch(regions.length);
Set<Make> unique = new HashSet<>();
for (Region region : regions) {
EXECUTOR.submit(new Runnable() {
@Override public void run() {
List<Make> regionMakes = getVehicleMakesByRegion(region); //provides all the vehicles based on the region
unique.addAll(regionMakes);
latch.countDown();
}
});
}
try {
latch.await();
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new RuntimeException(ie);
}
return unique;
}
Make是一个由最终变量id,region和name组成的类。
答案 0 :(得分:1)
据我了解,您希望找到区域列表的独特品牌。如果是这样的话:
Arrays.stream("Japan", "Germany", "USA").parallel()
.map(loader::getVehicleMakesByRegion)
.flatMap(List::stream)
.map(Make::getName)
.distinct()
...
如果Make.equals
存在,那么您可以在distinct
之前移动getName
。更改getVehicleByRegion
以返回Stream<Make>
。
答案 1 :(得分:0)
这样的事情怎么样,看起来有点短:)
override func viewDidAppear(_ animated: Bool) {
let query = PFQuery(className: "ProfilePictures")
query.whereKey("user", equalTo: (PFUser.current()?.objectId!)! as String)
query.findObjectsInBackground { (objects, error) in
if let objectss = objects{
if objectss.count != 0{
for object in objectss{
let imagePulled = object["ProfilePicture"] as! PFFile
imagePulled.getDataInBackground(block: { (data, error) in
if let downloadedImage = UIImage(data: data!){
self.imageHolder.image = downloadedImage
} else{
print(error)
}
})
}
}
}
}
}
请记住向Loader接口添加注释,指定实现必须是线程安全的。