Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何表示地图<;字符串,Map<;字符串,Obj>&燃气轮机;字符串格式_Java_Lambda_Java 8_Java Stream - Fatal编程技术网

Java 如何表示地图<;字符串,Map<;字符串,Obj>&燃气轮机;字符串格式

Java 如何表示地图<;字符串,Map<;字符串,Obj>&燃气轮机;字符串格式,java,lambda,java-8,java-stream,Java,Lambda,Java 8,Java Stream,我有下面的代码,我想给用户的输出在给定的格式,这是很容易阅读 如何使用Lambda实现这一点。我尝试了map函数,但没有得到结果 以下是我期望的输出: ID1=DEFAULT1 => NewYork ID1=DEFAULT2 => London ID1=DEFAULT3 => India ID2=DEFAULT1 => NewYork ID2=DEFAULT2 => London ID2=DEFAULT3 => India 公共类测试{ 公共静

我有下面的代码,我想给用户的输出在给定的格式,这是很容易阅读

如何使用Lambda实现这一点。我尝试了map函数,但没有得到结果

以下是我期望的输出:

ID1=DEFAULT1 => NewYork

ID1=DEFAULT2 => London

ID1=DEFAULT3 => India


ID2=DEFAULT1 => NewYork

ID2=DEFAULT2 => London

ID2=DEFAULT3 => India

公共类测试{
公共静态void main(字符串[]args){
Map Map=newhashmap();
Map timePeriodMap=newhashmap();
timePeriodMap.put(“DEFAULT1”,新时间段(“NewYork”);
timePeriodMap.put(“DEFAULT2”,新的时间段(“伦敦”);
timePeriodMap.put(“DEFAULT3”,新的时间段(“印度”);
Map stringMap=newhashmap();
stringMap.put(“ID1”,timePeriodMap);
stringMap.put(“ID2”,timePeriodMap);
}
}
班级时间段{
弦带;
公共时间段(字符串区域){
这个区域=区域;
}
公共字符串getZone(){
回流区;
}
公共无效设置区域(字符串区域){
这个区域=区域;
}
}
无需使用,因为每种方法都可以通过简单的程序轻松实现。用于生成可打印输出并保持键的有序

StringJoiner joiner = new StringJoiner(System.lineSeparator());

stringMap.forEach((id, timeMap) -> {
    timeMap.forEach((defaultValue, timePeriod) -> {
         joiner.add(String.format("%s=%s => %s", id, defaultValue, timePeriod.getZone()));
    });
});

System.out.println(joiner.toString());
结果:

ID1=DEFAULT1 => NewYork
ID1=DEFAULT2 => London
ID1=DEFAULT3 => India
ID2=DEFAULT1 => NewYork
ID2=DEFAULT2 => London
ID2=DEFAULT3 => India

首先,您的地图类型看起来有点错误

你的意思是要声明地图吗

如果是这样,您不需要第二个stringMap。您可以将时间段条目添加到map变量中

如果只是想打印,您可以执行以下操作:

Map<String, Map<String, TimePeriod>> map = new HashMap<>();

    Map<String,TimePeriod> timePeriodMap = new HashMap<>();

    timePeriodMap.put("DEFAULT1",new TimePeriod("NewYork"));
    timePeriodMap.put("DEFAULT2",new TimePeriod("London"));
    timePeriodMap.put("DEFAULT3",new TimePeriod("India"));

    map.put("ID1",timePeriodMap);
    map.put("ID2",timePeriodMap);


    map.forEach((id, timePeriods) -> {
        timePeriods.forEach((defaultValue, timePeriod) -> System.out.println(id + "=" + defaultValue + " => " + timePeriod.getZone()));
    });
Map Map=newhashmap();
Map timePeriodMap=newhashmap();
timePeriodMap.put(“DEFAULT1”,新时间段(“NewYork”);
timePeriodMap.put(“DEFAULT2”,新的时间段(“伦敦”);
timePeriodMap.put(“DEFAULT3”,新的时间段(“印度”);
map.put(“ID1”,timePeriodMap);
map.put(“ID2”,timePeriodMap);
map.forEach((id,时间段)->{
timePeriods.forEach((defaultValue,timePeriod)->System.out.println(id+“=”+defaultValue+“=>”+timePeriod.getZone());
});
Map接口有一个forEach方法,允许您迭代entrySet。
通过迭代映射的“外部”入口集,然后迭代嵌套映射的“内部”入口集,您可以获得所需的输出。

1)您是否只想打印结果或映射到特定的数据结构?2)您目前的尝试是什么?@Nikolas我尝试了以下操作,但未能完成:stringMap.entryset().stream().map(mapEntry->mapEntry.getValue().entrySet().stream().map(timePeriodEntry->mapEntry.getKey()+“=”+timePeriodEntry.getKey()+timePeriodEntry.getRegion()).collect(收集器.com)(“\n”)).map(s->s)).get().toString();我只需要字符串,这样我就可以打印到userWell,您只需要读取这些值并将其作为字符串打印输出。请参阅我编辑的答案。编辑它。我需要字符串作为回报,因为我将在GUISee@Nikolas answer上显示它,这是一种将输出添加到变量的好方法。您还可以将“new HashMap()”替换为new LinkedHashMap()“OPs answer中声明了三个映射。是的,您也可以使用printf。是的,类
StringJoiner
重写
toString
方法,但是,我更喜欢显示如何请求
字符串
类型,这在以后的处理中可能很有用:)
Map<String, Map<String, TimePeriod>> map = new HashMap<>();

    Map<String,TimePeriod> timePeriodMap = new HashMap<>();

    timePeriodMap.put("DEFAULT1",new TimePeriod("NewYork"));
    timePeriodMap.put("DEFAULT2",new TimePeriod("London"));
    timePeriodMap.put("DEFAULT3",new TimePeriod("India"));

    map.put("ID1",timePeriodMap);
    map.put("ID2",timePeriodMap);


    map.forEach((id, timePeriods) -> {
        timePeriods.forEach((defaultValue, timePeriod) -> System.out.println(id + "=" + defaultValue + " => " + timePeriod.getZone()));
    });