让我们假设我们有一个国家列表:List<Country>每个国家都有一个区域列表的参考:( List<Region> 例如美国的州).像这样的东西:
USA
Alabama
Alaska
Arizona
...
Germany
Baden-Württemberg
Bavaria
Brandenburg
...
Run Code Online (Sandbox Code Playgroud)
在"普通的"Java中,我们可以计算所有区域,例如:
List<Country> countries = ...
int regionsCount = 0;
for (Country country : countries) {
if (country.getRegions() != null) {
regionsCount += country.getRegions().size();
}
}
Run Code Online (Sandbox Code Playgroud)
是否有可能通过Java 8 Stream API实现相同的目标?我想到了类似的东西,但我不知道如何使用count()流API的方法计算嵌套列表的项目:
countries.stream().filter(country -> country.getRegions() != null).???
Run Code Online (Sandbox Code Playgroud) 我有一个<textarea name="desc" />可能包含换行符的field(),我想用它们的HTML替换它们:<br />.我怎样才能做到这一点?我正在使用Thymeleaf 2.1.4.RELEASE.
查看Immutable 的文档,有一个代码示例:
@Immutable
data class Person(val name: String, val phoneNumber: String)
@Composable
fun PersonView(person: Person) {
Column {
Row {
Text("Name: ")
Text(person.name)
}
Row {
Text("Phone: ")
Text(person.phoneNumber)
}
}
}
@Composable
fun PeopleView(people: List<Person>) {
Column {
for (person in people) {
PersonView(person)
}
}
}
Run Code Online (Sandbox Code Playgroud)
和标题:
如果将 Person 标记为不可变,则如果与上次合成期间是同一个人,则可以跳过调用 PersonView Composable 函数。
我的问题是:
如果重组仅在函数参数@Composable更改时发生,并且像上面的代码中那样的数据类Person(即仅包含原始值)不能在不创建新实例的情况下更改,那么这里的优化是什么?与没有注释
的相同代码相比,什么时候会跳过对 PersonView Composable 函数的调用?它是否与 的可变/不稳定参数有关?@Immutablepeople: List<Person>PeopleView
TL; DR:
@Transactional(propagation = Propagation.NOT_SUPPORTED)javax.persistence.TransactionRequiredException: no transaction is in progress被抛出题:
代码片段(Spring用作主框架):
DAO:
@Repository
public class UrlDaoImpl implements UrlDao {
@Autowired
private SessionFactory sessionFactory;
@Override
public List<Url> getAllUrls() {
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("from Url");
return query.list();
}
}
Run Code Online (Sandbox Code Playgroud)
服务:
@Service
public class UrlServiceImpl implements UrlService {
@Autowired
private UrlDao urlDao;
@Override
@Transactional // THIS WORKS IN NEW HIBERNATE
public List<Url> getAllUrls() {
return urlDao.getAllUrls();
}
@Override …Run Code Online (Sandbox Code Playgroud) 在阅读https://developer.android.com/kotlin/coroutines时,我偶然发现了以下警告:
警告:启动和异步处理异常的方式不同。由于 async 期望在某个时刻最终调用await,因此它会保留异常并作为await 调用的一部分重新抛出它们。这意味着如果您使用await 从常规函数启动新的协程,您可能会默默地丢弃异常。这些丢弃的异常不会出现在您的崩溃指标中,也不会在 logcat 中注明。
但是,在浏览https://kotlinlang.org/docs/reference/coroutines/exception-handling.html或https://www.google返回的任何其他资源时,我无法找到这种无声丢弃行为的任何示例.com/search?q=kotlin+await+exception+handling - 相反,所有资源都表明在 async/await 块中抛出的异常将导致整个协程范围内的失败,这是正确且预期的。
恐怕我在这里遗漏了一些东西,您能否提供一个示例,其中发生这种静默异常丢弃,而 logcat 中无法注意到?
正如指出这太问题和许多其他类似的执行顺序<script>S中的页面上应该是相同的,其中这些标签在HTML文档中定义的顺序.
我创建了一个简单的Java(服务器端)测试应用程序,它允许执行请求并在返回响应之前等待指定的时间段(此问题底部的相关代码段).它有一个简单的API:
http://localhost:8080/latency?time=XXX&response=YYY
Run Code Online (Sandbox Code Playgroud)
console.log('done')一秒钟后返回的示例请求(1000毫秒):
http://localhost:8080/latency?time=1000&response=console.log(%27done%27)
Run Code Online (Sandbox Code Playgroud)
接下来,我创建了一个简单的index.html页面(由nginx提供):
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test order</title>
</head>
<body>
<script type="text/javascript" async=false src="http://localhost:8080/latency?time=1000&response=console.log(%27done1%27)"></script>
<script type="text/javascript" async=false src="http://localhost:8080/latency?time=100&response=console.log(%27done2%27)"></script>
<script type="text/javascript" async=false src="http://localhost:8080/latency?time=10&response=console.log(%27done3%27)"></script>
<script>console.log('static script without "src" attr');</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
根据我到目前为止所读到的一切,我预计控制台输出的顺序是:
done1
done2
done3
static script without "src" attr
Run Code Online (Sandbox Code Playgroud)
这就是我得到的(Firefox 51开发控制台):
这与我期望的相反.我错过了什么吗?有没有办法以所需的顺序执行这些脚本(即按照HTML中定义的顺序)?
作为参考,服务器端的Java部分:
private String latency(HttpServletRequest request) {
long millis = Long.parseLong(request.getParameter("time"));
String response = request.getParameter("response");
try {
Thread.sleep(millis);
return (response != null) ? response : …Run Code Online (Sandbox Code Playgroud) java ×3
android ×2
hibernate ×1
java-8 ×1
java-stream ×1
javascript ×1
kotlin ×1
spring ×1
spring-mvc ×1
thymeleaf ×1
transactions ×1