集合体系:
Collection集合
1.collection集合的常用方法:
public static void main(String[] args){
//创建集合对象
Collection<String> c = new ArrayList<String>();
System.out.println(c.add("hello"));
System.out.println(c.add("world"));
System.out.println(c);
c.clear();
}
1. 添加一个元素
boolean add(E e)
2. 删除一个元素
boolean remove(Object o)
3. 判断集合是否包含指定的元素
boolean contains(Object o)
4. 判断集合是否为空(集合中没有元素)
boolean isEmpty()
5. 获取集合的长度
int size()
6. 清空集合中的元素
void clear()
2.集合的迭代:
public static void main(String[] args){
Collection<String> c = new ArrayList<String>();
c.add("hello");
c.add("world");
c.add("java");
Iterator<String> it = c.iterator();
while(it.hasNext()){//hasnext()判断是否还有下一个元素
System.out.println(it.next());//输出元素
}
Iterator迭代器:
1.返回下一个元素
E next()
2.判断下一个元素是否存在,如果存在,返回true
boolean hasNext()
3.collection集合存储学生对象并迭代
public class Student{
private String name;
private int age;
public Student(String name,int age){
this.name=name;
this.age=age;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
}
//首先导包
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class CollectionDemo{
public static void main(String[] args){
Collection<Student> c = new ArrayList<Student>();
Student s1 = new Student("zzz",20);
Student s2 = new Student("qqq",20);
Student s3 = new Student("fff",19);
Student s4 = new Student("jjj",21);
c.add(s1);
c.add(s2);
c.add(s3);
c.add(s4);
Iterator<Student> it = c.iterator();
while(it.hasNext()){
Student s = it.next();
System.out.println("名字是:"+s.getName()+",年龄为:"+s.getAge());
}
}
}
注意:s1,s2,s3,s4处的名字需要用" "来写,’ '我的idea上面报错。
List集合
List集合特点:有序,可重复,List集合继承自Collection集合
1.简单代码:
public static void main(String[] args){
List<String> list = new ArrayList<String>();
list.add("hello");
list.add("world");
list.add("java");
list.add("hello");
Iterator<String> it = list.iterator();
while(it.hasNext()){
String s = it.next();
System.out.println(s);
}
}
2.List集合特有方法:
1.向指定索引处添加元素,在此元素后面的元素,向后移动1位
list.add(int index,E element)
2.删除指定索引处元素
list.remove(int index)
3。:修改索引处元素
list.set(int index,E element)
4.得到索引处元素
list.get(int index)
3.增强for
public static void main(String[] args){
List<String> list = new ArrayList<String>();
list.add("hello");
list.add("world");
list.add("java");
//内部是一个迭代器
for(String s:list){
System.out.print(s);
}
}
4.List集合的常用子类:ArrayList,LinkedList
ArrayList是数组(查询快,增删慢)
LinkedList是链表 (查询慢,增删快)
5.LinkedList特有的功能:
import java.util.LinkedList;
public static void main(String[] args){
LinkedList<String> lilist = new LinkedList<String>();
lilist.add("hello");
lilist.add("world");
lilist.add("java");
//得到第一个和最后一个
System.out.println(lilist.getFirst());
System.out.println(lilist.getLast());
//删除第一个和最后一个
System.out.println(lilist.removeFirst());
System.out.println(lilist.removeLast());
}
Set集合
Set集合特点:无序,不重复的
1.Set的简单使用
import java.util.HashSet;
public static void main(String[] args){
Set<String> set = new HashSet<String>();
set.add("hello");
set.add("world");
set.add("java");
set.add("world");//不会重复world
for(String s:set){
System.out.println(s);
}
}
2.hashCode()的特点
public static void main(String[] args){
Student s = new Student();
System.out.println(s.hashCode());
System.out.println(s.hashCode());
System.out.println("---------------");
Student s1 = new Student("zqy",20);
System.out.println(s1.hashCode());
System.out.println("-----------------");
System.out.println("hello".hashCode());
System.out.println("world".hashCode());
System.out.println("java".hashCode());
System.out.println("-----------------");
System.out.println("通话".hashCode());
System.out.println("重地".hashCode());
}
得出结论:
hashCode()在不同对象上表示的哈希值不同
在字符串重写之后哈希值表示的值可以相同(上面的两个是特例,正好这两个的哈希值是相同的)
4.HashSet集合的应用
import java.util.HashSet;
public static void main(String[] args){
HashSet<String> hs = new HashSet<String>();
hs.add("hello");
hs.add("world");
hs.add("java");
for(String s:hs){
System.out.println(s);
}
}
5.HashSet存储学生对象:
public static void main(String[] args){
HashSet<Student> hs = new HashSet<Student>();
Student s1 = new Student("zqy",20);
Student s2 = new Student("lww",19);
Stuednt s3 = new Student("zfy",20);
Student s4 = new Student("zqy",20);
hs.add(s1);
hs.add(s2);
hs.add(s3);
hs.add(s4);
for(Student s : hs){
SYstem.out.println("名字是:"+s.getName+",年龄为:"+s.getAge())
}
}
s4和s1内容一样,需要在Student类中重写equals()和hashCode()方法
6.LinkedHashSet的特点:
链表保证了元素的有序性,哈希表保证了元素唯一。
import java.util.LinkedHashSet;
public static void main(String[] args){
LinkedHashSet<String> lhs = new LinkedHashSet<String>();
lhs.add("hello");
lhs.add("world");
lhs.add("java");
System.out.println(lhs);
}
注:输出的结果是按照顺序依次输出的。
7.TreeSet集合
分为无参时自然排序,有参使按照比较器排序
public static void main(String[] args){
TreeSet<Character> tree = new TreeSet<Character>();
tree.add('d');
tree.add('b');
tree.add('c');
tree.add('e');
tree.add('a');
tree.add('a');
for(Character c: tree){
System.out.println(c);
}
}
注:结果显示的是abcde,按照自然排序实现的
如果将Character改为char类型会报错,基本数据类型需要用包装类型。
存储学生类:
public class TreeSetDemo02 {
public static void main(String[] args){
TreeSet<Student> tree = new TreeSet<Student>();
Student s1 = new Student("jiahui",28);
Student s2 = new Student("xiaozhuzhu",16);
Student s3 = new Student("daidai",40);
Student s4 = new Student("charenqiang",30);
Student s5 = new Student("char",30);
tree.add(s1);
tree.add(s2);
tree.add(s3);
tree.add(s4);
tree.add(s5);
for(Student s: tree){
System.out.println("名字是:"+s.getName()+","+"年龄为:"+s.getAge());
}
}
}
public class Student implements Comparable<Student> {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
@Override
public int compareTo(Student s) {
//this能够代表接下来进来的学生,s表示当前已经的学生,两者进行比较判断顺序
int num = this.age - s.age;
System.out.println(num);
// return 0;//说明元素重复,不会添加新元素
// return -1;
int num1 = num == 0 ? this.name.compareTo(s.name) : num;//如果年龄相同,按照姓名
return num1;
}
}
后面重写的内容没有看懂。
return 正数 表示按照顺序来输出
return 负数 表示按照倒的顺序来输出