Set <接口>
HashSet
特点 : 无序 无下标 不重复
public static void fun1() {
// 使用HashSet 主要用来 去重
// 创建一个HashSet
HashSet<String> set = new HashSet<>()
// 添加 d d 看看效果
boolean b1 = set .add ("d" )
boolean b2 = set .add ("d" )
set .add ("a" )
set .add ("a" )
set .add ("b" )
set .add ("b" )
set .add ("c" )
set .add ("c" )
// 有序 怎么存 怎么打印出来
System.out .println (b1)
System.out .println (b2)
System.out .println (set )
}
public static void fun2 () {
HashSet<Person> set = new HashSet<>();
set .add(new Person("张三" , 12 ));
set .add(new Person("张三" , 12 ));
set .add(new Person("李四" , 13 ));
set .add(new Person("李四" , 13 ));
set .add(new Person("王五" , 14 ));
set .add(new Person("王五" , 14 ));
for (Person person : set ) {
System.out .println(person);
}
}
public static void fun3() {
// linkedHashSet
// 特点: 有序 怎么存怎么取
// 字符串为什么直接放进set 中能去重 ?
LinkedHashSet<String> set = new LinkedHashSet<>();
set .add ("l" );
set .add ("w" );
set .add ("w" );
set .add ("a" );
set .add ("n" );
set .add ("g" );
System.out.println(set );
}
public static void fun4 () {
LinkedHashSet<Integer> set = new LinkedHashSet<>();
while (set .size() < 10 ) {
int num = (int )(Math.random() * (20 - 1 + 1 ) + 1 );
set .add(num);
}
System.out .println(set );
}
public static void fun5 () {
System.out .println("请输入一个字符串" );
Scanner scanner = new Scanner(System.in );
String string = scanner.nextLine();
LinkedHashSet<Character> set = new LinkedHashSet<>();
char [] charArray = string .toCharArray();
for (int i = 0 ; i < charArray.length; i++) {
set .add(charArray[i]);
}
System.out .println(set );
}
// 利用set 集合 去除ArrayList集合中的重复元素 a a b b c c d d
// (操作原ArrayList)
ArrayList<String> arrayList = new ArrayList<>()
arrayList.add ("a" )
arrayList.add ("a" )
arrayList.add ("b" )
arrayList.add ("b" )
arrayList.add ("c" )
arrayList.add ("c" )
arrayList.add ("d" )
arrayList.add ("d" )
LinkedHashSet<String> set = new LinkedHashSet<>()
// 将list中所有元素 扔到set 中去重
set .addAll (arrayList)
// 请控原arraylist
arrayList.clear ()
// 再把set 中的所有元素 扔arraylist 中
arrayList.addAll (set )
System.out .println (arrayList)
TreeSet (内部实现 二叉树)
无序 不重复 无下标
主要作用: 用来排序
public static void fun1 () {
TreeSet<Integer> set = new TreeSet<>();
set .add(1 );
set .add(13 );
set .add(1 );
set .add(2 );
set .add(3 );
for (Integer integer : set ) {
System.out .println(integer);
}
}
public static void fun2 () {
TreeSet<Worker> set = new TreeSet<>();
set .add(new Worker("张三" , 23 ));
set .add(new Worker("李四" , 13 ));
set .add(new Worker("王五" , 43 ));
set .add(new Worker("赵六" , 33 ));
for (Worker worker : set ) {
System.out .println(worker);
}
System.out .println((int )'张' );
}
class KeepStringComImpl implements Comparator <String >{
@Override
public int compare(String o1, String o2) {
int length = o1.length() - o2.length();
int num = length == 0 ? o1.compareTo(o2) : length;
return num == 0 ? 1 : num;
}
}
public static void fun3 () {
TreeSet<String> set = new TreeSet<>(new KeepStringComImpl());
set .add("asd" );
set .add("asde" );
set .add("asdef" );
set .add("a" );
set .add("as" );
System.out .println(set );
}
class KeepString implements Comparator <Character >{
@Override
public int compare(Character o1, Character o2) {
int num = o1.compareTo(o2);
return num == 0 ? 1 : num;
}
}
public static void fun4 () {
System.out .println("请输入一个字符串" );
Scanner scanner = new Scanner(System.in );
String nextLine = scanner.nextLine();
TreeSet<Character> set = new TreeSet<>(new KeepString());
char [] charArray = nextLine.toCharArray();
for (int i = 0 ; i < charArray.length; i++) {
set .add(charArray[i]);
}
System.out .println(set );
}
class KeepInt implements Comparator <Integer >{
@Override
public int compare(Integer o1, Integer o2) {
int num = o1 - o2;
return -(num == 0 ? 1 : num);
}
}
public static void fun5 () {
System.out .println("请输入整数" );
Scanner scanner = new Scanner(System.in );
TreeSet<Integer> treeSet = new TreeSet<>(new KeepInt());
while (true ) {
String string = scanner.nextLine();
if (string .equals("quit" )) {
break ;
}
int num = Integer.parseInt(string );
treeSet.add(num);
}
System.out .println(treeSet);
}
Scanner scanner = new Scanner(System.in );
TreeSet<Student> set = new TreeSet<>();
while (set .size() < 5 ) {
System.out .println("请输入学生信息格式 " + "(姓名,语文成绩,数学成绩,英语成绩)" );
String string = scanner.nextLine();
String[] strings = string .split("," );
int chinese = Integer.parseInt(strings[1 ]);
int math = Integer.parseInt(strings[2 ]);
int english = Integer.parseInt(strings[3 ]);
Student student = new Student(strings[0 ], chinese, math, english);
set .add(student);
}
System.out .println(set );
@Override
public int compareTo (Worker o) {
int s = this .getName().length() - o.getName().length();
int num = this .getAge() - o.getAge();
return s == 0 ? num == 0 ? this .getName().compareTo(o.getName()) : num : s ;
}
系统重写的equals方法
@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 (age != other.age)
return false ;
if (name == null ) {
if (other.name != null )
return false ;
} else if (!name.equals(other.name))
return false ;
return true ;
}