Person这是我将用作键的类的示例。
它会覆盖,equals()以便它始终返回false并且hashCode()that 始终返回1。
public class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object o) {
System.out.println("Equals is called");
return false;
}
@Override
public int hashCode() {
System.out.println("Hashcode is called");
return 1;
}
}
Run Code Online (Sandbox Code Playgroud)
现在让我们用它作为键并随机填充地图。
public class Main {
public static void main(String[] args) {
var person1 = new Person("Mike", 21);
var person2 = new Person("Alex", 32); …Run Code Online (Sandbox Code Playgroud) Instant我正在尝试根据本文中的参考从对象创建 ISO 8601 格式的 DateTime,我使用该格式YYYY-MM-DD'T'hh:mm:ss'T'ZD来解析即时日期,如下所示。
但它生成的时间格式错误:
2022-06-172T06:08:13T-0500172
Run Code Online (Sandbox Code Playgroud)
预期的格式应该是:
2022-06-21T13:31:49-05:00
Run Code Online (Sandbox Code Playgroud)
我的代码:
DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("YYYY-MM-DD'T'hh:mm:ss'T'ZD")
.withZone(ZoneId.systemDefault());
formatter.format(Instant.now())
Run Code Online (Sandbox Code Playgroud)
如何生成如下所示的格式化时间?
2022-06-21T13:31:49-05:00
Run Code Online (Sandbox Code Playgroud) 我希望避免多种if-else情况。下面的代码有更简洁的写法吗?
private Set<String> getValues(Optional<String> one, Optional<String> two) {
if (one.isPresent() && two.isPresent()) {
return ImmutableSet.of(one.get(), two.get());
} else if (one.isPresent()) {
return ImmutableSet.of(one.get());
} else {
return two.isPresent() ? ImmutableSet.of(two.get()) : ImmutableSet.of();
}
}
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 后,代码性能会更好吗?
我有一个字符串流:
Stream<String> st = Arrays.stream(new String[]{"10", "20", "30", "40", "50"});
Run Code Online (Sandbox Code Playgroud)
我想将它们转换为整数并使用该方法对它们进行求和reduce。我使用以下格式的reduce:
int result = st.reduce(0, (a, b) -> a + Integer.parseInt(b));
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
no suitable method found for reduce(int, (a, b) -> a + [...]nt(b))
Run Code Online (Sandbox Code Playgroud)
有关如何重写reduce 方法以使其工作的任何提示吗?
我尝试执行以下操作,但它不起作用
Optional<List<Integer>> listOne = someAPI.call()
// convert
Optional<ArrayList<Integer>> listTwo = new ArrayList<>(listOne); // doesn't work
Optional<ArrayList<Integer>> listTwo = Optional.of(new ArrayList(listOne)); // also not working
Run Code Online (Sandbox Code Playgroud)
请注意,第一个 API 返回Optional<List>. 我需要发送Optional<ArrayList>到另一个 API。
我有一份清单Employee。
public class Employee {
private String department;
private double salary;
// other properties
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试查找平均工资最高的部门的名称。
我尝试过的代码:
List<Employee> employeeList = new ArrayList<Employee>();
employeeList.add(new Employee("abc", 38, "M", "QA", 115000.00d, 2016));
employeeList.add(new Employee("def", 23, "M", "Sports", 5000000.00d, 2011));
employeeList.add(new Employee("ghi", 38, "M", "QA", 120000.00d, 2007));
employeeList.add(new Employee("jkl", 35, "M", "DEV", 50000.00d, 2016));
employeeList.add(new Employee("mno", 30, "F", "QA", 80000.00d, 206));
employeeList.add(new Employee("pqr", 32, "F", "DEV", 75000.00d, 2014));
Map<String, Double> highestAvgSalary = employeeList.stream()
.collect(Collectors.groupingBy(
emp -> emp.getDepartment(),
Collectors.averagingDouble(emp -> emp.getSalary(),
Collectors.maxBy(Comparator.comparingDouble(emp -> …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个名为 Range 的类,它采用一个length 的整数数组(未排序),仅包含从到n范围内的数字。0k
首先,我声明一个构造函数,它将通过计数排序算法预处理数组。
然后我想编写query()接受两个整数参数的方法:a和,它形成从到 的b数字范围,并返回数组中具有给定范围内的值的所有元素的总频率。ab
我的代码:
import java.util.Arrays;
import java.util.HashMap;
public class Range {
private int[] a;
private int k;
public Range(int[] a, int k) {
int index = 0;
int[] counterArray = new int[k + 1];
for (int i : a)
counterArray[i]++; // initialize counterArray
for (int i = 0; i < counterArray.length; i++)
while (0 < counterArray[i]) {
a[index++] …Run Code Online (Sandbox Code Playgroud) 我创建了一个TreeMapwith 产品。
我想计算它们重复的次数,但不知道要编码什么。有什么提示吗?(我期望没有代码,只是建议)
private static Map<Integer, String> shoppingCart() {
Map<Integer, String> result = new TreeMap<>();
result.put(1, "sausage");
result.put(2, "sausage");
result.put(3, "soup");
result.put(4, "egg");
result.put(5, "egg");
result.put(6, "tomato");
result.put(7, "sausage");
return result;
}
Run Code Online (Sandbox Code Playgroud)
我正在考虑添加一个计数变量,但仍然不能解决重复问题。
我问了很多与此相关的问题,但无法解决我的问题。
这是我的问题。
我有一个父抽象类。
public abstract class Parent{ }
Run Code Online (Sandbox Code Playgroud)
我还有另外两个子类,它们是从上面的父类扩展而来的。
public class ChildOne extends Parent{}
public class ChildTwo extends Parent{}
Run Code Online (Sandbox Code Playgroud)
在另一堂课中,我使用这三个课程,如下所示。
public class A{
public List<ExcelRecord<Parent>> getExcelRecords() {
ChildOne childone = new ChildOne();
List<ExcelRecord<ChildOne>> list = new ArrayList<>();
// some logic here
return list; // **compilation here**
}
}
Run Code Online (Sandbox Code Playgroud)
该代码产生以下编译错误:
required: List<ExcelRecord<Parent>>
provided: List<ExcelRecord<ChildOne>>
Run Code Online (Sandbox Code Playgroud)
我需要将子类型泛型返回到父类型泛型。我怎样才能实现这个目标?
请注意,此方法的返回值正在旧代码中使用,无法进行相应更改。它应该保留List<ExcelRecord<Parent>>。
java ×10
hashmap ×3
java-stream ×3
java-8 ×2
option-type ×2
algorithm ×1
arraylist ×1
arrays ×1
collections ×1
collectors ×1
datetime ×1
equals ×1
generics ×1
guava ×1
hashcode ×1
inheritance ×1
java-time ×1
lambda ×1
list ×1
set ×1
sorting ×1
string ×1
treemap ×1