Java - 将 ConcurrentHashMap 添加到 JList 的正确方法?
Java - Correct way of add a ConcurrentHashMap to a JList?
这是我的示例代码:
人Class:
public class Person {
private String firstName;
private String lastName;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((firstName == null) ? 0 : firstName.hashCode());
result = prime * result
+ ((lastName == null) ? 0 : lastName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (firstName == null) {
if (other.firstName != null)
return false;
} else if (!firstName.equals(other.firstName))
return false;
if (lastName == null) {
if (other.lastName != null)
return false;
} else if (!lastName.equals(other.lastName))
return false;
return true;
}
public Person(String firstName,String lastName){
this.setFirstName(firstName);
this.setLastName(lastName);
}
public String getFirstName() {
return firstName;
}
private void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
private void setLastName(String lastName) {
this.lastName = lastName;
}
}
Test/Main:
ConcurrentHashMap<Integer,Person> storage = new ConcurrentHashMap<Integer, Person>();
storage.put(1, new Person("Sally","Solomon"));
storage.put(2, new Person("Harry","Solomon"));
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton jButton1 = new JButton("Button");
Person [] personArr= storage.values().toArray(new Person[0]);
String [] names = new String[personArr.length];
System.out.println(personArr.length);
for(int i=0; i<personArr.length; i++){
System.out.println(personArr[i].getFirstName());
names[i] = personArr[i].getFirstName() + " " + personArr[i].getLastName();
}
final JList jList1 = new JList(names);
问题:
获取 ConcurrectHashMap 中的值并将它们添加到 JList 的正确方法是什么?我的做法是将所有值读入字符串数组并将其添加到 JList。
- 可以更高效地完成吗?
- 他们这样做是一种安全的方式吗?或者我只是更新存储并将其添加到 JList?
根据 ConcurrentHashMap's documentation:
Similarly, Iterators and Enumerations return elements reflecting the state of the hash table at some point at or since the creation of the iterator/enumeration. They do not throw ConcurrentModificationException. However, iterators are designed to be used by only one thread at a time.
因此,如果您是唯一一个遍历地图的人,那您就很好,即使另一个线程正在更新它。但请注意,在使用 Swing 时,线程安全主要是在访问 GUI 小部件时提到的——它必须从事件调度线程完成。
所以你上面的代码应该是 运行 从 运行nable 传递给 EventQueue#invokeLater
这是我的示例代码:
人Class:
public class Person {
private String firstName;
private String lastName;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((firstName == null) ? 0 : firstName.hashCode());
result = prime * result
+ ((lastName == null) ? 0 : lastName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (firstName == null) {
if (other.firstName != null)
return false;
} else if (!firstName.equals(other.firstName))
return false;
if (lastName == null) {
if (other.lastName != null)
return false;
} else if (!lastName.equals(other.lastName))
return false;
return true;
}
public Person(String firstName,String lastName){
this.setFirstName(firstName);
this.setLastName(lastName);
}
public String getFirstName() {
return firstName;
}
private void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
private void setLastName(String lastName) {
this.lastName = lastName;
}
}
Test/Main:
ConcurrentHashMap<Integer,Person> storage = new ConcurrentHashMap<Integer, Person>();
storage.put(1, new Person("Sally","Solomon"));
storage.put(2, new Person("Harry","Solomon"));
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton jButton1 = new JButton("Button");
Person [] personArr= storage.values().toArray(new Person[0]);
String [] names = new String[personArr.length];
System.out.println(personArr.length);
for(int i=0; i<personArr.length; i++){
System.out.println(personArr[i].getFirstName());
names[i] = personArr[i].getFirstName() + " " + personArr[i].getLastName();
}
final JList jList1 = new JList(names);
问题: 获取 ConcurrectHashMap 中的值并将它们添加到 JList 的正确方法是什么?我的做法是将所有值读入字符串数组并将其添加到 JList。
- 可以更高效地完成吗?
- 他们这样做是一种安全的方式吗?或者我只是更新存储并将其添加到 JList?
根据 ConcurrentHashMap's documentation:
Similarly, Iterators and Enumerations return elements reflecting the state of the hash table at some point at or since the creation of the iterator/enumeration. They do not throw ConcurrentModificationException. However, iterators are designed to be used by only one thread at a time.
因此,如果您是唯一一个遍历地图的人,那您就很好,即使另一个线程正在更新它。但请注意,在使用 Swing 时,线程安全主要是在访问 GUI 小部件时提到的——它必须从事件调度线程完成。 所以你上面的代码应该是 运行 从 运行nable 传递给 EventQueue#invokeLater