集合中List和Set的区别:
- List:有序可重复,有序指的是下标有序 、可重复指的是不同下标位置上可以存储相同值的元素。
- Set:无序不可重复,录入的顺序和存储的顺序不一致、不可重复指的是相同值的元素不可重复存储。
@Test
public void test() {
Set set = new HashSet();
byte a = 1;
short b = 2;
int c = 3;
long d = 4l;
double e = 5.0;
float f = 6.0f;
char g = 'a';
boolean h = true;
String i = "路飞";
set.add(a);
set.add(b);
set.add(c);
set.add(d);
set.add(e);
set.add(f);
set.add(g);
set.add(h);
set.add(i);
set.add(a);
System.out.println("该集合长度为 >>> " + set.size());
System.out.println(set);
}
我们发现set创建的集合无序所以无法使用下标读取元素,那么遍历元素应该选择加强for循环和迭代器遍历,如下:
@Test
public void test01(){
Set set = new HashSet();
//创建一个String类型的集合
for (int i = 0; i < 10; i++) {
set.add("路飞"+i);
}
//使用for each遍历集合
for (String name:set
) {
System.out.print(name + "t");
}
}
@Test
public void test02(){
Set set = new HashSet();
//创建一个String类型的集合
for (int i = 0; i < 10; i++) {
set.add("索隆"+i);
}
//使用迭代器遍历集合
Iterator it = set.iterator();
while(it.hasNext()){
String next = it.next();
System.out.print(next + "t");
}
}
既然HashSet是创建无序的集合,那么就会有有序的集合,如下(使用LinkedHashSet):
@Test
public void test() {
Set set = new LinkedHashSet();
byte a = 1;
short b = 2;
int c = 3;
long d = 4l;
double e = 5.0;
float f = 6.0f;
char g = 'a';
boolean h = true;
String i = "路飞";
set.add(a);
set.add(b);
set.add(c);
set.add(d);
set.add(e);
set.add(f);
set.add(g);
set.add(h);
set.add(i);
set.add(a);
System.out.println("该集合长度为 >>> " + set.size());
System.out.println(set);
}
接下来看一下另外一个排序的set使用(TreeSet):
正序:
先创建一个books类:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books implements Comparable{
String name;
String author;
int price;
int sales;
@Override
public int compareTo(Object o) {
Books book = (Books) o;
return 1;
}
}
创建一个books的测试类:
@Test
public void test02(){
Set books = new TreeSet<>();
books.add(new Books("浮城谜事","姜烨",35,150));
books.add(new Books("邪不压正","姜文",35,150));
books.add(new Books("推拿","姜烨",33,150));
books.add(new Books("让子弹飞","姜文",33,150));
books.add(new Books("颐和园","姜烨",35,180));
books.add(new Books("一步之遥","姜文",35,180));
Iterator it = books.iterator();
while(it.hasNext()){
Books next = it.next();
System.out.println(next);
}
}
倒序:
先创建一个books类:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books implements Comparable{
String name;
String author;
int price;
int sales;
@Override
public int compareTo(Object o) {
return -1;
}
}
创建一个books的测试类:
@Test
public void test02(){
Set books = new TreeSet<>();
books.add(new Books("浮城谜事","姜烨",35,150));
books.add(new Books("邪不压正","姜文",35,150));
books.add(new Books("推拿","姜烨",33,150));
books.add(new Books("让子弹飞","姜文",33,150));
books.add(new Books("颐和园","姜烨",35,180));
books.add(new Books("一步之遥","姜文",35,180));
Iterator it = books.iterator();
while(it.hasNext()){
Books next = it.next();
System.out.println(next);
}
}
可见:顺序由正负数而定(正数为正序,负数为倒序)
自定义排序:
先创建一个books类:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books implements Comparable {
String name;
String author;
int price;
int sales;
@Override
public int compareTo(Object o) {
Books book = (Books) o;
int x = this.name.compareTo(book.getName());
int y = this.price - book.getPrice();
int z = this.sales - book.getSales();
int w = this.author.compareTo(book.getAuthor());
if (x != 0) {
return x;
} else if (y != 0) {
return y;
} else if (z != 0) {
return z;
} else if (w != 0) {
return w;
} else {
return 0;
}
}
}
创建一个books的测试类:
@Test
public void test02() {
Set books = new TreeSet<>();
books.add(new Books("浮城谜事", "姜烨", 35, 150));
books.add(new Books("邪不压正", "姜文", 35, 150));
books.add(new Books("推拿", "姜烨", 33, 150));
books.add(new Books("让子弹飞", "姜文", 33, 150));
books.add(new Books("颐和园", "姜烨", 35, 180));
books.add(new Books("一步之遥", "姜文", 35, 180));
Iterator it = books.iterator();
while (it.hasNext()) {
Books next = it.next();
System.out.println(next);
}
}



