List与set区别:
1.List:有序可重复。有序指:按照新增的顺序存储。可重复:存储元素的值可以重复。
2.set:无序不可重复。录入的顺序和存储的顺序不一致。不可重复:相同值的元素不可重复存储。
一:使用set构建集合,并赋值读取。
@Test
public void test0(){
Set set = new HashSet();
byte a = 1;
short b = 2;
int c = 3;
long d = 4L;
double e = 5.0d;
float f = 6.0f;
char g = 'a';
boolean h = false;
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);
System.out.println("该集合中有"+set.size()+"个元素");
System.out.println(set);
}
结果为:
二:迭代器与foreach两种方式遍历用set写法构建的集合。
1.foreach遍历用set写法构建的集合。(因为set创建的集合没有下标,所以无序,所以只能foreach遍历)
@Test
public void test02() {
Set<String> set = new HashSet<>();
for (int i = 0; i < 5; i++) {
set.add("浩楠" + (i + 1));
}
System.out.println(set);
for (String name : set) {
System.out.println(name);
}
}
结果为:
2. 迭代器遍历用set写法构建的集合。
@Test
public void test03(){
Set<String> set = new HashSet<>();
for (int i = 0; i < 5; i++) {
set.add("浩楠" + (i + 1));
}
System.out.println(set);
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String name = it.next();
System.out.println(name);
}
}
结果为:
三: HashSet是创建无序的集合,那么有有序的集合吗?
答案是有,如下(使用LinkedHashSet创建集合):LinkedHashSet是HashSet的子类
@Test
public void test04(){
Set set = new LinkedHashSet();
byte a = 1;
short b = 2;
int c = 3;
long d = 4L;
double e = 5.0d;
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);
System.out.println("该集合中有" + set.size() + "个元素");
System.out.println(set);
}
结果是:
四:除了HashSet能创建集合外,TreeSet也能创建集合
但TreeSet有点特殊,它可以自定义集合内元素的排序规则。
例如:我们先创建一个Book类,标明属性(正着排序)
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Book implements Comparable{
private String name;
private String author;
private int price;
private int sales;
@Override
public int compareTo(Object o) {
return 1;
}
}
再写一个测试类:
@Test
public void test05(){
Set<Book> books = new TreeSet<>();
books.add(new Book("一步之遥", "姜文", 35, 180));
books.add(new Book("浮城谜事", "娄烨", 35, 150));
books.add(new Book("邪不压正", "姜文", 35, 150));
books.add(new Book("推拿", "娄烨", 33, 150));
books.add(new Book("让子弹飞", "姜文", 33, 150));
books.add(new Book("颐和园", "娄烨", 35, 180));
books.add(new Book("一步之遥", "姜文", 35, 180));
Iterator<Book> it = books.iterator();
while (it.hasNext()){
Book book = it.next();
System.out.println(book);
}
}
结果是:
我们先创建一个Book类,标明属性(倒着排序)
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Book implements Comparable{
private String name;
private String author;
private int price;
private int sales;
@Override
public int compareTo(Object o) {
return -1;
}
}
写一个测试类:
@Test
public void test05(){
Set<Book> books = new TreeSet<>();
books.add(new Book("一步之遥", "姜文", 35, 180));
books.add(new Book("浮城谜事", "娄烨", 35, 150));
books.add(new Book("一步之遥", "姜文", 35, 180));
books.add(new Book("邪不压正", "姜文", 35, 150));
books.add(new Book("推拿", "娄烨", 33, 150));
books.add(new Book("让子弹飞", "姜文", 33, 150));
books.add(new Book("颐和园", "娄烨", 35, 180));
Iterator<Book> it = books.iterator();
while (it.hasNext()){
Book book = it.next();
System.out.println(book);
}
}
结果是:
不难发现:return值为正数,正着排序; return值为负数,倒着排序。
自定义排序:
我们先创建一个Book类,标明属性(自定义排序)
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Book implements Comparable{
private String name;
private String author;
private int price;
private int sales;
@Override
public int compareTo(Object o) {
Book book = (Book) o;
int x = this.author.compareTo(book.getAuthor());
int y = this.price - book.getPrice();
int z = this.sales - book.getSales();
int k = this.name.compareTo(book.getName());
if (x != 0) {
return x;
} else if (y != 0) {
return y;
} else if (z != 0) {
return z;
} else if (k != 0) {
return k;
} else {
return 0;
}
}
}
测试:
@Test
public void test05(){
Set<Book> books = new TreeSet<>();
books.add(new Book("一步之遥", "姜文", 35, 180));
books.add(new Book("浮城谜事", "娄烨", 35, 150));
books.add(new Book("一步之遥", "姜文", 35, 180));
books.add(new Book("邪不压正", "姜文", 35, 150));
books.add(new Book("推拿", "娄烨", 33, 150));
books.add(new Book("让子弹飞", "姜文", 33, 150));
books.add(new Book("颐和园", "娄烨", 35, 180));
Iterator<Book> it = books.iterator();
while (it.hasNext()){
Book book = it.next();
System.out.println(book);
}
}
结果为: