1.总论
1.1 集合的作用
同样作为容器,与数组的区别:长度可变,只能用于存储对象,多种集合类型对应不同的功能。
1.2 Collection
常用方法:
增:boolean add(Object obj);
Boolean add(Collection cl);
删:boolean remove(Object obj);
Boolean removeAll(Collection cl);
Void clear();
判断: .contains(),.containsAll(),isEmpty();
查:int size();
其他:retainAll()取交集 Object toArray();转成数组
主要子类 List 和 Set
|--Collection
|--List 有序,有索引,元素可重复
|--Set 无序,元素不能重复
2.List
List的方法
1.增:add(); addAll();
2.删:remove();
3.改:void set (index ,element)
4.查:Object get(index)
Int indexOf(Object); LastIndexOf();
List subList(from ,to);
迭代器 :Iterator
方法: .hasNext();.next();注:用.next()取到元素以后,最好只用一次。
在迭代器的使用过程中,不要使用集合操作元素,容易出现异常。
ListIterator :List特用迭代器,多了删除等一些方法
List:
|--ArrayList 最常用,底层为数组结构,查询快,增删慢
|--LinkedList 底层为双向链表结构,增删快
|--vector 线程同步,速度慢,被ArrayList取代
LinkedList常用方法:
addFirst()/addLast():开始或末尾添加
peekFirst()/peekLast(); 开始或末尾取出 1.6前为getXX
pollFirst()/Last() ;删除 1.6前为remove
面试题:用LinkedList模拟堆栈功能。主要就是用上述addXX与removeXX方法。
一个删除List重复元素的Demo,用到了ListIterater与一些List方法。
import java.util.*;
/**
* List删除重复元素
* 注:list的contain方法底层也是用的equals方法,本例用的是String,对于其他对象,则重写equals方法。
*/
public class ArrayListRemoveSame {
public static void main(String[] args) {
ArrayList<String> al=new ArrayList<String>();
al.add("fooABC");
al.add("fooABD");
al.add("fooABE");
al.add("fooABD");
al.add("fooABC");
removeSame(al);//调用函数删除
System.out.println(al);
}
<pre name="code" class="java">}
3.Set
Set的方法与Collection基本上完全一样。主要有HashSet,与TreeSet两个子类。
public class Student implements Comparable<Student>{
private String name;
private int age;
public String getName(){
return this.name;
}
public void setName(String name){
this.name=name;
}
public int getAge(){
return this.age;
}
public void setAge(int age){
this.age=age;
}
//构造函数
public Student(){
}
public Student(String name,int age){
this.name=name;
this.age=age;
}
//为了存hashSet重写hashCode与equals
public int hashCode(){
return this.name.hashCode()+this.age*33;
}
public boolean equals(Object b){
if(!(b instanceof Student))
throw new ClassCastException("类型错误");
Student t=(Student)b;
return this.name==t.getName()&&this.age==t.getAge();
}
//为了存二叉树treeSet要写compareTo
public int compareTo(Student st) {
int num=new Integer(this.age).compareTo(st.getAge());
if(num==0){
return this.name.compareTo(st.getName());
}
else return num;
}
@Override
public String toString() {
// TODO 自动生成的方法存根
return "姓名"+this.name+",年龄"+this.age;
}
}
3.1 HashSet
HashSet是Set的典型实现,HashSet按Hash算法来存储集合中的元素,因此具有很好的存取和查找性能。
特点:不能保证元素的排列顺序;不是同步的,不是线程安全;集合值可以是null。
判断元素重复标准:First step用hashCode()判断地址值,Second step用euqals()判断对象内容。所以一般其所存储的对象都要重写这两个方法。
</pre><pre name="code" class="java">public class HashSetDemo {
public static void main(String[] args) {
Set st=new HashSet();
st.add(new Student("asdf",23));
st.add(new Student("asdf",23));
st.add(new Student("asdf1",23));
st.add(new Student("asdf2",23));
System.out.println(st);
}
}
3.2TreeSet
能够实现排序。判断的方法有两种:1.比较器,2.对象implements Compatable接口实现compataTo()方法。
import java.util.*;
public class TreeSetDemo {
public static void main(String[] args) {
//年龄升序
Set st=new TreeSet();
st.add(new Student("asdf",25));
st.add(new Student("bsdf",21));
st.add(new Student("asdf",25));
st.add(new Student("asdf2",23));
System.out.println("升序");
System.out.println(st);
//年龄降序
Set st2=new TreeSet(Collections.reverseOrder());
st2.add(new Student("asdf",25));
st2.add(new Student("bsdf",21));
st2.add(new Student("asdf",25));
st2.add(new Student("asdf2",23));
System.out.println("降序");
System.out.println(st2);
//自定义按姓名比较排序
Set st3=new TreeSet(new StuNameComp());
st3.add(new Student("asdf",23));
st3.add(new Student("bsdf",23));
st3.add(new Student("asdf",23));
st3.add(new Student("asdf2",25));
System.out.println("自定义按姓名排序");
System.out.println(st3);
}
}
class StuNameComp implements Comparator<Student>{//Student类中是按年龄比较,此处构造一个比较器按姓名比较
public int compare(Student a, Student b) {
int num=a.getName().compareTo(b.getName());
if(num==0){
return new Integer(a.getAge()).compareTo(b.getAge());
}
return num;
}
}
本文深入探讨了Java集合框架的核心概念,包括Collection、List、Set及其具体实现如ArrayList、LinkedList、HashSet和TreeSet等。详细解释了每种类型的特性和应用场景,并通过示例代码展示了如何进行操作。
1万+

被折叠的 条评论
为什么被折叠?



