我正在阅读"Java 8 Lambdas"这本书,并且在某些时候作者说:"由于性能优势,尽可能使用原始专用函数是个好主意."
他在这里指的是mapToInt,mapToLong等.
事情是我不知道性能来自哪里说实话.
我们来看一个例子:
// Consider this a very very long list, with a lot of elements
List<Integer> list = Arrays.asList(1, 2, 3, 4);
//sum it, flavour 1
int sum1 = list.stream().reduce(0, (acc, e) -> acc + e).intValue();
//sum it, flavour 2
int sum2 = list.stream().mapToInt(e -> e).sum();
System.out.println(sum1 + " " + sum2);
Run Code Online (Sandbox Code Playgroud)
因此,在第一种情况下,我使用reduce来对值进行求和,因此BinaryOperator函数将始终接收int(acc)和Integer(集合的当前元素),然后将对整数进行取消装箱int(acc + e)
在第二种情况下,我使用mapToInt,它将每个Integer解包成一个int,然后对它求和.
我的问题是,第二种方法有什么优势吗?当我可以使用地图时,对于int的地图有什么意义呢?
所以是的,这只是糖语法还是有一些性能优势?如果有,请提供一些信息
问候,
给定一个java.util.List
与n
元素和所需的页面大小m
,我想将它转换为包含地图n/m+n%m
的元素.每个地图元素都应包含m
元素.
这是一个整数的例子:
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// What is the equivalent Java 8 code to create the map below from my list?
Map<Integer, List<Integer>> map = new HashMap<>();
map.put(0, Arrays.asList(1,2,3));
map.put(1, Arrays.asList(4,5,6));
map.put(2, Arrays.asList(7,8,9));
map.put(3, Arrays.asList(10));
Run Code Online (Sandbox Code Playgroud)
这是可能的,使用Java 8?
我正在对kerberos认证的REST服务执行https请求.如果我使用keytab,一切都很好.但是,我要求我应该使用kerberos票证缓存文件,该文件是在使用其密码登录工作站时创建的.
我将用MY_DOMAINE.COM替换域名
所以,klist显示:
Ticket cache: FILE:/tmp/krb5cc_210007
Default principal: dragomira@MY_DOMAINE.COM
Valid starting Expires Service principal
05/15/18 07:21:51 05/15/18 17:21:51 krbtgt/MY_DOMAINE.COM@MY_DOMAINE.COM
renew until 05/22/18 06:18:22
Run Code Online (Sandbox Code Playgroud)
像这样使用卷曲可以正常工作:
curl -k --negotiate -u : 'my_url' -v
Run Code Online (Sandbox Code Playgroud)
现在,让我们回到代码.我的login.conf是这样的:
com.sun.security.jgss.login {
com.sun.security.auth.module.Krb5LoginModule required
client=TRUE
doNotPrompt=true
useTicketCache=true;
};
com.sun.security.jgss.initiate {
com.sun.security.auth.module.Krb5LoginModule required
client=TRUE
doNotPrompt=true
useTicketCache=true;
};
com.sun.security.jgss.accept {
com.sun.security.auth.module.Krb5LoginModule required
client=TRUE
doNotPrompt=true
useTicketCache=true;
};
Run Code Online (Sandbox Code Playgroud)
我的http客户端的相关java代码是针对kerberos的,它是:
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (chain, authType) -> true).build();
HostnameVerifier hostnameVerifier = new NoopHostnameVerifier();
Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()
.register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory()) …
Run Code Online (Sandbox Code Playgroud) java kerberos apache-httpcomponents apache-httpclient-4.x apache-httpclient-5.x
我在Student
和之间有一个 one2one 关系Address
。我希望firstName
和lastName
字段Student
被延迟加载。我也想懒惰的address
领域。
这些是我的实体类:
@Entity
@Table(name = "students")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne(mappedBy = "student", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@LazyToOne(LazyToOneOption.NO_PROXY)
private Address address;
@Basic(fetch = FetchType.LAZY)
@Column(name = "first_name")
private String firstName;
@Basic(fetch = FetchType.LAZY)
@Column(name = "last_name")
private String lastName;
// getters and setters
}
Run Code Online (Sandbox Code Playgroud)
地址类:
@Entity
@Table(name = "addresses")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) …
Run Code Online (Sandbox Code Playgroud) 考虑以下玩具示例类:
public class Test {
private volatile Outer outerVar = new Outer();
static class Outer {
Inner innerVar = new Inner();
}
static class Inner {
// state
// setters
// getters
}
private void multithreadedUse() {
// play with outerVar.innerVar
}
}
Run Code Online (Sandbox Code Playgroud)
outerVar是易失性的,因此可能正在使用它的所有线程都会看到它处于相同的状态.但是outerVar.innerVar怎么样?它的父对象(outerVar)被标记为volatile的事实是否也使它变得不稳定?
或者我们必须显式声明innerVar volatile吗?
我有一个关于使用单线程执行器的问题。由于它重用同一个线程,这是否意味着如果我在一次提交调用中修改了一个对象状态,我是否可以假设在随后的提交调用中对该对象状态的另一次修改是线程安全的?让我举一个玩具的例子......
public class Main {
public static void main(String[] args) throws Exception {
final A a = new Main().new A();
ExecutorService executor = Executors.newSingleThreadExecutor();
Callable<Integer> callable = new Callable<Integer>() {
@Override
public Integer call() throws Exception {
return a.modifyState();
}
};
/* first call */
Future<Integer> result = executor.submit(callable);
result.get();
/* second call */
result = executor.submit(callable);
int fin = result.get();
System.out.println(fin);
executor.shutdown();
}
class A {
private int state;
public int modifyState() {
return ++state;
}
public int getState() …
Run Code Online (Sandbox Code Playgroud) 我是python3初学者。我正在尝试使用 python3 脚本获取 java 版本。检查文档后,我看到 subprocess.check_output 可能是我需要的。
output = subprocess.check_output(["java", "-version"])
print("Output is {}".format(output))
Run Code Online (Sandbox Code Playgroud)
问题是我得到的输出是
Output is b''
Run Code Online (Sandbox Code Playgroud)
为什么我没有得到我用 bash 得到的正确字符串?
谢谢
我试图用gradle中的参数运行可执行文件:
task deploy(dependsOn: jar) {
exec {
commandLine "javafxpackager -deploy -native -outdir ${deployDirName} -outfile ${jarBaseName} -srcfiles ./${project.buildDir}/${project.libsDirName}/${jarBaseName}-${project.version}.jar -appclass ${mainClass} -name ${jarBaseName} -title '${project.description}'"
}
}
Run Code Online (Sandbox Code Playgroud)
Gradle抱怨该进程以非零返回代码结束,但是如果我复制命令并在bash终端中运行它,它可以完美地工作.
那么我做错了什么?
问候,
请考虑从jacoco示例中获取此pom摘录(http://www.eclemma.org/jacoco/trunk/doc/examples/build/pom-it.xml)
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5-SNAPSHOT</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-prepare-agent-integration</id>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>default-report-integration</id>
<goals>
<goal>report-integration</goal>
</goals>
</execution>
<execution>
<id>default-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<!-- implmentation is needed only for Maven 2 -->
<rule implementation="org.jacoco.maven.RuleConfiguration">
<element>BUNDLE</element>
<limits>
<!-- implmentation is needed only for Maven 2 -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>COMPLEXITY</counter>
<value>COVEREDRATIO</value>
<minimum>0.60</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
</plugin> …
Run Code Online (Sandbox Code Playgroud) **1.服务用法:当你看到一个hibernate spring教程时,他们都说对于一个实体(例如我的用户)你必须有一个名为UserRepository的存储库,其中包含find,findAll,delete等方法.通常,UserRepository扩展了一些基础知识库接口.
然后你必须添加UserService,它注入一个UserRepository.
a.我必须有一个UserService接口由UserServiceImpl实现吗?从我的角度来看,它没有增加任何价值.我可以让UserService成为一个类,并使用Spring的能力使用GCLIB而不是JDKInterfaces创建代理.
b.通过从UserRepository复制每个方法并委托给@Autowired存储库,然后添加任何其他业务方法来编写UserService是否正确?
c.如果我的UserService没有任何业务方法,它只是将所有内容委托给UserRepository,我可以跳过UserService并直接从我的REST层访问UserRepoisitory吗?
d.假设我也有一个地址实体.保存时用户需要一个地址(强制要求为one2).从UserService可以在其中注入UserRepository和AddressRepository,在那里设置关系然后在每个存储库上调用save吗?(不想使用级联持久,我想知道如果没有它我将如何做,因为在某些情况下你不能使用级联)
2. DTO用法:当您想要读取数据时,您可以通过JPQL(或Criteria,我更喜欢JPQL)直接获取实体或获取DTO.
a.从我的角度来看,我总是使用DTO而不是实体获取.这是因为,我认为SQL很简单,如果我不得不考虑实体合并,分离,附加等,我会错过SQL的简单性,ORM在性能和复杂性方面成为敌人.因此,当我修改数据时,我总是在读取数据和实体时使用DTO.你怎么看?
b.我想从User实体返回所有列.是否可以使用UserDTo或我夸大其词并且应该返回用户实体?我50% - 50%.
c.我想从User返回部分列.在这里,我是75%DTO和25%实体.你怎么看?
d.我想返回一些User列和一些Address列.在这里,我100%为DTO(虽然我可以加入获取地址).你怎么看?
亲切的问候,
我有一个只有一个活动的应用程序.在onCreate中我显示一个通知,点击我显示我的活动,必要时重复使用它(或者我认为)代码是:
package com.example.multiplestartupifusingpendingintent;
import android.app.Activity;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
public static final String TAG = "hexor";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG,
"onCreate " + getClass().getSimpleName()
+ Integer.toHexString(hashCode()));
NotificationManager notifMgr = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
showReuseNotification(notifMgr);
}
private void showReuseNotification(NotificationManager notifMgr) {
Intent reuseIntent = new Intent(this, MainActivity.class);
reuseIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent reusePendingIntent = PendingIntent.getActivity(this, 2,
reuseIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder builder = …
Run Code Online (Sandbox Code Playgroud) java ×6
hibernate ×2
java-8 ×2
android ×1
dto ×1
exec ×1
gradle ×1
java-stream ×1
kerberos ×1
lambda ×1
lazy-loading ×1
list ×1
maven ×1
partition ×1
plugins ×1
python-3.x ×1
repository ×1
service ×1
subprocess ×1
volatile ×1