当前位置:网站首页>Puge -- understanding of getordefault() method
Puge -- understanding of getordefault() method
2022-06-28 06:32:00 【Dear-JC】
analysis getOrDefault() Method
Introduce
getOrDefault(Object key, V defaultValue)
In operation Map When the collection , Determine whether this is stored key. If there is a corresponding value value , If it does not exist , Then the user-defined default value is returned defaultValue value .
Code implementation
LeetCode The first 387 topic : The first unique character in the string
public static void main(String[] args) {
String s = "loveleetcode";
System.out.println(firstUniqChar(s));
}
public static int firstUniqChar(String s) {
Map<Character, Integer> frequency = new HashMap<Character, Integer>();
for (int i = 0; i < s.length(); ++i) {
char ch = s.charAt(i);
// When this key when , Just use this value value , There is no default defaultValue
frequency.put(ch, frequency.getOrDefault(ch, 0) + 1); // Put the previous values in , By default +1 Calculation , Press... Only when there is a repetition ch Of value Calculation .
}
for (int i = 0; i < s.length(); ++i) {
if (frequency.get(s.charAt(i)) == 1) {
// Returns the specified character , Treat characters as key Go back
return i;
}
}
return -1; // Returns the last value
}
The end
边栏推荐
猜你喜欢

Caused by: com. fasterxml. jackson. databind. exc.InvalidDefinitionException: Cannot construct instance

助力涨点 | YOLOv5结合Alpha-IoU

Drop down list processing in Web Automation

Difficulty calculation of Ethereum classic

Yygh-7-user management

Exception handling (I) -- null pointer and array index out of bounds

Triode driven brushless motor

Build your jmeter+jenkins+ant

AutoCAD C# 多段线自相交检测

Speech enhancement - spectrum mapping
随机推荐
Exception handling (I) -- null pointer and array index out of bounds
【Paper Reading-3D Detection】Fully Convolutional One-Stage 3D Object Detection on LiDAR Range Images
Freeswitch uses origin to dialplan
windows上安装redis并永久修改密码,及ssm框架集成redis
小程序页面设置100%高度还是留白怎么办?
AutoCAD C polyline self intersection detection
[interval DP] stone consolidation
Yygh-7-user management
Shell script one click deployment (MySQL)
Failed to start component [StandardEngine[Catalina]. StandardHost[localhost]]
ROS rviz_satellite功能包可视化GNSS轨迹,卫星地图的使用
ThreadLocal
Freeswitch uses Mod_ Shot module plays mp3
Interpretation of Blog
AutoCAD C # Polyline Small Sharp angle Detection
@The reason why the Autowired annotation is empty
AutoCAD C# 多段線小銳角檢測
FPGA - 7 Series FPGA selectio -08- oserdese2 of advanced logic resources
FPGA - 7系列 FPGA SelectIO -08- 高级逻辑资源之OSERDESE2
4. use MySQL shell to install and deploy Mgr clusters | explain Mgr in simple terms