Scala 2.12在这里,是Scala的新手.我得到一个Array[Double]代表我本地系统上所有逻辑处理器的CPU负载(%):
val logicalProcessorLoads : Array[Double] = cpu.getProcessorCpuLoadBetweenTicks
Run Code Online (Sandbox Code Playgroud)
出于这个问题,逻辑处理器的数量是机器上物理处理器或"核心"数量的2倍.
所以在四核机器上,会有8个逻辑处理器,所以这个logicalProcessorLoads数组在任何时间点的值都可能是这样的:
[12.4,2.2,10.0,5.0,23.7,18.9,1.1,11.2]
因此,要在任何给定时间点获得所有4个内核的CPU负载,需要我遍历这个8项数组并将每个后续元素对添加到一起,因此:
我试图找到最简洁+干净的方式在我的阵列上进行这种成对添加,其中输出为perCoreLoads : Array[Double]4个元素,每个元素代表我的4个核心中的每个核心的负载.到目前为止我最好的尝试:
var perCoreLoads : Array[Double] = Array()
for((lpl,i) <- logicalProcessorLoads.zipWithIndex) {
if(i+1 <= logicalProcessorLoads.size - 1) {
perCoreLoads :+ (lpl(i) + lpl(i+1))
}
}
Run Code Online (Sandbox Code Playgroud)
但这看起来有点令人费解,我相信Scala有一些魔力可以帮助我.有任何想法吗?
新来咖啡因,我清楚地失去了一些东西根本超强。我看到的所有基本用法示例如下所示:
LoadingCache<String,Fizzbuzz> fizzbuzzes = Caffeine.newBuilder()
.maximumSize(100)
.expireAfterWrite(10, TimeUnit.DAYS)
.build(k -> fetchFizzbuzzes());
Run Code Online (Sandbox Code Playgroud)
我正在苦苦挣扎的是fetchFizzbuzzes()函数所扮演的角色:
Java 8在这里.我有以下代码:
final String createdDateStr = "20110920";
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYYMMdd");
final LocalDate localDate = LocalDate.parse(createdDateStr, formatter);
Run Code Online (Sandbox Code Playgroud)
在运行时,我得到以下异常:
java.time.format.DateTimeParseException: Text '20110920' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.
Run Code Online (Sandbox Code Playgroud)
......被LocalDate.parse(...)调用抛出 解析器出了什么问题?!
请注意:我首先看了一下这个问题,希望它能回答我的问题,但我认为我的略有不同!
有人告诉我,在git remote update origin --prune本地运行会强制我的本地存储库与原始存储库具有完全相同的分支。但是,我可以找到大量有关使用 的信息/文档git remote prune origin,但找不到git remote update origin --prune...
我对这个命令的理解正确吗?如果不是,我怎么被误导了?如果我是对的,那么如果我有尚未远程推送的本地功能分支会发生什么?如果我有一个变化的分支,会发生什么做远程现有/原点,但有我没有推开这些变化了吗?
提前致谢!
我有以下Java POJO:
public class Order {
private List<OrderLine> orderLines;
private String name;
// ... lots of other fields
// Getters & setters & ctors down here
}
public class OrderLine {
private String productId;
private Integer quantity;
// Getters & setters & ctors down here
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试找到一种Java 8“ Streamy”方式来获取订单中所有订单行的总数。获取该计数的“旧”方法如下所示:
int totalQuantity = 0;
for (OrderLine ol : order.getOrderLines()) {
totalQuantity += ol.getQuantity();
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我最大的尝试是:
Integer totalQuantity = order.getOrderLines().stream().filter(ol -> ol.getQuantity());
Run Code Online (Sandbox Code Playgroud)
我知道这是错误的,因为它没有迭代List<OrderLine>和求和每行的数量,并且它不编译,因为filter(...)需要将表达式解析为a boolean而不是int值。
有什么想法我要去哪里吗?
我正在尝试使用Java 8 Stream API mapToDouble方法,如下所示:
BigDecimal totalCost = order.getOrderlineList().stream()
.mapToDouble(Orderline::getPrice)
.sum();
Run Code Online (Sandbox Code Playgroud)
问题是Orderline::getPrice返回a BigDecimal而不是a Double。因此,以上尝试无法编译(方法参考中的错误返回类型:无法将java.math.BigDecimal转换为doubele)。
看到那Orderline#price是a BigDecimal,我如何使用Stream API(mapToDouble或类似的东西)来获取我的totalCost?
我有以下Java 8 Swing代码:
JButton button = new JButton("Browse");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Choose file as input");
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel Filter", "xls", "xlsx");
fileChooser.setFileFilter(filter);
if (fileChooser.showOpenDialog(mainWindow) == JFileChooser.APPROVE_OPTION) {
File selection = fileChooser.getSelectedFile();
createFile(selection);
}
}
});
Run Code Online (Sandbox Code Playgroud)
这个想法是,用户选择一个目录,然后键入应用程序将随后创建的新文件的名称。但是,当我单击按钮时,这就是我看到的:
请注意,在没有“ File Name ”文本字段的地方可以输入新文件名吗?我需要更改哪些配置才能获得此功能?
Java 8 在这里。我有以下 POJO:
public enum VehicleType {
Car,
Motorcycle,
Scooter,
Skateboard
}
@Getter
@Setter
public class Driver {
private String uuid;
private Long numYearsDriving;
private VehicleType vehicleType;
}
Run Code Online (Sandbox Code Playgroud)
现在我有以下代码,它接受 aList<Driver>并将它们组织到 a 上Map<VehicleType,List<Driver>,其中键是我们拥有的不同类型的车辆,每个值都是Drivers这些车辆类型的所有“子列表”(原始列表的):
List<Driver> allDrivers = getSomehow();
Map<VehicleType,List<Driver>> driversByVehicleType = allDrivers.stream()
.collect(Collectors.groupingBy(Driver::getVehicleType));
Run Code Online (Sandbox Code Playgroud)
到目前为止,很好,我认为。现在我想检查该地图并获取踏板车或滑板的所有驱动程序。我最好的尝试:
List<Driver> scooterSkateboarders = Stream
.concat(
driversByVehicleType.get(VehicleType.Scooter).stream(),
driversByVehicleType.get(VehicleType.Skateboard).stream())
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
...编译,但是,它会抛出一个NullPointerExceptionif 开始时allDrivers 不包含任何滑板车/滑板驱动程序。所以我问:如果allDrivers只包含Car驱动程序,如何使我的代码“空安全” ?
我有一个使用ubuntu:21.04. 当我通过 SSH 连接到它时,我尝试使用官方 Git Ubuntu 安装说明来安装 git ,但出现错误:
root@c812b171354a:/home/ubuntu# sudo apt install git-all
bash: sudo: command not found
Run Code Online (Sandbox Code Playgroud)
我很惊讶地发现sudo不存在!所以我尝试不使用sudo:
root@c812b171354a:/home/ubuntu# apt install git-all
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package git-all
Run Code Online (Sandbox Code Playgroud)
所以我问:我可以运行什么命令来git在 Dockerized Ubuntu 21.04 容器上安装?
这是我的完整 Dockerfile:
FROM ubuntu:21.04
COPY keep-alive.sh /home/ubuntu/keep-alive.sh
# give keep-alive script permission to run
RUN ["chmod", "+x", "/home/ubuntu/keep-alive.sh"]
# install git …Run Code Online (Sandbox Code Playgroud) 我正在将 Maven 构建的 Java 8 应用程序升级到 Java 11。在我的 POM 中,我指定:
<properties>
<java.version>1.11</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
Run Code Online (Sandbox Code Playgroud)
当我使用正常的 Maven 构建调用来构建我的应用程序时:
mvn verify -Plocal -Dspring-boot.run.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
Run Code Online (Sandbox Code Playgroud)
我得到:
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< example.com:myapp-svc >------------------------
[INFO] Building myapp-svc 1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-checkstyle-plugin:3.1.2:check (validate) @ myapp-svc ---
[WARNING] Old version of checkstyle detected. Consider updating to >= v8.30
[WARNING] For more information see: https://maven.apache.org/plugins/maven-checkstyle-plugin/examples/upgrading-checkstyle.html
[INFO] Starting audit...
Audit done.
[INFO] You have 0 Checkstyle violations.
[INFO]
[INFO] --- …Run Code Online (Sandbox Code Playgroud) java ×7
java-8 ×5
java-stream ×3
git ×2
apt ×1
bigdecimal ×1
caffeine ×1
datetime ×1
docker ×1
java-11 ×1
jfilechooser ×1
localdate ×1
maven ×1
maven-plugin ×1
scala ×1
sudo ×1
sum ×1
swing ×1
ubuntu ×1