我有一系列哈希
例如:
cars = [{:company => "Ford", :type => "SUV"},
{:company => "Honda", :type => "Sedan"},
{:company => "Toyota", :type => "Sedan"}]
# i want to fetch all the companies of the cars
cars.collect{|c| c[:company]}
# => ["Ford", "Honda", "Toyota"]
# i'm lazy and i want to do something like this
cars.collect(&:company)
# => undefined method `company'
Run Code Online (Sandbox Code Playgroud)
我想知道是否有类似的快捷方式来执行上述操作.
我有两个简单的类ImageEntity和ImageList
如何将结果列表ImageEntity收集到ImageList?
List<File> files = listFiles();
ImageList imageList = files.stream().map(file -> {
return new ImageEntity(
file.getName(),
file.lastModified(),
rootWebPath + "/" + file.getName());
}).collect(toCollection(???));
Run Code Online (Sandbox Code Playgroud)
类
public class ImageEntity {
private String name;
private Long lastModified;
private String url;
...
}
Run Code Online (Sandbox Code Playgroud)
和
public class ImageList {
private List<ImageEntity> list;
public ImageList() {
list = new ArrayList<>();
}
public ImageList(List<ImageEntity> list) {
this.list = list;
}
public boolean add(ImageEntity entity) {
return list.add(entity);
}
public void addAll(List<ImageEntity> list) {
list.addAll(entity);
}
}
Run Code Online (Sandbox Code Playgroud)
这不是一个优雅的解决方案 …
我发现这个在线代码是在取消初始化Excel Interop对象后附加的:
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
Run Code Online (Sandbox Code Playgroud)
这是对DRY的准违反(以口吃的方式连续两次调用GC.Collect()和GC.WaitForPendingFinalizers())有什么用处,或者只是浪费时间?
我刚刚发现我的提取器中的unapply由于某种原因被调用了两次.任何人都知道为什么,以及如何避免它?
val data = List("a","b","c","d","e")
object Uap {
def unapply( s:String ) = {
println("S: "+s)
Some(s+"!")
}
}
println( data.collect{ case Uap(x) => x } )
Run Code Online (Sandbox Code Playgroud)
这会产生输出:
S: a
S: a
S: b
S: b
S: c
S: c
S: d
S: d
S: e
S: e
List(a!, b!, c!, d!, e!)
Run Code Online (Sandbox Code Playgroud)
最后的结果很好,但在我的真实程序中,unapply是非平凡的,所以我当然不想把它叫两次!
我有一个问题,我真的不知道如何以适当的Scala方式做到这一点.
我有一个对象列表,持有一个日期.我想做这样的事情:

我想在列表中的2个后继者之间使用可接受的时间值(例如2小时)进行选择.目的是保持用户趋势与某点的比较(如果他在这里显示2次,或1或15!).
我想象的算法:
怎么做,有一些过滤器或收集?哦,如果算法听起来不适合你,我会受到批评!
编辑:我不是要求解决方案,而是要查找正确的函数!
我最近开始使用collectAndThen,发现与其他编码程序相比,它需要花费相当长的时间,我用它来执行类似的任务.
这是我的代码:
System.out.println("CollectingAndThen");
Long t = System.currentTimeMillis();
String personWithMaxAge = persons.stream()
.collect(Collectors.collectingAndThen(
Collectors.maxBy(Comparator.comparing(Person::getAge)),
(Optional<Person> p) -> p.isPresent() ? p.get().getName() : "none"
));
System.out.println("personWithMaxAge - "+personWithMaxAge + " time taken = "+(System.currentTimeMillis() - t));
Long t2 = System.currentTimeMillis();
String personWithMaxAge2 = persons.stream().sorted(Comparator.comparing(Person::getAge).reversed())
.findFirst().get().name;
System.out.println("personWithMaxAge2 : "+personWithMaxAge2+ " time taken = "+(System.currentTimeMillis() - t2));
Run Code Online (Sandbox Code Playgroud)
在这里输出:
CollectingAndThen
personWithMaxAge - Peter time taken = 17
personWithMaxAge2 : Peter time taken = 1
Run Code Online (Sandbox Code Playgroud)
这表明收集和收集时间相对较长.
所以我的问题是 - 我应该继续收集和其他建议吗?
使用 Groovy,要求是收集地图的嵌套元素值及其顶级元素值。
不确定是否需要递归方法。
示例 JSON
{
"items": [
{
"attribute": "Type",
"options":
[
{
"label": "Type1",
"value": "1"
},
{
"label": "Type2",
"value": "2"
}
]
},
{
"attribute": "Size",
"options": [
{
"label": "SizeA",
"value": "A"
},
{
"label": "SizeB",
"value": "B"
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
收集后的预期输出
[
{attribute=Type,label=Type1,value=1},
{attribute=Type,label=Type2,value=2},
{attribute=Size,label=SizeA,value=A},
{attribute=Size,label=SizeB,value=B}
]
Run Code Online (Sandbox Code Playgroud) 这段代码(游乐场):
fn resolve_score(string: String) -> u16 {
let mut score: u16;
string
.drain(..)
.map(|char| {
match char {
'a' => score += 1,
'f' => score += 4,
_ => ()
};
})
.collect();
}
Run Code Online (Sandbox Code Playgroud)
生成此错误:
<anon>:16:14: 16:21 error: unable to infer enough type information about `_`; type annotations or generic parameter binding required [E0282]
<anon>:16 .collect();
^~~~~~~
<anon>:16:14: 16:21 help: see the detailed explanation for E0282
error: aborting due to previous error
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
我有一个整数列表
List<Integer> lst = new ArrayList<>();
lst.add(10);
lst.add(15);
lst.add(16);
lst.add(8);
lst.add(100);
lst.add(1);
lst.add(40);
Run Code Online (Sandbox Code Playgroud)
如何编写代码以便我可以从列表中获取前 5 个最大元素,即100, 40, 16, 15, 10?
我尝试过使用 Java 流 API:
Integer var = lst.stream().max(Integer::compare).get();
Run Code Online (Sandbox Code Playgroud)
但只得到一个值元素。
我正在测试collect流的方法。在这段代码中,我试图通过StringBuilder在第一种情况下使用 a并String在第二种情况下使用连接来从字符串流构建单个字符串。我不明白为什么第二种情况会产生一个空字符串。
String[] strs = new String[] {"aaaaa", "bbbbbbb", "c", "dd"};
StringBuilder sb = Arrays.stream(strs).collect(StringBuilder::new, StringBuilder::append, StringBuilder::append);
System.out.println(sb);
String s = Arrays.stream(strs).collect(String::new, String::concat, String::concat);
System.out.println(s);
Run Code Online (Sandbox Code Playgroud)