我收到了格式为“yyyy-MM-dd'T'HH:mm:ssXXX”的字符串
例如“2020-06-01T11:04:02+02:00”
我想把它转换成“yyyy-MM-dd'T'HH:mm:ss:SSSZ”
例如“2020-06-01T11:04:02.000+0200”
我实际上不知道时区。它应该取自字符串的最后一部分。
我已经尝试过,但是当我将字符串转换为日期(即 IST)时,它占用了我的本地时间和时区。
SimpleDateFormat sd1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
//sd1.setTimeZone(TimeZone.getTimeZone("PST"));
Date dt = sd1.parse("2020-06-01T11:04:02+02:00");
SimpleDateFormat sd2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss:SSSZ");
System.out.println(sd2.format(dt));
Run Code Online (Sandbox Code Playgroud)
输出:
2020-06-01T14:34:02:000+0530
Run Code Online (Sandbox Code Playgroud)
只有日期是正确的,时间和时区已更改。
我知道我做错了,如果有人能告诉我我该怎么做,这将非常有帮助。谢谢您的帮助。
我有一个功能可以按 3 个整数字段对 pojo 列表进行排序。
目前我正在使用comparingInt()和thenComparingBy()。
但我可能会在两者之间得到一个空值。这会抛出一个NullPointerException.
为此,我想在末尾添加空值。Comparator.nullsLast()不适用于我的情况,因为我正在比较 3 个整数值。
有没有办法实现这一点......
为什么这段代码不能为我编译?
我尝试使用stream和toMap选项将List转换为Map
List<CountryToPaymentMethodsDisplayRules>
paymentMethodDisplayRulesByCountryList =
gatway.getCountryPaymentMethodsDisplayRulesByCountry();
Map<PaymentMethod, CountryToPaymentMethodsDisplayRules>
countryToPaymentMethodsDisplayRulesMap = paymentMethodDisplayRulesByCountryList
.stream()
.collect(Collectors.toMap(type -> type.getCountryToPaymentMethodsDisplayRules().getPaymentMethod(),
type -> type));
public interface PaymentMethod extends Serializable {
}
public enum PaymentMethodType implements PaymentMethod, Serializable {
}
public interface CountryToPaymentMethodsDisplayRules {
public PaymentMethod getPaymentMethod();
}
public class CountryToPaymentMethodsDisplayRulesEntity implements CountryToPaymentMethodsDisplayRules, PersistentEntity<Long>, Serializable {
@Type(type = "com.plimus.core.payment.PaymentMethodTypeUserType")
@Column(name = "PAYMENT_TYPE")
private PaymentMethod paymentMethod;
}
Run Code Online (Sandbox Code Playgroud)
这有什么不对?
我有一个数组String[],我想转换为数组Float[]
考虑e是String[]提供的通道HttpServletRequest::getParameterMap().我试过了:
Arrays.stream(e.getValue()).mapToDouble(Float::parseFloat).boxed().toArray(Float[]::new));
Run Code Online (Sandbox Code Playgroud)
得到例外:
java.lang.ArrayStoreException:java.lang.Double
那么我试过:
Arrays.stream(e.getValue()).mapToDouble(Double::parseDouble).boxed().toArray(Float[]::new));
Run Code Online (Sandbox Code Playgroud)
结果相同.
我的 groupingBy 方法有问题。我举个例子:假设有一个List<Person>wherePerson.class和City.classare like this
class Person{
String name;
String surname;
City city;
}
class City{
String name;
String postalCode;
}
Run Code Online (Sandbox Code Playgroud)
我会将列表分组city.postalcode并执行如下操作:
List<Person> myList = ...some persons here...
Map<City,List<Person>> groupMap = myList.stream().collect(
Collectors.groupingBy(person -> person));
Run Code Online (Sandbox Code Playgroud)
显然这不起作用,因为groupingBy不知道如何按对象分组。我会做类似的事情Collectors.groupingBy(person -> person.city.postalcode),但是这样会产生一个类型的地图Map<String,List<Person>>,而不是 Map<City,List<Person>>
谢谢你的帮助。
更新
为了groupingBy在类上使用,它必须简单地正确实现 equals并且hashcode
我想从列表中删除一个项目,如果它不是集合中的最后一个.
例如:
List<String> items = ....
Iterator<String> it = items.iterator();
while (it.hasNext() && items.size() > 1) {
String item = it.next();
if (condition(item)) {
it.remove();
}
}
Run Code Online (Sandbox Code Playgroud)
是否可以通过在Java 8中使用流以更优雅的方式执行相同的操作?
给出一个对象列表
// ["A", ["B", null, "C"], "D", ["E"], [], null, "F"];
List<Object> objects = Arrays.asList("A", Arrays.asList("B", null, "C"), "D", Arrays.asList("E"), Arrays.asList(), null, "F");
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法可以将非空值组合到一个列表中["A", "B", "C", "D", "E", "F"]?
有些人坚持认为帖子必须显示尝试的内容
objects.stream().map(o -> {
if (o instanceof List) {
return ((List) o).stream();
} else return objects.stream();
}).filter(Objects::nonNull).collect(Collectors.toList()));
Run Code Online (Sandbox Code Playgroud)
显然上面没有用.
假设有如下图所示:
Map<String, List<String>> myMap =
{
k1: [ v1, v2, v3, v3],
k2: [ v1, v2],
k3: [ v1, v2, v6, v7]
}
Run Code Online (Sandbox Code Playgroud)
我想找到所有键通用的值。
List<String> commonValues = {v1,v2}
Run Code Online (Sandbox Code Playgroud)
我想了解使用 java 8 的有效方法。我可以在 java 5 中使用 for 循环实现相同的效果,但我确信 Java 8 有更好的方法来做到这一点。
考虑以下代码:
public class StreamDemo {
public static void main(String[] args) {
StreamObject obj = new StreamObject();
obj.setName("mystream");
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
list.parallelStream().forEach(l -> {
obj.setId(l);
System.out.println(obj + Thread.currentThread().getName());
});
}
static public class StreamObject {
private String name;
private Integer id;
// getters, setters, toString()
}
}
Run Code Online (Sandbox Code Playgroud)
当使用 java 11 编译并运行时,它返回以下内容:
StreamObject{name='mystream', id=4}ForkJoinPool.commonPool-worker-23
StreamObject{name='mystream', id=4}main
StreamObject{name='mystream', id=4}ForkJoinPool.commonPool-worker-9
StreamObject{name='mystream', id=4}ForkJoinPool.commonPool-worker-5
StreamObject{name='mystream', id=4}ForkJoinPool.commonPool-worker-19
Run Code Online (Sandbox Code Playgroud)
但对于 java 1.8,它返回不同的结果:
StreamObject{name='mystream', id=3}main
StreamObject{name='mystream', id=5}ForkJoinPool.commonPool-worker-2
StreamObject{name='mystream', id=2}ForkJoinPool.commonPool-worker-9
StreamObject{name='mystream', id=1}ForkJoinPool.commonPool-worker-11
StreamObject{name='mystream', id=4}ForkJoinPool.commonPool-worker-4
Run Code Online (Sandbox Code Playgroud)
为什么结果不同?
我想使用Streams和Lambdas.但是如何用它重写我当前的方法呢?
public double calculate() {
double result = 0.0;
if (!mediumList.isEmpty()) {
Iterator<Medium> it = mediumList.iterator();
while (it.hasNext()) {
result= result+ it.next().getAge();
}
result = result / mediumList.size();
}
return result;
}
Run Code Online (Sandbox Code Playgroud) 我有一个对象:
LocalDate localDate;
int amount;
Run Code Online (Sandbox Code Playgroud)
我有一个这样的对象列表,如:
{2018-01-01, 5}
{2018-01-01, 10}
{2018-01-02, 15}
Run Code Online (Sandbox Code Playgroud)
现在我想要实现我想要从同一天解析金额.预期结果将列出不重复金额的总和
{2018-01-01, 15}
{2018-01-02, 15}
Run Code Online (Sandbox Code Playgroud)
好像我错过了一些流方法来做到这一点......
precondition:
1.inteface A extended B,C,D;
2.A,B,C,D have same default method : default Object getById(Long Id){...};
user case:
1.class E implements A;
2.class E call default method getById, then which one will be called?
Run Code Online (Sandbox Code Playgroud)
我很迷惑。记不清了。^_^
java ×12
java-stream ×10
java-8 ×9
arraylist ×1
collectors ×1
comparator ×1
compare ×1
date ×1
grouping ×1
hashmap ×1
int ×1
java-11 ×1
lambda ×1
list ×1