小编Amy*_*y B的帖子

如何在Scala/ReactiveMongo中映射两个`future`结果?

我有一个用Play和ReactiveMongo编写的应用程序,我想要:

  • 有一个将landingPage文档插入MongoDB的操作.
  • 插入新的landingpage并等待插入.
  • 计算新的landingPage文档总数并将其返回给用户.

我有这个工作代码:

// Insert the landing page and wait for it to be inserted, so we can then get the new count of landing pages.
val futures = for {
  wr <- landingPagesCollection.insert(landingPage)
  count <- landingPagesCollection.count()
} yield count

futures.map { (count: Int) =>
  Created(Json.obj(
    "created" -> true,
    "landingPage" -> landingPage.toJson,
    "count" -> count
  ))
}
Run Code Online (Sandbox Code Playgroud)

这段代码工作正常.但是,出于好奇,我想知道如何访问wr(WriteResult)值.当我将代码更改为:

val futures = for {
  wr <- landingPagesCollection.insert(landingPage)
  count <- landingPagesCollection.count()
} yield (wr, count)

futures.map …
Run Code Online (Sandbox Code Playgroud)

scala future mongodb reactivemongo

2
推荐指数
2
解决办法
326
查看次数

为什么Java不允许"new List <T>"?

要创建List,为什么Java不允许创建它们然后逐个添加元素?

这有效:

public static List<TrackedItem> create(List<Item> items)
{
    TrackedItem[] arr = new TrackedItem[items.size()];

    int i = 0;
    for (Item item : items)
    {
        arr[i] = TrackedItem.createOrUpdate(item);

        i++;
    }

    return java.util.Arrays.asList(arr);
}
Run Code Online (Sandbox Code Playgroud)

这不起作用(tracked.add()导致a NullPointerException):

public static List<TrackedItem> create(List<Item> items)
{
    List<TrackedItem> tracked = java.util.Collections.emptyList();

    for (Item item : items)
    {
        tracked.add(TrackedItem.createOrUpdate(item));
    }

    return tracked;
}
Run Code Online (Sandbox Code Playgroud)

java generics

1
推荐指数
1
解决办法
4246
查看次数

在Java中传递一个类("Country.class")作为参数

我试图使这需要的参数的方法Country.class,User.class等等,并返回argument.count().

我将为此方法提供的所有可能的类都扩展Model并拥有该方法count().

我的代码:

private static long <T> countModel(Model<T> clazz)
{
    // there is other important stuff here, which prevents me from
    // simply by-passing the method altogether.

    return clazz.count();
}
Run Code Online (Sandbox Code Playgroud)

被称为:

renderArgs.put("countryCount", countModel(Country.class));
Run Code Online (Sandbox Code Playgroud)

然而,这根本不起作用.

我该怎么办?

java casting

0
推荐指数
1
解决办法
846
查看次数

0
推荐指数
1
解决办法
1537
查看次数