文章目录
一、Collection
方法名 | 使用说明 |
---|---|
boolean add(E e) | 添加元素 |
boolean remove(Object o) | 从集合中移除指定的元素 |
void clear() | 清空集合中的元素 |
boolean contains(Object o) | 判断集合中是否存在指定的元素 |
boolean isEmpty() | 判断集合是否为空 |
int size() | 返回集合长度 |
1.collection(接口)的遍历(迭代器)
迭代器的创建
Collection cl = new ArrayList<>();
Iterator it= cl.iterator();
(1)元素遍历
public static void main(String[] args) {
//Collection接口通过ArrayList实现
Collection<String> cl = new ArrayList<>();
cl.add("head");
cl.add("first");
cl.add("ywq");
Iterator<String> it = cl.iterator();
//第一种遍历
while(it.hasNext()) //判断是否还有下一个元素,返回true或false
{
System.out.println(it.next());
}
//第二种遍历,加强版for(类型 名称:数组名/集合名)
for (String a : cl) {
System.out.println(a);
}
}
(2)对象遍历
public static void main(String[] args) {
stu s1 = new stu("ywq",18);
stu s2 = new stu("lmx",17);
stu s3 = new stu("liuliu",1);
Collection<stu> e = new ArrayList<>();
e.add(s1);
e.add(s2);
e.add(s3);
Iterator<stu> it = e.iterator();
while(it.hasNext())
{
stu s = it.next();
System.out.println(s.getName()+","+s.getAge());
}
}
2.List(接口)
有序集合,可以通过索引插入,删除,修改元素(set与remove与get)
有序指存储和取出顺序一致,与set不同,List允许重复
(1)List的元素添加和遍历与前面Collection一样
(2)(3)List的非迭代器元素遍历:
public static void main(String[] args) {
//List接口通过ArrayList实现
List<String> list = new ArrayList<>();
list.add("head");
list.add("first");
list.add("ywq");
for(int i=0;i<list.size();i++)
{
System.out.println(list.get(i));
}
//或者用加强版for
for(String it:list)
{
System.out.println(it);
}
}
(4)List的对象遍历
public static void main(String[] args) {
stu s1 = new stu("ywq",18);
stu s2 = new stu("lmx",17);
stu s3 = new stu("liuliu",1);
List<stu> e = new ArrayList<>();
e.add(s1);
e.add(s2);
e.add(s3);
Iterator<stu> it = e.iterator();
//第一种遍历:while+迭代器
while(it.hasNext())
{
stu s = it.next();
System.out.println(s.getName()+","+s.getAge());
}
//第二种遍历:for循环
for(int i=0;i<e.size();i++)
{
stu s = e.get(i);
System.out.println(s.getName()+","+s.getAge());
}
//第三种遍历,加强for循环
for (stu a : e) {
System.out.println(a.getName()+","+a.getAge());
}
}
(5)列表迭代器
ListIterator<String> it = cl.listIterator();
//列表迭代器可以变遍历边添加元素,普通迭代器不行
//前往后遍历
while(it.hasNext())
{
System.out.println(it.next());
}
//后往前遍历
while(it.hasPrevious())
{
System.out.println(it.previous());
}
接下来是List的两个子类,ArrayList和LinkedList
ArrayList底层是数组,查询快,增删慢
LinkedList底层是链表,查询慢,增删快
(1).ArrayList
ArrayList的基本使用
ArrayList的应用:图书管理系统

(2).LinkedList
用法与ArrayList及List大致相同
三种遍历:
public static void main(String[] args) {
LinkedList<String> ll = new LinkedList<>();
ll.add("ywq");
ll.add("lmx");
ll.add("love");
for(String it:ll)
{
System.out.println(it);
}
for(int i=0;i<ll.size();i++)
{
System.out.println(ll.get(i));
}
Iterator<String> it = ll.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
LinkedList的特有功能:
3.Set(接口)
1.Set的使用:
不保证顺序一致(通过hash值进行存储)
public static void main(String[] args) {
Set<Integer> set = new HashSet<>();
set.add(100);
set.add(17);
set.add(18);
set.add(100);
for(Integer i:set)
{
System.out.println(i);
}
}
输出:
17
18
100
(1).HashSet
底层结构是哈希表
不保证存入和取出的顺序一致
不允许重复元素且对整型排序
无索引,不能用普通for循环遍历,可用加强版for或者迭代器
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("lmx");
set.add("ywq");
set.add("love");
set.add("ywq");
for(String i:set)
{
System.out.println(i);h
}
}```
输出:
```java
love
ywq
lmx
HashSet存储对象,需要重写equals和Hashcode()
- 在idea中按下fn+alt+ins选中equals和Hashcode()即可自动重写
String name;
int age;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
stu stu = (stu) o;
if (age != stu.age) return false;
return name != null ? name.equals(stu.name) : stu.name == null;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + age;
return result;
}
public static void main(String[] args) {
HashSet<stu> set = new HashSet<>();
stu s1 = new stu("lmx",17);
stu s2 = new stu("ywq",18);
stu s3 = new stu("ywq",18);
set.add(s1);
set.add(s2);
set.add(s3);
for(stu i:set)
{
System.out.println(i.getName()+","+i.getAge());
}
}
输出:
ywq,18
lmx,17
(2).LinkedHashSet
由链表保证存储和取出数据顺序一致
LinkedHashSet中的整型元素是没有排好序的,这两点与HashSet不同
没有重复元素
存储对象时也需要重写equals and hashcode方法
public static void main(String[] args) {
LinkedHashSet<Integer> set = new LinkedHashSet<>();
set.add(100);
set.add(60);
set.add(70);
set.add(100);
for(Integer i:set)
{
System.out.println(i);
}
}
输出:
100
60
70
(3).TreeSet
-
按照一定规则进行排序,取决于它的构造方法
-
无参构造:TreeSet():按照自然排序进行排序
-
带比较器构造:new TreeSet<>(Comparator comparator)
-
不包含重复元素
public static void main(String[] args) {
TreeSet<stu> ts = new TreeSet<>(new Comparator<stu>() {
@Override
public int compare(stu s1, stu s2) {
//return num;
//若s1放前面则是从小到大
//若s2放前面则是从大到小
//判断年龄
int num1 = s1.getAge()-s2.getAge();
//判断名字
int num2 = num1==0? s1.getName().compareTo(s2.getName()):num1;
return num2;
}
});
stu s1 = new stu("lmx",17);
stu s2 = new stu("ywq",18);
stu s3 = new stu("liu",1);
stu s4 = new stu("ywq",18);
ts.add(s1);ts.add(s2);ts.add(s3);ts.add(s4);
for(stu it :ts)
{
System.out.println(it.getName()+","+it.getAge());
}
}
(4).随机数
用hashset存储随机数是不排好序的
public static void main(String[] args) {
Set<Integer> set = new HashSet<>();
Random r = new Random();
while(set.size()<10)
{
int num = r.nextInt(20)+1;
set.add(num);
}
for(Integer a:set)
{
System.out.println(a);
}
}
输出:
16
17
1
2
18
19
3
7
8
11
用TreeSet是排好序的
public static void main(String[] args) {
Set<Integer> set = new TreeSet<>();
Random r = new Random();
while(set.size()<10)
{
int num = r.nextInt(20)+1;
set.add(num);
}
for(Integer a:set)
{
System.out.println(a);
}
}
2
3
5
6
8
9
11
13
16
17