我喜欢下面写的地图,我的实现为mutable.HashMap。
class SampleMap() extends mutable.HashMap[String, (Any, BigInt)]
Run Code Online (Sandbox Code Playgroud)
映射的+ =方法和其他方法都是从超类使用的,而不是其他方法。下面的作品完美。
override def +=(kv: (String, (Any, BigInt))): this.type = {/*compiled code*/}
Run Code Online (Sandbox Code Playgroud)
现在,我想编写我的++ =方法的自定义实现,该方法是Map从可增长类继承的。当我编写重写的方法++ =时,编译器没有抱怨要重写的内容。
override def ++=(currentMap: MergeMap): this.type = {
Run Code Online (Sandbox Code Playgroud)
如何为我的自定义Map编写此方法的自定义实现。
我正在实现一个流,在其中使用集合listOfFoo来获取该列表中所有项目的ID,并使用它们来获取Bar实例的值。
我想确保此方法在酒吧列表上没有任何项目的情况下将抛出ResourceNotFoundException,尽管在当前状态下它会检查列表酒吧是否为null,而不是,因为它包含一个空列表。
您能帮我一下,并提出一些解决方案吗?
List<Bar> bars = Optional.ofNullable(
listOfFoos.stream()
.map(Foo::getId)
.map(fooId -> service.getBars(fooId))
.filter(Objects::nonNull)
.collect(Collectors.toList()))
.orElseThrow(() -> new ResourceNotFoundException(Bar.class, OBJECT_NULL));
Run Code Online (Sandbox Code Playgroud) 我需要获取现有Java TimeZone对象的缩写(作为String)。没有直接映射,有没有办法做到这一点?
Eg- TimeZone - ("America/Los_Angeles")
Abbreviation - PDT
Run Code Online (Sandbox Code Playgroud) 我想提供用户定义的方法来合并Java 8中的地图吗?您创建的方法应接受两个映射和“合并”行为。
public <T> Map<? super T, ? super T> mergeMaps( Map<? super T, ? super
T> map1, Map<? super T, ? super T> map2 ) {
// merging code here
}
Run Code Online (Sandbox Code Playgroud)
但我想要这样的东西
public <T> Map<? super T, ? super T> mergeMaps( Map<? super T, ? super T>
map1, Map<? super T, ? super T> map2 , MergeTwoMaps<T, U> mergeTwoMaps)
{
// merging code here
}
Run Code Online (Sandbox Code Playgroud)
这样的事情。Java通用合并
Map<String, Integer> map1 = new HashMap<>();
Map<String, Integer> map2 = new HashMap<>(); …Run Code Online (Sandbox Code Playgroud) 我是Java 8的新手,我需要重写一段旧代码以实现新的算法。任务是过滤每个列表具有最大速度的对象。列表嵌套在地图内部:道路的根地图,其中包含路段的地图,每条路段的地图都包含一系列对象,每个对象描述一个时间间隔的测得速度。我需要找到每个列表的所有最大速度。
我发现以下链接看起来像我的问题,但是我无法适应它们,而且我不确定我的尝试能否正确解决我的问题。
Java 8筛选器映射<String,List <Employee >>
这是我用来写我的例子的代码
Map<String, Map<String, Employee>> employeeMap =
employeeMap.entrySet()
.stream()
.collect(toMap(Map.Entry::getKey,
e -> e.getValue().entrySet().stream()
.filter(emp -> !emp.getValue().getState().equals("MI"))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue))));
for(Car car : cars) {
for (Engine engine : car.getEngines()) {
for (Part part : engine.getParts()) {
// ...
}
}
}
cars.stream()
.flatMap(car -> car.getEngines().stream())
.flatMap(engine -> engine.getParts().stream())
.forEach(part -> { ... });
Run Code Online (Sandbox Code Playgroud)
这是我的结果代码
Map<Integer, Map<Integer, List<PartialSpeed>>> speedsPerRoadPerSegment;
ArrayList<PartialSpeed> maxSpeedPerSegment = new ArrayList<PartialSpeed>();
speedsPerRoadPerSegment.entrySet().stream()
.forEach(road-> road.getValue().entrySet()
.stream()
.forEach(segment ->
{
Optional<PartialSpeed> result …Run Code Online (Sandbox Code Playgroud) 我习惯用java样板在方法顶部检查输入参数:
public static Boolean filesExist(String file1, String file2, String file3 ... ) {
if (file1 == null || file2 == null || file3 == null ||...) {
throw new IllegalArgumentException();
}
if (another_param == null) {
throw new NullPointerException();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我正在阅读Java 8的可选参数,并注意到我们可以改为执行以下操作:
Optional.ofNullable(file1).orElseThrow(IllegalArgumentException::new);
Optional.ofNullable(file2).orElseThrow(IllegalArgumentException::new);
Optional.ofNullable(another_param).orElseThrow(NullPointerException::new);
...
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,第二种方法是否有任何弊端,我觉得它对我来说看起来更干净一些。
PS: Sorry for my poor english.I can't describe the problem clearly. :(
When I don't use the "lambda method reference" code style in the static block, like:
static{
map.keySet().parallelStream().forEach(e -> {
System.out.println(e);
});
}
Run Code Online (Sandbox Code Playgroud)
then the program running forever, never stop.
But when I change the code to
static{
map.keySet().parallelStream().forEach(System.out::println);
}
Run Code Online (Sandbox Code Playgroud)
然后该错误消失了。该程序可以立即完成。
请直接看一下代码,我已经尽力简化了代码。
public class BugSimulate {
static {
init();
}
private static void init() {
Map<Integer, String> map = new HashMap<>();
int i = 0;
map.put(++i, "1");
map.put(++i, "1"); …Run Code Online (Sandbox Code Playgroud) 我有以下课程:
class Data {
String systemId;
String fileName;
int x;
int y;
Data(String systemId, String fileName, int x, int y) {
this.systemId = systemId;
this.fileName = fileName;
this.x = x;
this.y = y;
}
public String getSystemId() {
return systemId;
}
public void setSystemId(String systemId) {
this.systemId = systemId;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = …Run Code Online (Sandbox Code Playgroud) 我们正在维护一个当前在Java 8上运行的项目。现在,我们必须通过其api调用合作伙伴服务器,以将一些数据迁移到我们的数据库中。
当我们调用他们的服务器时,我们得到了一个例外:
原因:org.apache.http.ProtocolException:服务器无法使用有效的HTTP响应进行响应
在这种情况下,响应代码为-1。
所以我尝试使用curl从他们的服务器获取数据,我看到他们的响应状态行是:
HTTP / 2 200
虽然HttpURLConnection像这样检查:
if (statusLine.startsWith("HTTP/1.")) {
int codePos = statusLine.indexOf(' ');
if (codePos > 0) {
int phrasePos = statusLine.indexOf(' ', codePos+1);
if (phrasePos > 0 && phrasePos < statusLine.length()) {
responseMessage = statusLine.substring(phrasePos+1);
}
// deviation from RFC 2616 - don't reject status line
// if SP Reason-Phrase is not included.
if (phrasePos < 0)
phrasePos = statusLine.length();
try {
responseCode = Integer.parseInt
(statusLine.substring(codePos+1, phrasePos));
return responseCode;
} catch (NumberFormatException e) …Run Code Online (Sandbox Code Playgroud) 我有一个独特的位置对象集,数据如下
location 1: "USA", "AZ", "Phoenix"
location 2: "USA", "AZ", "Scottsdale"
location 3: "USA", "AZ", "Peoria"
Front end needs following json structure to render UI:
"USA"-> [{"AZ" -> ["Phoenix", "Scottsdale","Peoria"]},
{"MD" -> ["Baltimore", "Gaithersburg","OwingsMills"]}
]
Run Code Online (Sandbox Code Playgroud)
适用于Location.java的Java POJO
我编写了几行代码来迭代位置集,并使用FacetsGeo对象构造了包含状态对象集的国家对象(包含城市对象的集合),并从此数据模型中生成json。
我在想,可能会有更好的方法:可能是使用Java 8 Streaming API。
正如我上面所说的,任何帮助表示赞赏,都是示例数据,如何构建平坦的位置。
public class Location {
private final String country;
private final String state;
private final String city;
public Location(final String country, final String state, final
String city) {
this.country = country;
this.state = state;
this.city = city;
} …Run Code Online (Sandbox Code Playgroud)