想对LinkedHashSet 排序 应该用什么函数 Collections.sort为什么没用?

livehejie 2008-03-11 09:29:52

LinkedHashSet lhs1 = new LinkedHashSet();
lhs1.add("f");
lhs1.add("n");
lhs1.add("m");
lhs1.add("t");
lhs1.add("p");
lhs1.add("5");
lhs1.add("o");
lhs1.add("q");
lhs1.add("5");
System.out.println("lhs1 is LinkedHashSet"+lhs1);
//Collections.sort(lhs1);




...全文
1203 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
caiming250 2008-03-11
  • 打赏
  • 举报
回复
LinkedHashSet<String> lhs1 = new LinkedHashSet<String>();
lhs1.add("f");
lhs1.add("n");
lhs1.add("m");
lhs1.add("t");
lhs1.add("p");
lhs1.add("5");
lhs1.add("o");
lhs1.add("q");
lhs1.add("5");

String[] strs = lhs1.toArray(new String[lhs1.size()]);
Arrays.sort(strs);
lhs1 = new LinkedHashSet<String>();
Collections.addAll(lhs1, strs);

Iterator<String> it = lhs1.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
wu364241846 2008-03-11
  • 打赏
  • 举报
回复
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
public class T {
public static void main(String []args) {
// TODO Auto-generated method stub

List lhs1 = new ArrayList();
//inkedHashSet lhs1 = new LinkedHashSet();
lhs1.add("f");
lhs1.add("n");
lhs1.add("m");
lhs1.add("t");
lhs1.add("p");
lhs1.add("5");
lhs1.add("o");
lhs1.add("q");
lhs1.add("5");

Collections.sort(lhs1);
System.out.println("lhs1 is LinkedHashSet"+lhs1);

}
}
  • 打赏
  • 举报
回复
Collections.sort() 你可以去看看方法签名,只允许 List 类型的

Set 的话,用 TreeSet 再重新构造一下:

public class Test {

public static void main(String[] args) {
Set<Integer> set = new LinkedHashSet<Integer>();
set.add(4);
set.add(2);
set.add(3);
Set<Integer> set1 = new TreeSet<Integer>(set);
for(Integer a : set1) {
System.out.println(a);
}
}
}
livehejie 2008-03-11
  • 打赏
  • 举报
回复
Collections.sort()函数 用不了 报错的 你试过吗??
nihuajie05 2008-03-11
  • 打赏
  • 举报
回复
另外,你不是应该先Collections.sort()再打印,写在后头有啥意义?
临远 2008-03-11
  • 打赏
  • 举报
回复
你直接用TreeSet不就自动排序了吗?
另外,你不是应该先Collections.sort()再打印,写在后头有啥意义?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧