javaMerea 2019-06-21
package com.test.demo30; import java.util.HashSet; import java.util.Scanner; public class Demo39 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一行字符串"); String str = sc.nextLine(); char[] ch = str.toCharArray(); HashSet<Character> hs = new HashSet(); for (char c : ch) { hs.add(c); } for (char c : hs) { System.out.println(c); //想要打印是按集合的顺序就用LinkedHashSet } } }
package com.test.demo30; import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; public class Demo40 { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(1); list.add(2); list.add(2); list.add(3); list.add(3); list.add(1); list.add(1); list.add(1); list.add(1); System.out.println("去重前"); System.out.println(list); getSingle(list); System.out.println("去重后"); System.out.println(list); } public static <T> void getSingle(List<T> list){ LinkedHashSet<T> lhs = new LinkedHashSet<>(); lhs.addAll(list); list.clear(); list.addAll(lhs); } }
package com.test.demo30; import java.util.TreeSet; public class Demo41 { public static void main(String[] args) { TreeSet<Integer> ts = new TreeSet<>(); ts.add(3); ts.add(3); ts.add(2); ts.add(2); ts.add(3); ts.add(1); ts.add(1); ts.add(3); System.out.println(ts); } }
后面的调用方法进行比较
TreeSet<Person> ts = new TreeSet<>(); ts.add(new Person("张三",23)); ts.add(new Person("张4",23)); ts.add(new Person("张5",23)); ts.add(new Person("张6",23));
public int compareTo(Person o) { int num = this.age - o.age; //年龄是主要排序依据 return num == 0? this.name.compareTo(o.name) : num; //姓名是次要排序依据【姓名之间的比较实际上比较的是unnicode码的值】 }
使用方式:
自然顺序(Comparable)
比较器顺序(Comparator)
两种方式的区别
package com.test.demo30; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.TreeSet; public class Demo42 { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.add("c"); list.add("b"); list.add("a3"); list.add("a3"); list.add("c3"); list.add("c2"); list.add("c2"); list.add("a7"); list.add("a5555"); list.add("a777"); list.add("a777"); list.add("a777"); list.add("a7"); System.out.println(list); sort(list); System.out.println(list); } public static void sort(List<String> list) { TreeSet<String> ts = new TreeSet<>(new Comparator<String>(){ public int compare(String s1,String s2){ int num = s1.compareTo(s2); return num == 0 ? 1:num; //TreeSet对比较器比较后返回为0的值会视为相等,就不会存入集合了,可以利用这一点存入重复的元素 } }); ts.addAll(list); list.clear(); list.addAll(ts); } }
package com.test.demo30; import java.util.Comparator; import java.util.Scanner; import java.util.TreeSet; public class Demo44 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); TreeSet<Integer> ts = new TreeSet<>(new Comparator<Integer>(){ public int compare(Integer i1,Integer i2){ int num = i2.compareTo(i1); return num == 0? 1 : num; } }); while(true){ System.out.println("请输入一个整数:"); String line = sc.nextLine(); if("quit".equals(line)){ break; } else { Integer i =Integer.parseInt(line); ts.add(i); } } for (Integer k : ts) { System.out.println(k); } } }
package com.test.demo555; import java.util.Comparator; import java.util.Scanner; import java.util.TreeSet; public class Demo444 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入学生成绩,格式是:姓名,语文成绩,数学成绩,英语成绩"); TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>(){ public int compare(Student s1,Student s2){ int num = s2.getSum() - s1.getSum(); return num == 0? 1: num; } }); while(ts.size() < 5){ String line = sc.nextLine(); String[] arr = line.split(","); int chinese = Integer.parseInt(arr[1]); int math = Integer.parseInt(arr[2]); int english = Integer.parseInt(arr[3]); ts.add(new Student(arr[0],chinese,math,english)); } System.out.println("排序后的学生信息:"); for (Student s : ts) { System.out.println(s); } } }
叨逼叨两句收拾收拾~18-11:Collection中的常见方法sortpackage com.test.demo001;System.out.println; //根据默认排序结果获取集合中的最大值