HashMap本质上具有O(1)性能,而开关状态可以具有O(1)或O(log(n)),具体取决于编译器是使用tableswitch还是查找开关.
可以理解的是,如果switch语句是这样编写的,
switch (int) {
case 1:
case 2:
case 3:
case 4:
default:
}
Run Code Online (Sandbox Code Playgroud)
那么它将使用一个tableswitch,并且显然比标准的HashMap具有性能优势.但是如果switch语句稀疏怎么办?这将是我要比较的两个例子:
HashMap<Integer, String> example = new HashMap<Integer, String>() {{
put(1, "a");
put(10, "b");
put(100, "c");
put(1000, "d");
}};
Run Code Online (Sandbox Code Playgroud)
.
switch (int) {
case 1:
return "a";
case 10:
return "b";
case 100:
return "c";
case 1000:
return "d";
default:
return null;
}
Run Code Online (Sandbox Code Playgroud)
什么会提供更多的吞吐量,lookupswitch或HashMap?HashMap的开销是否能够尽早为lookupswitch提供优势,但随着案例/条目数量的增加最终逐渐减少?
编辑:我尝试了一些使用JMH的基准测试,这里是我的结果和使用的代码.https://gist.github.com/mooman219/bebbdc047889c7cfe612 正如你们提到的,lookupswitch语句的表现优于HashTable.我仍然想知道为什么.
当我收到此错误时,我正在尝试为Windows安装github:
Application cannot be started. Contact the application vendor.
Run Code Online (Sandbox Code Playgroud)
错误日志产生了这个:
PLATFORM VERSION INFO
Windows : 6.2.9200.0 (Win32NT)
Common Language Runtime : 4.0.30319.34014
System.Deployment.dll : 4.0.30319.33440 built by: FX45W81RTMREL
clr.dll : 4.0.30319.34014 built by: FX45W81RTMGDR
dfdll.dll : 4.0.30319.33440 built by: FX45W81RTMREL
dfshim.dll : 6.3.9600.16384 (winblue_rtm.130821-1623)
SOURCES
Deployment url : http://github-windows.s3.amazonaws.com/GitHub.application
ERROR SUMMARY
Below is a summary of the errors, details of these errors are listed later in the log.
* Activation of http://github-windows.s3.amazonaws.com/GitHub.application resulted in exception. Following failure messages were detected: …Run Code Online (Sandbox Code Playgroud) 我正在java中制作一个Tic Tac Toe程序,因为我正在学习java,我认为一个简单的项目将是一个很好的起点.到目前为止这是我的代码:
public class Start {
public static void main(String[] args) {
GameTicTacToe gameTicTacToe = new GameTicTacToe();
gameTicTacToe.windowBirth();
}
}
Run Code Online (Sandbox Code Playgroud)
和,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GameTicTacToe implements ActionListener {
private int gridSize = 3;
private JButton[] gridButton = new JButton[(gridSize * gridSize)];
private JPanel grid = new JPanel(new GridLayout(gridSize, gridSize, 0, 0));
private JFrame windowTicTacToe = new JFrame("Tisk, Task, Toes");
private int[] gridButtonOwner = new int[(gridSize * gridSize)];
private int turn = 1;
private int …Run Code Online (Sandbox Code Playgroud) 我有一个JButton,当玩家点击它时,它会告诉我的Action Listener单击一个按钮.我想知道的是,有一个命令或某些东西就像玩家点击了按钮一样.
就像Tic Tac Toe一样,我有2个玩家可以互相对战,但我想为电脑玩家和人类玩家添加选项.但由于计算机实际上无法点击按钮,我迷路了.
编辑:它会像gridButton2.click()(按钮名称)一样容易.click();
现在我通过使用改变按钮的背景颜色
button.setBackground(Color.WHITE);
Run Code Online (Sandbox Code Playgroud)
这是一个例子.
但是当我有一个巨大的网格jbuttons(1000+)时,只需运行for循环来改变每个按钮的背景是非常非常慢的.你可以看到网格慢慢变白,一帧一帧.我真的不想要这个
有没有更好的方法将网格上的每个JButton同时更改为相同的颜色?
这就是我制作网格的方式,所使用的数字仅仅是例如......
grid = new JPanel(new GridLayout(64, 64, 0, 0));
Run Code Online (Sandbox Code Playgroud)
这是4096个按钮,将每个按钮更改为相同颜色需要大约30秒以上.
编辑1:我需要按钮是可点击的,就像当我点击一个按钮时,它变为蓝色.单击所有按钮时,将每个按钮的颜色更改为白色.现在我的工作正常,但改变每个按钮的颜色只是很慢.
编辑2:这就是我改变按钮的方式:
new javax.swing.Timer(300, new ActionListener() {
int counter = 0;
public void actionPerformed(ActionEvent e) {
if (counter >= counterMax) {
((Timer) e.getSource()).stop();
}
Color bckgrndColor = (counter % 2 == 0) ? flashColor : Color.white;
for (JButton button : gridButton) {
button.setBackground(bckgrndColor);
}
counter++;
}
}).start();
Run Code Online (Sandbox Code Playgroud)