最新项目我使用Spring启动,并准备部署到生产环境,我想知道哪种方式运行应用程序有更好的性能或具有相同的性能?
另外,在发布到生产环境时如果要删除devtools依赖.
我有两个List
,例如:
List<Foo> list1 = Lists.newArrayList(new Foo(...),...);
List<Bar> list2 = Lists.newArrayList(new Bar(...),...);
Run Code Online (Sandbox Code Playgroud)
里面Bar
有一个属性,fooId
. 认为list1.size() == list2.size()
。我想按顺序设置fooId
实例Bar
。
我尝试了下面的代码:
int index = 0;
list2.forEach(b -> b.fooId = list1.get(index++).getId());
Run Code Online (Sandbox Code Playgroud)
但编译失败
在封闭范围内定义的局部变量索引必须是最终的或有效最终的
Java 8 有一些方便的方式来处理这个问题吗?
当执行单元测试( mvn test
)时有时会出现以下异常
org.springframework.dao.CannotAcquireLockException:
### Error updating database. Cause: java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction
### The error may involve com.foo.dao.mapper.TestMapper.insertSuccesfulPaymentOrder-Inline
### The error occurred while setting parameters
### SQL: insert into order(order_seq,note,user_id,product_id, pay_status) values(uuid(),'',?,?,1)
### Cause: java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction
; SQL []; Lock wait timeout exceeded; try restarting transaction; nested exception is java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction
Run Code Online (Sandbox Code Playgroud)
从mysql 文档我知道
InnoDB 事务在放弃之前等待行锁的时间长度(以秒为单位)。默认值为 50 秒。尝试访问被另一个 InnoDB 事务锁定的行的事务最多等待这么多秒才能对该行进行写访问,然后才会发出以下错误:
ERROR …
我想计算一个^ b,例如2 ^ 30,
public long pow(final int a, final int b)
Run Code Online (Sandbox Code Playgroud)
首先我用这种方式
return LongStream.range(0, b).reduce(1, (acc, x) -> a * acc); // 1073741824
Run Code Online (Sandbox Code Playgroud)
得到正确的结果.然后我想要平行计算它,所以我自然而然地将它改为
return LongStream.range(0, b).parallel().reduce(1, (acc, x) -> a * acc); // 32
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,结果就是32
.为什么?
所以为了支持并行我再次改变它
return Collections.nCopies(b,a).parallelStream().reduce(1, (acc, x) -> acc * x); // 1073741824
Run Code Online (Sandbox Code Playgroud)
在这种情况下它有效.
那方式有什么问题parallel
?
我有一个文件 - 一个,并且存在一些继续空行(多个),见下文:
cat a
1
2
3
4
5
Run Code Online (Sandbox Code Playgroud)
所以首先我想知道是否存在继续空行,我试过了
cat a | grep '\n\n\n'
Run Code Online (Sandbox Code Playgroud)
没什么输出.所以我必须使用以下方式
vi a
:set list
/\n\n\n
Run Code Online (Sandbox Code Playgroud)
所以我想知道是否存在其他shell命令可以轻松实现这个?然后,如果存在两个或更多的空行我想将它们转换为一个?见下文
1
2
3
4
5
Run Code Online (Sandbox Code Playgroud)
起初我试过下面的shell
sed 's/\n\n\(\n\)*/\n\n/g' a
Run Code Online (Sandbox Code Playgroud)
它不起作用,然后我尝试了这个shell
cat a | tr '\n' '$' | sed 's/$$\(\$\)*/$$/g' | tr '$' '\n'
Run Code Online (Sandbox Code Playgroud)
这次它有效.而且我想知道是否存在其他方式可以实现这一点?
我用的是最新的apache poi
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
但我无法设置粗体字体,下面的代码不起作用
font.setBold(true);
Run Code Online (Sandbox Code Playgroud)
因为默认为 true
set a boolean value for the boldness to use. If omitted, the default value is true.
Run Code Online (Sandbox Code Playgroud)
并且也不存在setBoldWeight
方法
那么如何在最新的apache poi中设置粗体粗细呢?
代码
XSSFWorkbook wb = new XSSFWorkbook()
XSSFSheet sheet = wb.createSheet();
XSSFCell cell = sheet.createRow(0).createCell(0);
cell.setCellValue("hello world");
XSSFCellStyle cellStyle = wb.createCellStyle();
XSSFFont font = wb.createFont();
font.setBold(true);
cellStyle.setFont(font);
cell.setCellStyle(cellStyle);
try (FileOutputStream fos = new FileOutputStream("bold_test.xls")) {
wb.write(fos);
}
Run Code Online (Sandbox Code Playgroud)
影响
大胆的效果应该是这样的
我使用java8的最新项目,发现处理Collection类型非常方便,它给了我很大的惊喜.但突然觉得一些传统的语法是如此繁琐,例如我有一个名为BuyerOrderCountStats的Object,每次买家提交订单,都会增加他的订单数,
BuyerOrderCountStats bocs = ...;
bocs.setOrderCount(bocs.getOrderCount()+1);
Run Code Online (Sandbox Code Playgroud)
在java 8中有哪些方便的方式我不知道?
后端,Spring启动项目(v1.3.0.RELEASE),将Rest JSON Api提供给前端,刚才遇到一个错误:
Infinite recursion (StackOverflowError)
Run Code Online (Sandbox Code Playgroud)
我决定更改为自定义FastJsonHttpMessageConverter,代码如下
@Bean
public HttpMessageConverter httpMessageConverter() {
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
return fastJsonHttpMessageConverter;
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用,实际上它使用default HttpMessageConverter
。尽管没有上述错误,但输出却不符合我的预期。例如
suppliers: [
{
$ref: "$.value"
}
]
Run Code Online (Sandbox Code Playgroud)
现在更改上面的代码
@Bean
public HttpMessageConverter mappingJackson2HttpMessageConverter() {
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
return fastJsonHttpMessageConverter;
}
Run Code Online (Sandbox Code Playgroud)
这次有效了,我想知道为什么方法名必须是mappingJackson2HttpMessageConverter
?如果使用其他方法名称怎么配置呢?
有一个内部阶级,例如
public class Foo {
public static void main(String[] args) {
}
@Data
public static class Award {
private final int id;
private final String name;
}
}
Run Code Online (Sandbox Code Playgroud)
我想搬出Award
去Foo
,可以Intellij IDEA
支持吗?我尝试选择代码然后选择Move
菜单Refactor
菜单.但它提示
首先是按年龄划分的boundaries
[0,20,30,40,50,200]
db.user.aggregate(
{$project: {_id:0, age:{$subtract:[{$year:new Date()}, {$year:"$birthDay"}]} } },
{$bucket:{
groupBy:"$age",
boundaries:[0,20,30,40,50,200]
}},
{ $project:{ _id:0,age:"$_id",count:1 } }
)
Run Code Online (Sandbox Code Playgroud)
低于结果
{ "count" : 5, "age" : 20 }
{ "count" : 1, "age" : 30 }
Run Code Online (Sandbox Code Playgroud)
然后我想统计每个城市的每个年龄段计数
{ city : "SH", age: 20, count: 2 }
{ city : "BJ", age: 20, count: 3 }
{ city : "BJ", age: 30, count: 1 }
Run Code Online (Sandbox Code Playgroud)
那么在这种情况下如何实施呢?
此外
db.user.aggregate(
{ $project: {_id:0, city:1, age:{$subtract:[{$year:new Date()}, {$year:"$birthDay"}]} } }, …
Run Code Online (Sandbox Code Playgroud) String str = "?foo";
System.out.println(str.matches("\\s*foo")); //false
System.out.println(Arrays.toString(str.getBytes()));//[-30, -128, -123, 102, 111, 111]
Run Code Online (Sandbox Code Playgroud)
从上面看,第一个字符不是space
.
String replaceStr = str.replaceAll(".*?([a-z]*)", "$1");
System.out.println(replaceStr.equals("foo"));//false
Run Code Online (Sandbox Code Playgroud)
上面的代码不能只获得foo
.
replaceStr = str.replaceAll("^.*?([a-z]*)$", "$1");
System.out.println(replaceStr.equals("foo"));//true
Run Code Online (Sandbox Code Playgroud)
为什么有^
和$
,则只能得到foo
?
我想用来Map<String, List<String>>
记录一些东西,比如每个城市都有多少用户.
现在我的代码是
Map<String, List<String>> map = new HashMap<>();
if(map.get("city_1")==null){
map.put("city_1", new ArrayList<>());
}
map.get("city_1").add("aaa");
Run Code Online (Sandbox Code Playgroud)
但我觉得这有点麻烦,我想要这个效果
Map<String, List<String>> map = new HashMap<>();
map.compute("city_1", (k,v)->v==null?new ArrayList<>():v.add("aaa"));
Run Code Online (Sandbox Code Playgroud)
但它有编译错误:
Type mismatch: cannot convert from boolean to List<String>
Run Code Online (Sandbox Code Playgroud)
那么有任何其他方式可以简化它吗?
我只是在研究Python,发现一些地方甚至不如Java8那么方便,例如字数
起初我认为它可能很容易实现
>>> {x : x**2 for x in range(10)}
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
Run Code Online (Sandbox Code Playgroud)
但实际上我发现它有点麻烦
>>> sent3
['In', 'the', 'beginning', 'God', 'created', 'the', 'heaven', 'and', 'the', 'earth', '.']
>>> for w in sent3:
... if w in word_count:
... word_count[w] += 1
... else:
... word_count[w] = 1
...
Run Code Online (Sandbox Code Playgroud)
但是在Java8中实现它非常方便,
List<String> strings = asList("In", "the", "beginning", "God", "created", "the", "heaven", "and", "the", "earth");
Map<String, …
Run Code Online (Sandbox Code Playgroud) java-8 ×5
java ×4
spring-boot ×2
apache-poi ×1
awk ×1
java-stream ×1
lambda ×1
mongodb ×1
mybatis ×1
mysql ×1
python ×1
reduce ×1
regex ×1
setter ×1
shell ×1
transactions ×1