set集合基础,练习练习
1.请说明set接口的特点?
set接口的特点:
①set集合内的元素不允许重复
②无序,元素没有下标,不能用普通for循环遍历
2.set接口的实现类有哪些?他们的底层实现是什么?分别有什么特点?
1.set接口实现类有:
①HashSet ,HashSet 有个子类LinkedHasSet类
②TreeSet
2.HashSet他的底层实现是HasMap
TreeSet底层实现是TreeMap
3.HashSet是无序,不带索引,对迭代顺序不做任何保证,元素不可重复;
TreeSet是有序的,不是存储有序,而是根据元素的自然排序而排序,不包含重复元素。
3.TreeSet的排序方式有哪两种?
自然排序(comparable),比较器排序(comparator);
4.请将如下4个字符串数据[“aaa”,“bbb”,“ccc”,“ddd”],依次添加到HashSet集合中,并遍历查看存储结果。(使用增强for循环遍历)
public class Work7_Set_0_4 {
public static void main(String[] args) {
//创建HasSet集合对象
Set<String> set = new HashSet<String>();
//添加4个字符串数据["aaa","bbb","ccc","ddd"]
set.add("aaa");
set.add("bbb");
set.add("ccc");
set.add("ddd");
//(使用增强for循环遍历)
for (String s : set){
System.out.println(s);
}
}
}
输出结果:
aaa
ccc
bbb
ddd
5.在List集合内去除重复数字值,要求尽量简单(可借助其他集合!!)
public class Work7_Set_0_5 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(4);
//可借助其他集合
Set<Integer> set = new HashSet<>();
set.addAll(list);
System.out.println(set);
}
}
输出结果:
[1, 2, 3, 4]
6.2019年1月份的世界编程语言排行榜从高到低依次如下: Java、C、Python、C++、Visual Basic .NET、JavaScript… 请将以上语言名称作为字符串元素,按顺序存入set集合中,并遍历查看。要求存储和遍历的顺序保持一致。
public class Work7_Set_0_6 {
public static void main(String[] args) {
Set<String> set = new LinkedHashSet<String>();
set.add("Java");
set.add("C");
set.add("Python");
set.add("C++");
set.add("Visual Basic.NET");
set.add("JavaScript...");
for (String s : set){
System.out.println(s);
}
}
}
输出结果:
Java C Python C++ Visual Basic.NET JavaScript…
7.现有若干图书信息(包含名称title、作者author、定价price)需要存储到set集合中,保证集合中无重复元素,并遍历查看。可以认为所有信息都相同的图书为重复数据。
图书实体类:
public class Library {
/**
* 图书标题
*/
private String title;
/**
* 图书作者
*/
private String author;
/**
* 图书价格
*/
private int price;
public Library() {
}
public Library(String title, String author, int price) {
this.title = title;
this.author = author;
this.price = price;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "title=" + title +
", author=" + author +
", price=" + price ;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Library library = (Library) o;
if (price != library.price) return false;
if (title != null ? !title.equals(library.title) : library.title != null) return false;
return author != null ? author.equals(library.author) : library.author == null;
}
@Override
public int hashCode() {
int result = title != null ? title.hashCode() : 0;
result = 31 * result + (author != null ? author.hashCode() : 0);
result = 31 * result + price;
return result;
}
}
测试类:
public class Work7_Set_0_7 {
public static void main(String[] args) {
Set<Library> set = new HashSet<>();
Library library1 = new Library("语文","鲁迅",20);
Library library2 = new Library("活着","余华",25);
Library library3 = new Library("恶意","东野圭吾",18);
Library library4 = new Library("活着","余华",25);
set.add(library1);
set.add(library2);
set.add(library3);
set.add(library4);
for (Library library : set){
System.out.println(library);
}
}
}
输出结果:
title=活着, author=余华, price=25
title=恶意, author=东野圭吾, price=18
title=语文, author=鲁迅, price=20
8.在某次考试中,学生的成绩信息如下(公有属性): 姓名(String) 年龄(int) 成绩(int): Tom 20 90; Jerry 22 95; John 20 100; Lily 22 100 ;Lucy 22 90; Kevin 22 90 请分别用Comparable和Comparator两个接口对以上同学的成绩做降序排序,如果成绩一样,那在成绩排序的基础上按照年龄由小到大排序,成绩和年龄都一样,则按照姓名的字典顺序排序。
①Comparable方式接口对以上同学的成绩做降序排序:
学生类:
public class Student implements Comparable<Student>{
/**
* 学生姓名
*/
private String name;
/**
* 学生年龄
*/
private int age;
/**
* 学生成绩
*/
private int score;
public Student() {
}
public Student(String name, int age, int score) {
this.name = name;
this.age = age;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "name='" + name +
" age=" + age +
" score=" + score;
}
//如果成绩一样,那在成绩排序的基础上按照年龄由小到大排序,
// 成绩和年龄都一样,则按照姓名的字典顺序排序。
@Override
public int compareTo(Student o) {
int num = this.score - o.score;
int num1 = num == 0 ? this.age - o.age : num;
int num2 = num1 == 0 ? this.name.compareTo(o.name) : num1;
return num2;
// return this.score - o.score == this.age - o.age ? this.name.compareTo(o.name) : (this.score - o.score == 0 ? this.age - o.age : this.score - o.score );
}
}
测试类:
public class Work7_Set_0_8 {
public static void main(String[] args) {
//Comparable方式接口对以上同学的成绩做降序排序
//创建TreeSet集合对象
Set<Student> treeSet = new TreeSet<Student>();
//创建学生对象
treeSet.add(new Student("Tom",20,90));
treeSet.add(new Student("Jerry",22,95));
treeSet.add(new Student("John",20,100));
treeSet.add(new Student("Lily",22,100));
treeSet.add(new Student("Lucy",22,90));
treeSet.add(new Student("Kevin",22,90));
//遍历数组
for (Student student : treeSet){
System.out.println(student);
}
}
}
输出结果:
name='Tom age=20 score=90
name='Kevin age=22 score=90
name='Lucy age=22 score=90
name='Jerry age=22 score=95
name='John age=20 score=100
name='Lily age=22 score=100
②Comparator两个接口对以上同学的成绩做降序排序
学生类:
public class Student{
/**
* 学生姓名
*/
private String name;
/**
* 学生年龄
*/
private int age;
/**
* 学生成绩
*/
private int score;
public Student() {
}
public Student(String name, int age, int score) {
this.name = name;
this.age = age;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "name='" + name +
" age=" + age +
" score=" + score;
}
}
测试类:
public class Work7_Set_0_8 {
public static void main(String[] args) {
//Comparator方式接口对以上同学的成绩做降序排序
//创建TreeSet集合对象
Set<Student> treeSet = new TreeSet<Student>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int num = o1.getScore() - o2.getScore();
int num1 = num == 0 ? o1.getAge() - o2.getAge() : num;
int num2 = num1 == 0 ? o1.getName().compareTo(o2.getName()) : num1;
return num2;
}
});
//创建学生对象
treeSet.add(new Student("Tom",20,90));
treeSet.add(new Student("Jerry",22,95));
treeSet.add(new Student("John",20,100));
treeSet.add(new Student("Lily",22,100));
treeSet.add(new Student("Lucy",22,90));
treeSet.add(new Student("Kevin",22,90));
//遍历数组
for (Student student : treeSet){
System.out.println(student);
}
}
}
输出结果:
name='Tom age=20 score=90
name='Kevin age=22 score=90
name='Lucy age=22 score=90
name='Jerry age=22 score=95
name='John age=20 score=100
name='Lily age=22 score=100