显示 LinkedHashMap 列表
Display a LinkedHashMap list
我无法正确显示数据。我有一个 sql 查询 return 以下数据:
| count (long) | country (string)|
---------------| ------------------
| 6 | null |
| 3 | spain |
| 6 | italy |
而我有以下方法:
private Map<String, Long> countryCount (List<CountryCount> summary) {
Map<String, Long> result = new HashMap<>();
summary.forEach(sum -> {
var country= sum.getCountry();
if (country == null) {
country= "unknown";
}
var count = result.get(country);
if (count == null) {
count = 0L;
}
result.put(country, count + sum.getCount());
});
return result.entrySet().stream().sorted(Map.Entry.comparingByKey())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new));
}
在我的方法中,我想 return 一个像 {"unknown" : 5, "spain" : 3, "italy" : 6}
这样的对象,但我得到 {"unknown" : 15}
也许这个代码更简单。
Map < String, Long > map =
listOfCountryCount
.stream()
.collect(
Collectors.toMap(
countryCount -> Objects.isNull( countryCount.getCountry() ) ? "unknown" : countryCount.getCountry() ,
countryCount -> Objects.isNull( countryCount.getCount() ) ? Long.valueOf( 0 ) : countryCount.getCount()
)
);
如果您想要一个排序的映射,请将该映射传递给 SortedMap
或 NavigableMap
实现的构造函数。
NavigableMap< String , Long > mapNav = new TreeMap<>( map ) ;
完整示例代码。
package work.basil.demo;
import java.util.*;
import java.util.stream.Collectors;
class Ideone
{
public static void main ( String[] args ) throws java.lang.Exception
{
List < CountryCount > listOfCountryCount = List.of(
new CountryCount( "Peru" , 17L ) ,
new CountryCount( "Xanadu" , 128L ) ,
new CountryCount( null , 42L ) ,
new CountryCount( "Andorra" , null )
);
Map < String, Long > map =
listOfCountryCount
.stream()
.collect(
Collectors.toMap(
countryCount -> Objects.isNull( countryCount.getCountry() ) ? "unknown" : countryCount.getCountry() ,
countryCount -> Objects.isNull( countryCount.getCount() ) ? Long.valueOf( 0 ) : countryCount.getCount()
)
);
NavigableMap < String, Long > mapNav = new TreeMap <>( map ); // Keys are maintained in sorted order.
System.out.println( "listOfCountryCount = " + listOfCountryCount );
System.out.println( "map = " + map );
System.out.println( "mapNav = " + mapNav );
}
}
final class CountryCount
{
private final String country;
private final Long count;
CountryCount ( String country , Long count )
{
this.country = country;
this.count = count;
}
public String getCountry ( ) { return country; }
public Long getCount ( ) { return count; }
@Override
public boolean equals ( Object obj )
{
if ( obj == this ) return true;
if ( obj == null || obj.getClass() != this.getClass() ) return false;
var that = ( CountryCount ) obj;
return Objects.equals( this.country , that.country ) &&
Objects.equals( this.count , that.count );
}
@Override
public int hashCode ( )
{
return Objects.hash( country , count );
}
@Override
public String toString ( )
{
return "CountryCount[" +
"country=" + country + ", " +
"count=" + count + ']';
}
}
当运行.
listOfCountryCount = [CountryCount[country=Peru, count=17], CountryCount[country=Xanadu, count=128], CountryCount[country=null, count=42], CountryCount[country=Andorra, count=null]]
map = {Andorra=0, Xanadu=128, Peru=17, unknown=42}
mapNav = {Andorra=0, Peru=17, Xanadu=128, unknown=42}
如果您不确定您的数据是否不同,让我们向该 Collectors.toMap
调用添加第三个参数。我们传递了一个 lambda 函数,在出现重复键的情况下调用。在我们这里的例子中,我们采用首次出现获胜的策略。
Map < String, Long > map =
listOfCountryCount
.stream()
.collect(
Collectors.toMap(
countryCount -> Objects.isNull( countryCount.getCountry() ) ? "unknown" : countryCount.getCountry() ,
countryCount -> Objects.isNull( countryCount.getCount() ) ? Long.valueOf( 0 ) : countryCount.getCount() ,
( existing , replacement ) -> existing // In case of duplicate key conflict, first one wins.
)
);
我们可以进一步缩短这段代码。我们可以将构造方法引用作为第四个参数传递给 Collectors.toMap
:TreeMap :: new
,而不是单独创建 TreeMap
。我们将返回映射的声明从 Map
更改为 NavigableMap
.
NavigableMap < String, Long > map =
listOfCountryCount
.stream()
.collect(
Collectors.toMap(
countryCount -> Objects.isNull( countryCount.getCountry() ) ? "unknown" : countryCount.getCountry() ,
countryCount -> Objects.isNull( countryCount.getCount() ) ? Long.valueOf( 0 ) : countryCount.getCount() ,
( existing , replacement ) -> existing , // In case of duplicate key conflict, first one wins.
TreeMap :: new
)
);
看到这个code run live at IdeOne.com。
listOfCountryCount = [CountryCount[country=Peru, count=17], CountryCount[country=Xanadu, count=128], CountryCount[country=null, count=42], CountryCount[country=Andorra, count=null]]
map = {Andorra=0, Peru=17, Xanadu=128, unknown=42}
我无法正确显示数据。我有一个 sql 查询 return 以下数据:
| count (long) | country (string)|
---------------| ------------------
| 6 | null |
| 3 | spain |
| 6 | italy |
而我有以下方法:
private Map<String, Long> countryCount (List<CountryCount> summary) {
Map<String, Long> result = new HashMap<>();
summary.forEach(sum -> {
var country= sum.getCountry();
if (country == null) {
country= "unknown";
}
var count = result.get(country);
if (count == null) {
count = 0L;
}
result.put(country, count + sum.getCount());
});
return result.entrySet().stream().sorted(Map.Entry.comparingByKey())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new));
}
在我的方法中,我想 return 一个像 {"unknown" : 5, "spain" : 3, "italy" : 6}
这样的对象,但我得到 {"unknown" : 15}
也许这个代码更简单。
Map < String, Long > map =
listOfCountryCount
.stream()
.collect(
Collectors.toMap(
countryCount -> Objects.isNull( countryCount.getCountry() ) ? "unknown" : countryCount.getCountry() ,
countryCount -> Objects.isNull( countryCount.getCount() ) ? Long.valueOf( 0 ) : countryCount.getCount()
)
);
如果您想要一个排序的映射,请将该映射传递给 SortedMap
或 NavigableMap
实现的构造函数。
NavigableMap< String , Long > mapNav = new TreeMap<>( map ) ;
完整示例代码。
package work.basil.demo;
import java.util.*;
import java.util.stream.Collectors;
class Ideone
{
public static void main ( String[] args ) throws java.lang.Exception
{
List < CountryCount > listOfCountryCount = List.of(
new CountryCount( "Peru" , 17L ) ,
new CountryCount( "Xanadu" , 128L ) ,
new CountryCount( null , 42L ) ,
new CountryCount( "Andorra" , null )
);
Map < String, Long > map =
listOfCountryCount
.stream()
.collect(
Collectors.toMap(
countryCount -> Objects.isNull( countryCount.getCountry() ) ? "unknown" : countryCount.getCountry() ,
countryCount -> Objects.isNull( countryCount.getCount() ) ? Long.valueOf( 0 ) : countryCount.getCount()
)
);
NavigableMap < String, Long > mapNav = new TreeMap <>( map ); // Keys are maintained in sorted order.
System.out.println( "listOfCountryCount = " + listOfCountryCount );
System.out.println( "map = " + map );
System.out.println( "mapNav = " + mapNav );
}
}
final class CountryCount
{
private final String country;
private final Long count;
CountryCount ( String country , Long count )
{
this.country = country;
this.count = count;
}
public String getCountry ( ) { return country; }
public Long getCount ( ) { return count; }
@Override
public boolean equals ( Object obj )
{
if ( obj == this ) return true;
if ( obj == null || obj.getClass() != this.getClass() ) return false;
var that = ( CountryCount ) obj;
return Objects.equals( this.country , that.country ) &&
Objects.equals( this.count , that.count );
}
@Override
public int hashCode ( )
{
return Objects.hash( country , count );
}
@Override
public String toString ( )
{
return "CountryCount[" +
"country=" + country + ", " +
"count=" + count + ']';
}
}
当运行.
listOfCountryCount = [CountryCount[country=Peru, count=17], CountryCount[country=Xanadu, count=128], CountryCount[country=null, count=42], CountryCount[country=Andorra, count=null]]
map = {Andorra=0, Xanadu=128, Peru=17, unknown=42}
mapNav = {Andorra=0, Peru=17, Xanadu=128, unknown=42}
如果您不确定您的数据是否不同,让我们向该 Collectors.toMap
调用添加第三个参数。我们传递了一个 lambda 函数,在出现重复键的情况下调用。在我们这里的例子中,我们采用首次出现获胜的策略。
Map < String, Long > map =
listOfCountryCount
.stream()
.collect(
Collectors.toMap(
countryCount -> Objects.isNull( countryCount.getCountry() ) ? "unknown" : countryCount.getCountry() ,
countryCount -> Objects.isNull( countryCount.getCount() ) ? Long.valueOf( 0 ) : countryCount.getCount() ,
( existing , replacement ) -> existing // In case of duplicate key conflict, first one wins.
)
);
我们可以进一步缩短这段代码。我们可以将构造方法引用作为第四个参数传递给 Collectors.toMap
:TreeMap :: new
,而不是单独创建 TreeMap
。我们将返回映射的声明从 Map
更改为 NavigableMap
.
NavigableMap < String, Long > map =
listOfCountryCount
.stream()
.collect(
Collectors.toMap(
countryCount -> Objects.isNull( countryCount.getCountry() ) ? "unknown" : countryCount.getCountry() ,
countryCount -> Objects.isNull( countryCount.getCount() ) ? Long.valueOf( 0 ) : countryCount.getCount() ,
( existing , replacement ) -> existing , // In case of duplicate key conflict, first one wins.
TreeMap :: new
)
);
看到这个code run live at IdeOne.com。
listOfCountryCount = [CountryCount[country=Peru, count=17], CountryCount[country=Xanadu, count=128], CountryCount[country=null, count=42], CountryCount[country=Andorra, count=null]]
map = {Andorra=0, Peru=17, Xanadu=128, unknown=42}