将 JList 分为 2 组的优化方法
我有一个用户列表。其中一些用户处于第一状态,而其他用户处于第二状态。所以我想要的是将这个列表显示为首先,它按排序顺序显示存在 = 1 的用户,然后按排序顺序显示存在 = 2 的用户。这里的排序是根据用户名进行的。目前我可以做所有这些事情,但是需要很长时间才能完成,因为列表中有大约 250 个用户。而且用户的存在可以随时更改。我有一个套接字连接来监听它,那时我还必须首先显示列表上的所有内容来显示更新的用户数据。如何才能以更少的时间完成此操作并且不会使我的应用程序挂起?
这是我目前正在做的事情:
List<User> us = new ArrayList<User>();
int num = model[j].getSize();
String[] strArr = new String[num];
for (int i = 0; i < num; i++) {
strArr[i] = ((User)model[j].get(i)).getName();
if(!isDuplicateSortedUser(strArr[i], us))
us.add((User)model[j].get(i));
}
sortArray(Collator.getInstance(), strArr);
User user;
List<User> temp2 = new ArrayList<User>();
List<User> temp1 = new ArrayList<User>();
for (String string : strArr) {
for (int i = 0; i < num; i++) {
user = (User) us.get(i);
if(user.getName().equals(string)){
if(!isDuplicateSortedUser(user.getUserid(), temp2) && !temp2.contains(user) && !temp1.contains(user)){
if(user.getPresence().toLowerCase().equals("1st"))
temp2.add(user);
else
temp1.add(user);
}
}
}
}
int l=0;
for (User user2 : temp1) {
model[j].setElementAt(user2, l);
l++;
}
for (User user2 : temp2) {
model[j].setElementAt(user2, l);
l++;
}
这里 model
是 JList
的 DefaultListModel
。 sortArray 方法是:
private void sortArray(Collator collator, String[] strArray) {
String tmp;
if (strArray.length == 1) return;
for (int i = 0; i < strArray.length; i++) {
for (int j = i + 1; j < strArray.length; j++) {
if(collator.compare(strArray[i], strArray[j] ) > 0 ) {
tmp = strArray[i];
strArray[i] = strArray[j];
strArray[j] = tmp;
}
}
}
}
如何优化上面的代码?
I have a jlist of users. In which some user has presence 1st and while others have 2nd. SO what I want is to display this list as First it shows users with presence=1st in sorted order and then user with presence=2 in sorted order. Here sorting is done based on user's name. Currently I'm able to do all this things but it takes a long to do it as the list has many users around 250. Also the presence of a user can change any time. I have a socket connection to listen to that and that time I also have to above all things on list to show updated user data. How can I do this in a way that takes less time and don't make my application hang?
Here is what I am doing currently:
List<User> us = new ArrayList<User>();
int num = model[j].getSize();
String[] strArr = new String[num];
for (int i = 0; i < num; i++) {
strArr[i] = ((User)model[j].get(i)).getName();
if(!isDuplicateSortedUser(strArr[i], us))
us.add((User)model[j].get(i));
}
sortArray(Collator.getInstance(), strArr);
User user;
List<User> temp2 = new ArrayList<User>();
List<User> temp1 = new ArrayList<User>();
for (String string : strArr) {
for (int i = 0; i < num; i++) {
user = (User) us.get(i);
if(user.getName().equals(string)){
if(!isDuplicateSortedUser(user.getUserid(), temp2) && !temp2.contains(user) && !temp1.contains(user)){
if(user.getPresence().toLowerCase().equals("1st"))
temp2.add(user);
else
temp1.add(user);
}
}
}
}
int l=0;
for (User user2 : temp1) {
model[j].setElementAt(user2, l);
l++;
}
for (User user2 : temp2) {
model[j].setElementAt(user2, l);
l++;
}
Here model
is the DefaultListModel
of JList
. The sortArray method is:
private void sortArray(Collator collator, String[] strArray) {
String tmp;
if (strArray.length == 1) return;
for (int i = 0; i < strArray.length; i++) {
for (int j = i + 1; j < strArray.length; j++) {
if(collator.compare(strArray[i], strArray[j] ) > 0 ) {
tmp = strArray[i];
strArray[i] = strArray[j];
strArray[j] = tmp;
}
}
}
}
How can I optimize above code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将用户放入 ArrayList 中并使用内置的 Java 排序功能: http://download.oracle.com/javase/6/docs/api/java/util/Collections.html
这将调用合并排序,这将比您编写的更有效。
您到底想在第一个 for 循环中做什么?
据我所知,您只需要执行以下操作:
Put the users in an ArrayList and use the built-in Java sort functionality: http://download.oracle.com/javase/6/docs/api/java/util/Collections.html
This will invoke a mergesort, which will be more efficient than what you've written.
What exactly are you trying to do in the first for loop?
As far as I can see, you only need to do the following:
正如 @Jeroen 所指出的,实现 Comparable 是首选方法。您还可以实现
Comparator
,如RecordComparator
或使用SortedComboBoxModel
。As noted by @Jeroen, implementing
Comparable
is the preferred approach. You can also implementComparator
as shown inRecordComparator
or useSortedComboBoxModel
.