我该如何合并List<Map<String,String>>到Map<String,String>使用flatMap?
这是我尝试过的:
final Map<String, String> result = response
.stream()
.collect(Collectors.toMap(
s -> (String) s.get("key"),
s -> (String) s.get("value")));
result
.entrySet()
.forEach(e -> System.out.println(e.getKey() + " -> " + e.getValue()));
Run Code Online (Sandbox Code Playgroud)
这不起作用.
我有一个场景,我在调用hashmap值中的函数,如下所示,
Map<Character, IntSupplier> commands = new HashMap<>();
// Populate commands map
int number=10;
commands.put('h', () -> funtion1(number) );
commands.put('t', () -> funtion1(number) );
// Invoke some command
char cmd = 'h';
IntSupplier result= commands.get(cmd); //How can I pass a parameter over here?
System.out.println(" Return value is "+result.getAsInt());
Run Code Online (Sandbox Code Playgroud)
我的问题是,我可以在获取hashmap值时将参数传递给函数(function1),即使用commands.get(cmd)时.
谢谢.
我想使用Predicate函数而不是&运算符.这意味着我想要"第二次测试通过!!" 在执行我的代码后打印.
我该怎么做 ?
public class BitwiseInclusiveAND{
public static void main(String[] args) {
final Predicate<String> condition1 = (Predicate<String>) (arg -> arg != null);
final Predicate<String> condition2 = (Predicate<String>) (arg -> {
System.out.println("Second Test is passed !!");
return arg.equals("Hello");
});
Predicate<String> equalsStrings
= condition1.and(condition2); // Here I want to execute the condition2 even if condition 1 = true
System.out.println(equalsStrings.test("Hello"));
}
}
Run Code Online (Sandbox Code Playgroud) 我有3个班:
class Class1 {}
class Class2 extends Class1 {}
class Class3 extends Class1 {}
Run Code Online (Sandbox Code Playgroud)
我试图改变包含Class1这样的数组的类型:
Class1[] arr = ... // [Class2, Class2, Class3, Class3]
arr = Arrays.copyOf(arr,2)
Class2[] arr2 = (Class2[]) arr
Run Code Online (Sandbox Code Playgroud)
这似乎是不可能的,但我能做到
Class2[] arr2 = new Class2[arr.length]
for ...
arr2[i] = (Class2) arr[i]
Run Code Online (Sandbox Code Playgroud)
有没有办法在没有迭代的情况下做到这一点?
我们可以假设arr(之后copyOf)中的所有元素都可以输入Class2.
我得到的错误是:
CassCastException:[LStuff.Class1; 无法转换为[LStuff.Class2;
我有一个CarPOJO:
class Car
{
String make;
int year;
BigDecimal amount;
}
Run Code Online (Sandbox Code Playgroud)
和那样List<Car>的汽车:
make : year : price
honda : 2011 : $20,000
honda : 2011 : $30,000
honda : 2012 : $50,000
ford : 2012 : $12,000
Run Code Online (Sandbox Code Playgroud)
结果我想要的地方:
// Map<make, List<year, totalForYear>>
Map<String, Map<int, BigDecimal>> revenuesByMakeAndYear;
Run Code Online (Sandbox Code Playgroud)
如何使用Streams实现这一目标?到目前为止,我已经到了一半:
// Map<make, List<car>>
Map<String, List<Car>> carsByMake = cars.stream()
.collect(Collectors.toMap(
car -> car.getMake(),
car -> cars.stream()
.filter(subCar -> subCar.getMake().equals(car.getMake())
.collect(Collectors.toList())));
Run Code Online (Sandbox Code Playgroud)
但是我该如何进入下一步呢?
我有两个类A和B,就像这样:
class A {
public Integer fetchMax() {
// Make a network call & return result
}
}
class B {
public Double fetchPercentile(Integer input) {
// Make a network call & return result
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我需要提供retry这两种方法的机理fetchMax()及fetchPercentile(Integer).我想使用一个helper类来提供这种行为,其中retry可以采用instance(A或B)的方法,method-name和method-arguments.重试基本上会对提供的对象方法进行重新尝试.
像这样的东西:
class Retry {
public static R retry(T obj, Function<T, R> method, Object... arguments) {
// Retry logic
while(/* retry condition */)
{ …Run Code Online (Sandbox Code Playgroud) 我有一个我希望用户可以选择的时区列表.所以,我以为我可以调用java.time.ZoneId.getAvailableZoneIds()并使用getDisplayName它们的方法.这导致了许多重复的条目,如
中欧时间
即使我添加了时区偏移,它们也不是不可取的.但是,a的ID ZoneId区分条目但我如何本地化它们?ID始终为英文
欧洲/罗马
double average = LongStream
.of(-4480186093928204294L, -1340542863544260591L, -6004296286240039273L)
.average()
.getAsDouble()
Run Code Online (Sandbox Code Playgroud)
这会返回,2.20723960999901594E18但我希望它会返回-3.9416750812375014E18.
另一方面,这会返回正确的结果(-1.4628605853333333E9):
double average = IntStream
.of(-1282256274, -1645263673, -1461061809)
.average()
.getAsDouble()
Run Code Online (Sandbox Code Playgroud)
为什么IntStream.average()总是返回正确的平均值,LongStream.average()有时却没有?
我试图将一个字节数组转换为LinkedList<Byte>:
//data is of type byte[]
List<Byte> list = Arrays.stream(toObjects(data)).boxed().collect(Collectors.toList());
Byte[] toObjects(byte[] bytesPrim) {
Byte[] bytes = new Byte[bytesPrim.length];
Arrays.setAll(bytes, n -> bytesPrim[n]);
return bytes;
}
Run Code Online (Sandbox Code Playgroud)
第一行引发一条错误消息:
该
boxed()类型的方法未定义Stream<Byte>.
知道为什么我会收到此错误消息以及如何规避这个问题吗?
假设我有一份月度商店销售清单,以便:
ID, date, amount
1, Jan 2016, $500
2, Feb 2017, $200
3, October 2017, $300
Run Code Online (Sandbox Code Playgroud)
我的财政年度是2016年9月1日到2017年8月31日.换句话说,前两个条目是在一个会计年度,另一个是在第二个会计年度.实际会计年度是特定于用户的.
我想storeSales为每个会计年度创建一个列表.
到目前为止,我有:
Map<fiscalYearID, List<StoreSale> = storeSales.stream()
.collect(
Collectors.groupingBy(
storeSale -> storeSale.getSaleLocalDate()));
Run Code Online (Sandbox Code Playgroud)
问题是上面的代码只会按确切日期分组.我可以添加,.getYear()但只能按年份而不是财政年度.我还考虑过使用Stream.iterate()每个会计年度日期范围,然后浏览该日期范围内所有商店的商店销售列表到列表中,但这会非常难看,大O值开始迅速攀升.这将是这样的:
Stream.iterate(startDate, date -> date.plusMonths(1))
.limit(ChronoUnit.MONTHS.between(startDate, endDate.plusMonths(1)))
.forEach(
{
addToFiscalYear(fiscalYearID,
storeSales.stream().
.filter(storeSale -> storeSale.getSaleLocalDate().equals(date))
.collect(Collectors.toList());
});
Run Code Online (Sandbox Code Playgroud)
或类似的东西.我没有太多探索它,因为它的大O快速增加,我希望有更好的解决方案......