Java集合
set特点
- 可以存储任何类型的值
- 无序且不可重复,无序不会按照新增的顺序存储,不可重复指元素的值不可重复储存
- HashSet:无序不可重复
- LinkedHashSet:有序不可重复,有序指按照新增的顺序储存
set与Lisi
1、HashSet
无序不可重复
public class HasMap {
@Test
public void test01() {
//无序且不可重复,无序不会按照新增的顺序存储,不可重复指元素的值不可重复储存
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 = "你好java";
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);//长度
}
运行:
[6.0, 1, a, 2, 3, 4, 5.0, false, 你好java]
Set遍历
for循环:
@Test
public void test02() {
//没有下标是无序的:所以不能用下标遍历,使用增强for循环,或迭代器
Set<String> set = new<String> HashSet();
for (int i=0;i<5;i++){
set.add("你好"+i);
}
System.out.println(set);
//-----------------------------------------------------------------------
增强for循环遍历:
for(String name:set ){
System.out.println(name);
}
//------------------------------------------------------------------------
迭代器遍历:
//推荐使用迭代器遍历,效率高
//HasSed无序且不可重复
//LinkedHasSed有序不可重复
Iterator<String> ite = set.iterator();
while (ite.hasNext()){
String next = ite.next();
System.out.println(next);
}}}
运行:
[你好1, 你好2, 你好3, 你好4, 你好0]
你好1
你好2
你好3
你好4
你好0
你好1
你好2
你好3
你好4
你好0
2、LinkedHashSet
有序不可重复,按照新增的顺序储存,
public class HasMap {
@Test
public void test01() {
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 = false;
String i = "你好java";
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);//长度
}
运行:
[1, 2, 3, 4, 5.0, 6.0, a, false, 你好java]
!3、TreeSet
用compareTo比较排序
/**
* @author xhj
* @version 1.0.0 2022/4/28 下午9:32
*/
@Data
public class Books implements Comparable{
String name;
String author;
int price;
int sales;
public Books(String name, String author, int price, int sales) {
/**
* 书名
*/
this.name = name;
/**
* 作者
*/
this.author = author;
/**
* 单价
*/
this.price = price;
/**
* 销量
*/
this.sales = sales;
}
@Override
public int compareTo(Object o) {
Books books = (Books) o;
int x =this.author.compareTo(books.getAuthor());
int y =this.price-books.getPrice();
int z =this.sales-books.getSales();
int k =this.name.compareTo(books.getName());
if (x != 0){
return x;
}else if(y != 0){
return y;
}else if (z != 0){
return -z;
}else {
return k;
}
}
}
/**
* @author xhj
* @version 1.0.0 2022/4/28 下午9:57
*/
public class Terrset {
@Test
public void test01(){
SortedSet<Books>books=new TreeSet<>();
books.add(new Books("一步之遥","姜文",23,200));
books.add(new Books("二步之遥","姜武",23,201));
books.add(new Books("三步之遥","姜文",33,200));
books.add(new Books("四步之遥","姜武",23,240));
books.add(new Books("五步之遥","姜文",33,100));
Iterator<Books> book = books.iterator();
while (book.hasNext()){
Books book2 = book.next();
System.out.println(book2);
}
}
}
//--------------------------------------------------
运行:
Books(name=一步之遥, author=姜文, price=23, sales=200)
Books(name=三步之遥, author=姜文, price=33, sales=200)
Books(name=五步之遥, author=姜文, price=33, sales=100)
Books(name=四步之遥, author=姜武, price=23, sales=240)
Books(name=二步之遥, author=姜武, price=23, sales=201)