用TreeSet对指定字符串数组内容进行排序(1)

来源:互联网 发布:ipad最实用软件 编辑:程序博客网 时间:2024/05/12 15:54
package test_set_map;import java.util.Iterator;import java.util.TreeSet;public class Test_Tree {/** * @param args *  *            String demos[]={"hello","donghongyu","test","中国"};//排序 */public static void main(String[] args) {// TODO Auto-generated method stubString demos[] = { "chun", "hello", "test", "中国", "test" };// 与set相同不能有重复元素TreeSet<String> str = new TreeSet<String>();for (int i = 0; i < demos.length; i++) {str.add(demos[i]);// 循环遍历并添加到TreeSet集合中}Iterator<String> itr = str.iterator();if (str.comparator() != null) {System.out.println("不是自然排序");}while (itr.hasNext()) {System.out.println(itr.next());}}}