我想做这样的事情:
ArrayList<String> strings = new ArrayList<>();
//build a collection of Strings
callMethod(strings);
Run Code Online (Sandbox Code Playgroud)
“callMethod”是遗留的(在遗留项目中),所以我无法更改它。
它的签名是这样的:
private void callMethod(String... input){
// and so forth...
Run Code Online (Sandbox Code Playgroud)
如何将字符串集合传递给 callMethod 方法?
从长远来看,下面的代码片段总是会导致内存不足错误,尤其是在读取非常庞大的文件/内容时。
有没有另一种方法可以重写它,特别是使用流?
我在这里看到了一种将字节数组转换为十六进制字符串的方法:Effective way to get hex string from a byte array using lambdas and Streams
public static byte[] hexStringToBytes(String hexString) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Hex string to convert to byte[] " + hexString);
}
byte[] buf = new byte[hexString.length() / 2];
String twoDigitHexToConvertToByte;
for (int i = 0; i < buf.length; i++) {
twoDigitHexToConvertToByte = extractPairFromStringBasedOnIndex(hexString, i);
parseStringToBytesAndStoreInArrayOnIndex(twoDigitHexToConvertToByte, buf, i);
}
return buf;
}
private static void parseStringToBytesAndStoreInArrayOnIndex(String twoDigitHexToConvertToByte, byte[] buf, int i) {
try {
buf[i] = …Run Code Online (Sandbox Code Playgroud) 观察者和可观察类:
可观察到:-
class NewsAgency extends Observable {
String news;
public void setNews(String news) {
this.news = news;
setChanged();
notifyObservers(news);
}
}
Run Code Online (Sandbox Code Playgroud)
观察员:-
public class IndiaNews implements Observer {
@Override
public void update(Observable o, Object news) {
System.out.println("Indianews: " +news.toString());
}
}
class AjjTak implements Observer {
@Override
public void update(Observable o, Object news) {
System.out.println("AjjTak: " +news.toString());
}
Run Code Online (Sandbox Code Playgroud)
主要类别:-
public class Main {
public static void main(String[] args) {
NewsAgency newsAgency = new NewsAgency();
IndiaNews …Run Code Online (Sandbox Code Playgroud) 我想将 a 转换Map<String,List<String>>为Map<String,Set<String>>优化搜索。我想出了下面的传统方法。
for (Map.Entry<String, List<String>> entry : this.mapWithList.entrySet()) {
Set<String> hSet = new HashSet<>(entry.getValue());
this.mapWithSet.put(entry.getKey(), hSet);
}
Run Code Online (Sandbox Code Playgroud)
forEach我想知道如何在Java 8中使用它。
另外,使用forEachlambda 后,代码性能会更好吗?
在java 17中,AtomicReference有compareAndExchange类似于 的方法compareAndSet,但它不返回布尔值,而是返回原子操作之前的值。我需要它来实现自定义并发结构。
由于项目的限制,我只能使用 Java 8 功能。一些挖掘揭示了VarHandle其中有compareAndExchange。但是,VarHandle需要 Java 9。
因此,看来我必须compareAndExchange亲自实施了。但如何利用现有方法高效地做到这一点呢?(那么compareAndExchangeWeak版本呢?)
(顺便说一句,我不能依赖任何第三方库)
我试图从模型类中获取两个字段的总和。并使用 pojo 返回它,但不断出现语法错误。我想要实现的目标类似于此中得票最高的答案:使用流 API 对对象列表中的多个不同字段求和?但我遇到语法错误。这是我的模型:
public class BranchAccount {
@NotNull(message = "Account balance is required")
private Double accountBalance;
@NotNull(message = "Profit is required")
private Double profit;
@Temporal(TemporalType.TIMESTAMP)
private Date dateCreated;
@Temporal(TemporalType.TIMESTAMP)
private Date lastUpdated;
}
Run Code Online (Sandbox Code Playgroud)
我的波乔:
public class ProfitBalanceDto {
private Double accountBalance;
private Double profit;
}
Run Code Online (Sandbox Code Playgroud)
我从 BranchAccount 获取 accountBalance 和利润总和的代码:
public ProfitBalanceDto getAllBranchAccount() {
List<BranchAccount> branchAccounts = branchAccountRepository.findAll();
branchAccounts.stream()
.reduce(new ProfitBalanceDto(0.0, 0.0), (branchAccount1, branchAccount2) -> {
return new ProfitBalanceDto(
branchAccount1.getAccountBalance() + branchAccount2.getAccountBalance(),
branchAccount1.getProfit() + branchAccount2.getProfit());
}); …Run Code Online (Sandbox Code Playgroud) 我有一个 Map<Integer、Map<String、List<String >>。我想将其展平为 Map<String, String> ,其中键是内部映射的键,值是使用 java 8 的内部映射列表的第一个元素。
这是我到目前为止所拥有的
public static void main(String[] args) {
Map<Integer, Map<String, List<String>>> ip = new HashMap<>();
Map<String, List<String>> iip1 = new HashMap<>();
List<String> ilp1 = new ArrayList<>();
ilp1.add("Uno");
ilp1.add("Dos");
ilp1.add("Tres");
iip1.put("Alpha", ilp1);
Map<String, List<String>> iip2 = new HashMap<>();
List<String> ilp2 = new ArrayList<>();
ilp2.add("One");
ilp2.add("Two");
ilp2.add("Three");
iip2.put("Beta", ilp2);
Map<String, List<String>> iip3 = new HashMap<>();
List<String> ilp3 = new ArrayList<>();
ilp3.add("Eins");
ilp3.add("Zwei");
ilp3.add("Drei");
iip3.put("Gamma", ilp3);
ip.put(1, iip1);
ip.put(2, iip2);
ip.put(3, iip3);
Map<String, String> op …Run Code Online (Sandbox Code Playgroud) 我在Jenkins JDK中定义->自动安装->选择JDK版本->我同意
但是当我运行构建时,JDK 安装失败并出现此错误
[jdk] $ /var/jenkins_home/tools/hudson.model.JDK/jdk/jdk.sh -noregister
/var/jenkins_home/tools/hudson.model.JDK/jdk/jdk.sh: 2: Syntax error: newline unexpected
Can you please help?
Run Code Online (Sandbox Code Playgroud)
我尝试使用 JDK+maven 运行构建,我希望看到 jdk 1.9 正在安装运行谢谢
我创建了一个 for every 循环,并获得了价格代码列表,但我想在不使用任何循环的情况下获得相同的东西,并使用 java8 执行此操作供您参考,我发布了我的旧代码。我只想更改代码的这个位置。
List<ItemPriceCode> itemPriceCodes = item.getItemPriceCodes();
List<String> priceCodeList = new ArrayList<String>();
for (ItemPriceCode ipc : itemPriceCodes) {
//get the string value from the list
priceCodeList.add(ipc.getPriceCode());
}
Run Code Online (Sandbox Code Playgroud)
但我不想使用任何循环我想在不使用任何循环的情况下获得相同的结果。为了解决这个问题,我尝试了这种方法,但没有取得任何成功。
itemPriceCodes.stream().map(n -> String.valueOf(n)).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
这是我的这个函数的完整代码
private Item getItemManufacturerPriceCodes(Item item) {
List<ItemPriceCode> itemPriceCodes = item.getItemPriceCodes();
List<String> priceCodeList = new ArrayList<String>();
for (ItemPriceCode ipc : itemPriceCodes) {
//get the string value from the list
priceCodeList.add(ipc.getPriceCode());
}
//pass this string value in query
List<ManufacturerPriceCodes>mpc = manufacturerPriceCodesRepository.
findByManufacturerIDAndPriceCodeInAndRecordDeleted(item.getManufacturerID(),priceCodeList,NOT_DELETED);
//Convert list to map
Map<String, ManufacturerPriceCodes> …Run Code Online (Sandbox Code Playgroud) 在下面的代码中,为什么x和y分别输出0和1?为什么当我把这段代码\xef\xbc\x9aprivate static Singleton instance = new Singleton();从位置\xe2\x91\xa0放到位置\xe2\x91\xa1时,输出是1, 1?
private static Singleton instance = new Singleton();\n//\xe2\x91\xa0\nprivate static int x = 0;\n\nprivate static int y;\n//\xe2\x91\xa1\nprivate Singleton()\n{\n System.out.println("Starting add");\n x++;\n y++;\n}\n\npublic static Singleton getInstance()\n{\n return instance;\n}\n\npublic static void main(String[] args)\n{\n System.out.println("Starting Singleton");\n Singleton singleton = Singleton.getInstance();\n //Singleton singleton = new Singleton();\n System.out.println(singleton.x);\n System.out.println(singleton.y);\n}\nRun Code Online (Sandbox Code Playgroud)\n java-8 ×10
java ×8
java-stream ×5
hashmap ×2
lambda ×2
atomic ×1
build ×1
collections ×1
hex ×1
java-9 ×1
jenkins ×1
maven ×1
singleton ×1
spring-boot ×1
stream ×1