Java集合
用于存储数量不等的对象;集合类主要负责保存,盛装其他数据,因此集合类也被称为容器类;
集合类和数组不一样,集合只能保存对象;由两个接口派生而出:Collection和Map;
常用集合的实现类有:HashSet,TreeSet,ArrarList,ArrayDeque,LinkedList,HashMap,TreeMap等;
大致分为 Set,List,Queue,Map四种体系;
Set代表无序,不可重复的集合;
List代表重复,有序的集合;
Map代表具有映射关系的集合;
Queue代表一种队列集合实现。
Collection接口:是List,Set,Queue接口的父接口
常用方法:
boolean add(object o);
boolean addAll(Collection c):
void clear();
boolean contains(object o):
boolean containsAll(Collection c):
boolean isEmpty();
Iterator iterator();
boolean remove(object o);
boolean removeAll(Collection c);//从集合中删除集合c里包含的所有元素,如果删除一个或一个以上的元素,则返回true;
boolean retainAll(Collection c)://从集合中删除集合c里不包含的元素,相当于求交集;
例子:
import java.util.*;
public class CollectionTest {
public static void main(String[] args) {
Collection c = new ArrayList();
c.add("王大锤");
c.add(6);
System.out.println("c集合元素个数为:"+c.size());
c.remove(6);
System.out.println(c.contains("王大锤"));
c.add("Dark");
System.out.println(c);
Collection book = new HashSet();
book.add("Java开发");
book.add("Linux");
System.out.println(c.containsAll(book));
book.clear();
System.out.println(book);
}
}
使用Iterator遍历集合元素
Iterator必须依附于Collection对象;
Iterator对象也被称为迭代器,Iterator接口定义了如下4个方法;
boolean hasNext();
Object next();返回集合里的下一个元素
void remove();删除集合里上一次next方法返回的元素;
void forEachRemaining....
import java.util.*;
public class CollectionTest {
public static void main(String[] args) {
Collection book = new HashSet();
book.add("Java开发");
book.add("Linux");
book.add("Mebefore You");
Iterator it = book.iterator();
while(it.hasNext()){
String b = (String)it.next();
System.out.println(b);
if(b=="Linux")
it.remove();
}
System.out.println(book);
}
}
使用加强for循环遍历集合元素:
mport java.util.*;
public class CollectionTest {
public static void main(String[] args) {
Collection book = new HashSet();
book.add("Java开发");
book.add("Linux");
book.add("Mebefore You");
for(Objecti : book){
String b = (String)i;
System.out.println(b);
}
System.out.println(book);
}
}
Set集合:
无序,不重复,也适合HashSet,TreeSet.
——HashSet类
特点:
1.不能保证元素的排列顺序,顺序可能与添加顺序不同
2.HashSet不是同步的,如果多个线程同时访问一个HashSet,必须通过代码保证其同步
3.集合元素值可以是null
import java.util.*;
//HashSet判断两个元素相等的条件是equals和hashCode的返回值都相等
class A{
public boolean equals(Object obj){
return true;
}
}
class B{
public int hashCode(){
return 1;
}
}
class C{
public int hashCode(){
return 2;
}
public boolean equals(Object obj){
return true;
}
}
public class HashSetTest {
public static void main(String[] args) {
HashSet books = new HashSet();
books.add(newA());
books.add(newA());
books.add(newB());
books.add(newB());
books.add(newC());
books.add(newC());
System.out.println(books); //输出结果中只有一个C
}
}
TreeSet类:
TreeSet类可以确保集合元素处于排序状态,包含方法:
Comparator comparator();
Object first();
Object last();
Object lower(Object e);返回集合中小于指定元素的最大元素
Object higher(Object e);......
import java.util.*;
public class TreeSetTest {
public static void main(String[] args) {
TreeSet nums = new TreeSet();
nums.add(5);
nums.add(10);
nums.add(2);
nums.add(-9);
System.out.println(nums);
System.out.println(nums.first());
System.out.println(nums.last());
}
}
输出结果:
[-9, 2, 5, 10]
-9
10
List集合
List集合代表元素有序,可以重复的集合,集合中每个元素都有与其对应的顺序索引;
与Set集合相比,List增加了根据元素索引来插入,替换和删除集合元素的方法;
作为Collecttion接口的子接口,可以使用Collection接口里的全部方法,此外还增加了一些根据索引来操作集合元素的方法;
void add(int index,Object element);将元素插入到指定的index处;
boolean addAll(int index,Collection c);将集合c的所有元素插入到List集合到index处;
Object get(int index);返回集合index索引处的元素;
int indexOf(Object o);返回对象o在List集合中第一次出现的位置索引;
int lastIndexOf(Object o);返回对象o在List集合中最后一次出现的位置索引;
Object remove(int index);删除并返回index索引处的元素;
Object set(int index,Object element);将index索引处的元素替换成element对象,返回新元素;
List subList(int fromIndex,int toIndex):返回从索引fromIndex(包含)到索引toIndex(不包含)处所有集合元素组成的子集合;
List判断两个对象相等只要通过equals()方法比较返回true即可;
List集合的常规用法:
import java.util.*;
public class ListTest{
public static void main(String[] args){
List books = new ArrayList();
books.add(new String("Linux"));
books.add(new String("Java"));
books.add(new String("C++"));
System.out.println(books);
books.add(1,new String("Python"));
for(int i=1;i<books.size();i++){
System.out.println(books.get(i));
}
books.remove(3);
System.out.println(books);
System.out.println(books.indexOf("Java"));
books.set(1,"C#");
System.out.println(books);
}
}
输出结果:
[Linux, Java, C++]
Python
Java
C++
[Linux, Python, Java]
2
[Linux, C#, Java]
ListIterator
List还提供了一个listIterator()方法,返回一个ListIterator对象,ListIterator接口继承了Iterator接口,提供了专门操作List的方法,在Iterator方法基础上还增加了如下方法:即增加了向前迭代的功能,且还能插入元素,Iterator只能删除元素;
boolean hasPrevious();返回迭代器关联的集合是否还有上一个元素;
Object previous();返回迭代器的上一个元素;
void add();在指定位置插入一个元素;
import java.util.*;
public class ListIteratorTest{
public static void main(String[] args){
String[] books = {"Java","Python","C++"};
List bookList = new ArrayList();
for(int i=0;i<books.length;i++){
bookList.add(books[i]);
}
ListIterator li = bookList.listIterator();
while(li.hasNext()){
System.out.println(li.next());
li.add("-----------------");
}
System.out.println("下面开始反向迭代:");
while(li.hasPrevious()){
System.out.println(li.previous());
}
}
}
输出结果:
Java
Python
C++
下面开始反向迭代:
-----------------
C++
-----------------
Python
-----------------
Java
ArrayList 和 Vector实现类
Vector还提供了一个Stack子类用与模拟“栈”这种数据结构;
固定长度的List
Queue集合
PriorityQueue实现类
import java.util.*;
public class PriorityQueueTest{
public static void main(String[] args){
PriorityQueue pq = new PriorityQueue();
pq.offer(6);
pq.offer(-3);
pq.offer(9);
pq.offer(0);
System.out.println(pq);
System.out.println(pq.poll);
}
}
输出结果:
[-3, 0, 9, 6]
-3
Deque接口与ArrayDque实现类
LinkedList实现类
import java.util.*;
public class LinkedList{
public static void main(String[] args){
LinkedList books = new LinkedList();
books.offer("Java");
books.push("C++");
booss.offerFirst("Andriod");
for(int i=0;i<boos.size();i++){
System.out.println(books.get(i));
}
System.out.println(books.peekFirst());
System.out.println(books.peekLast());
System.out.println(books.pop());
System.out.println(books);
System.out.println(books.pollLast());
System.out.println(books);
}
}
Andriod
C++
Java
Andriod
Java
Andriod
[C++, Java]
Java
[C++]
Map