我的哈希映射为<Integer, Set<Integer>>.
我愿意使用Java流操作来获得具有最大大小的Set。
这是我的例子:
public class Example {
public static void main( String[] args ) {
Map<Integer,Set<Integer>> adj = new HashMap<>();
Set<Integer> set1 = Stream.of(1,2,3).collect(Collectors.toSet());
Set<Integer> set2 = Stream.of(1,2).collect(Collectors.toSet());
adj.put(1,set1);
adj.put(2,set2);
}
}
我已经尝试过了:
Collections.max(adj,Comparator.comparingInt(Set::size));
但是我遇到了编译错误,因为Set接口中的size()方法不是静态的。
通常我们应该将最大设置为3。
答案 0 :(得分:4)
您不能将Map<Integer,Set<Integer>>
与Collection.max
一起使用。被定义为采用集合。
public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp)
因此,要使其正常运行,请执行以下操作:
Collections.max(adj.values(), Comparator.comparingInt(Set::size));
或流:
adj.values()
.stream()
.max(Comparator.comparingInt(Set::size));
答案 1 :(得分:3)
我们应该将最大尺寸设置为3。
要获取地图中int maxSetSize = adj.values()
.stream()
.max(Comparator.comparingInt(Set::size))
.map(Set::size)
.orElse(0);
的最大大小,可以使用:
data = [[{'age_days': '72', 'age_years': '30', 'alpha_name': 'RAVAL,JA', 'batting_hand': 'left-hand batsman', 'batting_style': 'lhb', 'batting_style_long': 'left-hand bat', 'bowling_hand': 'right-arm bowler', 'bowling_pacespin': 'spin bowler', 'bowling_style': 'lb', 'bowling_style_long': 'legbreak ', 'captain': '0', 'card_long': 'JA Raval', 'card_qualifier': '', 'card_short': 'Raval', 'dob': '1988-09-22', 'keeper': '0', 'known_as': 'Jeet Raval', 'mobile_name': 'Raval', 'object_id': '277914', 'player_id': '51094', 'player_primary_role': 'opening batsman', 'player_style_id': '1', 'player_type': '1', 'player_type_name': 'player', 'popular_name': 'Raval', 'portrait_alt_id': '1', 'portrait_object_id': '397349', 'status_id': '3'}, {'age_days': '245', 'age_years': '26', 'alpha_name': 'LATHAM,TWM', 'batting_hand': 'left-hand batsman', 'batting_style': 'lhb', 'batting_style_long': 'left-hand bat', 'bowling_hand': 'right-arm bowler', 'bowling_pacespin': 'pace bowler', 'bowling_style': 'rm', 'bowling_style_long': 'right-arm medium ', 'captain': '0', 'card_long': 'TWM Latham', 'card_qualifier': '', 'card_short': 'Latham', 'dob': '1992-04-02', 'keeper': '0', 'known_as': 'Tom Latham', 'mobile_name': 'Latham', 'object_id': '388802', 'player_id': '59148', 'player_primary_role': 'wicketkeeper batsman', 'player_style_id': '7', 'player_type': '1', 'player_type_name': 'player', 'popular_name': 'Latham', 'portrait_alt_id': '1', 'portrait_object_id': '1099471', 'status_id': '3'}]]
df = pd.DataFrame(data[0])
print(df)
age_days age_years alpha_name batting_hand batting_style \
0 72 30 RAVAL,JA left-hand batsman lhb
1 245 26 LATHAM,TWM left-hand batsman lhb
batting_style_long bowling_hand bowling_pacespin bowling_style \
0 left-hand bat right-arm bowler spin bowler lb
1 left-hand bat right-arm bowler pace bowler rm
bowling_style_long ... object_id player_id player_primary_role \
0 legbreak ... 277914 51094 opening batsman
1 right-arm medium ... 388802 59148 wicketkeeper batsman
player_style_id player_type player_type_name popular_name portrait_alt_id \
0 1 1 player Raval 1
1 7 1 player Latham 1
portrait_object_id status_id
0 397349 3
1 1099471 3
[2 rows x 28 columns]