1 java 集合类关系图
上述类图中,实线边框是实现类,比如ArrayList,LinkedList,HashMap等,折线边框是抽象类,比如AbstractCollection,AbstractList,AbstractMap等,点线边框是接口,比如Collection,Iterator,List等。
集合类详细内容:http://www.imooc.com/article/1080
2 集合类关系图List、Set、Map对比
接口 | 实现类 | 保持插入顺序 | 可重复 | 排序 | 使用说明 |
List | ArrayList | Y | Y | N | 擅长于随机访问元素;但插入、删除元素较慢(数组特性)。 |
LinkedList | Y | Y | N | 插入、删除元素较快,但随即访问较慢(链表特性)。 | |
Set | HashSet | N | N | N | 使用散列,最快的获取元素方法。 |
TreeSet | N | N | Y | 将元素存储在红-黑树数据结构中。默认为升序。 | |
LinkedHashSet | Y | N | N | 使用散列,同时使用链表来维护元素的插入顺序。 | |
Map | HashMap | N | N | N | 使用散列,提供最快的查找技术。 |
TreeMap | N | N | Y | 默认按照比较结果的升序保存键。 | |
LinkedHashMap | Y | N | N | 按照插入顺序保存键,同时使用散列提高查找速度。 |
更多对比详细内容:https://blog.csdn.net/hzw05103020/article/details/47207787
3 集合和数组的区别
数组长度是固定的,集合长度是可变的。数组中可以存储基本数据类型或者引用数据类型,集合只能存储引用数据类型。数组存储的是同一种类型的元素,集合可以存储不同类型的元素。
4 Collection<E> 接口
public interface Collection<E> extends Iterable<E>{};
public interface Iterable<T>{};
Collection 层次结构中的根接口。Collection 表示一组对象,这些对象也称为Collection 的元素。一些 Collection 允许有重复的元素,而另一些则不允许。一些 Collection 是有序的,而另一些则是无序的。
方法摘要 | |
---|---|
boolean | add(E e) 确保此 collection 包含指定的元素(可选操作)。 |
boolean | addAll(Collection<? extends E> c) 将指定 collection 中的所有元素都添加到此 collection 中(可选操作)。 |
void | clear() 移除此 collection 中的所有元素(可选操作)。 |
boolean | contains(Object o) 如果此 collection 包含指定的元素,则返回 true。 |
boolean | containsAll(Collection<?> c) 如果此 collection 包含指定 collection 中的所有元素,则返回 true。 |
boolean | equals(Object o) 比较此 collection 与指定对象是否相等。 |
int | hashCode() 返回此 collection 的哈希码值。 |
boolean | isEmpty() 如果此 collection 不包含元素,则返回 true。 |
Iterator<E> | iterator() 返回在此 collection 的元素上进行迭代的迭代器。 |
boolean | remove(Object o) 从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。 |
boolean | removeAll(Collection<?> c) 移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。 |
boolean |
返回值表示此collection 是否发生了改变。 |
int | size() 返回此 collection 中的元素数。 |
Object[] | toArray() 返回包含此 collection 中所有元素的数组。 |
代码示例:
public class CollectionTest {
public static void main(String[] args) {
Collection<String> c=new ArrayList<String>();
c.add("hello");
c.add("world");
c.add("java");
Object[] objs=c.toArray();
for (int i = 0; i < objs.length; i++) {
System.out.println(objs[i]);
String s=(String)objs[i];
System.out.println(s+"---"+s.length());
}
Collection<Student> c1=new ArrayList<Student>();
Student s1=new Student("name1",18);
Student s2=new Student("name2",19);
Student s3=new Student("name3",20);
Student s4=new Student("name4",21);
Student s5=new Student("name5",22);
c1.add(s1);
c1.add(s2);
c1.add(s3);
c1.add(s4);
c1.add(s5);
/*public Iterator<E> iterator() {
return new Itr();
}
private class Itr implements Iterator<E> {}
*/
Iterator<Student> it=c1.iterator();
while(it.hasNext()) {
//System.out.println(it.next());
Student s=(Student)it.next();
System.out.println(s.getName()+"---"+s.getAge());
}
for (Iterator<Student> iterator = c1.iterator(); iterator.hasNext();) {
Student ss = (Student) iterator.next();
System.out.println(ss.getName()+"---"+ss.getAge());
}
}
}
5 List<E> 接口
public interface List<E> extends Collection<E>;
有序的 collection(存储顺序和取出顺序一致),元素可重复。
public class ListTest {
public static void main(String[] args) {
List<Student> list=new ArrayList<Student>();
Student s1=new Student("name1",18);
Student s2=new Student("name2",19);
Student s3=new Student("name3",20);
Student s4=new Student("name4",21);
Student s5=new Student("name5",22);
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
Iterator<Student> it=list.iterator();
while(it.hasNext()) {
//System.out.println(it.next());
Student s=(Student)it.next();
//System.out.println(s.getName()+"---"+s.getAge());
}
for (Iterator<Student> iterator = list.iterator(); iterator.hasNext();) {
Student s = (Student) iterator.next();
//System.out.println(s.getName()+"---"+s.getAge());
}
for (int i = 0; i < list.size(); i++) {
Student s=(Student)list.get(i);
//System.out.println(s.getName()+"---"+s.getAge());
}
for(Student s:list) {
//System.out.println(s.getName()+"---"+s.getAge());
}
/*public ListIterator<E> listIterator() {
return new ListItr(0);
}
private class ListItr extends Itr implements ListIterator<E>{}*/
ListIterator<Student> lit=list.listIterator();
while(lit.hasNext()) {
//System.out.println(it.next());
Student s=(Student)lit.next();
//System.out.println(s.getName()+"---"+s.getAge());
}
while(lit.hasPrevious()) {
Student s=(Student)lit.previous();
//System.out.println(s.getName()+"---"+s.getAge());
}
List<String> list1=new ArrayList<String>();
list1.add("hello");
list1.add("world");
list1.add("java");
// Iterator<String> it1=list1.iterator();
// while(it1.hasNext()) {
// //Exception in thread "main" java.util.ConcurrentModificationException
// String s=(String)it1.next();
// if("world".equals(s)) {
// list1.add("android");
// }
// }
// System.out.println(list1);
ListIterator<String> it1=list1.listIterator();
while(it1.hasNext()) {
String s=(String)it1.next();
if("world".equals(s)) {
it1.add("android");
}
}
System.out.println(list1);//[hello, world, android, java]
for (int i = 0; i <list1.size(); i++) {
String s=(String)list1.get(i);
if("world".equals(s)) {
list1.add("android");
}
}
System.out.println(list1);//[hello, world, java, android]
}
}
6 常见的数据结构
栈 、队列、数组、链表、树、哈希表。
数据结构(数组和链表)
List的相关子类特点
ArrayList:
底层数据结构是数组,查询快,增删慢。
线程不安全,效率高。
Vector:
底层数据结构是数组,查询快,增删慢。
线程安全,效率低。
LinkedList:
底层数据结构是链表,查询慢,增删快。
线程不安全,效率高。
7 ArrayList<E>类
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable{};
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {};
public abstract class AbstractCollection<E> implements Collection<E> {};
ArrayList的相关方法
public class ArrayListTest {
public static void main(String[] args) {
ArrayList<String> array=new ArrayList<String>();
array.add("hello");
array.add("world");
array.add("java");
array.add("android");
Iterator<String> it=array.iterator();
while(it.hasNext()) {
String s=(String)it.next();
System.out.println(s);
}
for (int i = 0; i < array.size(); i++) {
String s=(String)array.get(i);
System.out.println(s);
}
ArrayList<Student> list=new ArrayList<Student>();
Student s1=new Student("name1",18);
Student s2=new Student("name2",19);
Student s3=new Student("name3",20);
Student s4=new Student("name4",21);
Student s5=new Student("name5",22);
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
Iterator<Student> its=list.iterator();
while(its.hasNext()) {
Student s=(Student)its.next();
System.out.println(s.getName()+"---"+s.getAge());
}
for (Iterator<Student> iterator = list.iterator(); iterator.hasNext();) {
Student s = (Student) iterator.next();
System.out.println(s.getName()+"---"+s.getAge());
}
for (int i = 0; i < list.size(); i++) {
Student s=(Student)list.get(i);
System.out.println(s.getName()+"---"+s.getAge());
}
for(Student s:list) {
System.out.println(s.getName()+"---"+s.getAge());
}
}
}
ArrayList的简单应用(元素去重和删除指定元素)
public class ArrayListTest {
public static void main(String[] args) {
ArrayList<String> array=new ArrayList<String>();
array.add("hello");
array.add("world");
array.add("world");
array.add("world");
array.add("world");
array.add("world");
array.add("java");
array.add("android");
array.add("world");
array.add("java");
array.add("android");
//dedupLicationOriginal(array);
//dedupLicationNew(array);
//deleteElement(array,"world");
getNumber();
}
/**
* @descripe 产生10个1-20之间不重复的随机数
*/
private static void getNumber() {
int count=0;
ArrayList<Integer>list=new ArrayList<Integer>();
while(count<10) {
int number=new Random().nextInt(20)+1;
if(!list.contains(number)) {
list.add(number);
count++;
}
}
System.out.println(list);
}
/**
* @param list 目标集合
* @param target 需要删除的元素
* @descripe 删除集合中的某个元素
*/
private static void deleteElement(ArrayList<String> list, String target) {
//普通for循环
for (int i = list.size()-1; i >=0; i--) {
String s=list.get(i);
if(target.equals(s)) {
list.remove(i);
}
}
//迭代器
Iterator<String> it = list.iterator();
while(it.hasNext()) {
String s=it.next();
if(target.equals(s)) {
it.remove();
}
}
// 利用CopyOnWriteArrayList
final CopyOnWriteArrayList<String> cowList=new CopyOnWriteArrayList<String>(list);
for(String s:cowList) {
if(target.equals(s)) {
cowList.remove(s);
}
}
System.out.println(list);
}
/**
* @param array 需要去重的集合
* @descripe:在原有集合操作去重
*/
private static void dedupLicationOriginal(ArrayList<String> list) {
for (int i = 0; i < list.size()-1; i++) {
for (int j = i+1; j < list.size(); j++) {
if(list.get(i).equals(list.get(j))) {
list.remove(j);
j--;//集合连续多个重复元素删除后会有替位问题
}
}
}
System.out.println(list);
}
/**
* @param array 需要去重的集合
* @descripe:创建新集合去重
*/
private static void dedupLicationNew(ArrayList<String> list) {
HashSet<String> set = new HashSet<String>();
ArrayList<String> newarray=new ArrayList<String>();
// 增强for循环
for(String s:list) {
if(!newarray.contains(s)) {
newarray.add(s);
}
}
// 普通for循环
for (int i = 0; i < list.size(); i++) {
String s=list.get(i);
if(!newarray.contains(s)) {
newarray.add(s);
}
}
//迭代器
Iterator<String> it=list.iterator();
while(it.hasNext()) {
String s=it.next();
if(!newarray.contains(s)) {
newarray.add(s);
}
}
// Hashset集合去重
for(String s:list) {
if(set.add(s)) {
newarray.add(s);
}
}
System.out.println(newarray);
}
}
ArrayList 源码分析:https://www.kancloud.cn/swg2018/java_collection/542303
https://blog.csdn.net/zxt0601/article/details/77281231
8 Vector<E>类
public class Vector<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable{};
public class VectorTest {
public static void main(String[] args) {
Vector<String> vec=new Vector<String>();
vec.addElement("hello");
vec.addElement("world");
vec.addElement("java");
vec.addElement("android");
for (int i = 0; i < vec.size(); i++) {
String s=(String)vec.elementAt(i);
System.out.println(s);
}
Enumeration<String> en=vec.elements();
while(en.hasMoreElements()) {
String s=(String)en.nextElement();
System.out.println(s);
}
}
}
8 LinkedList<E>类
public class LinkedList<E>extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable{};
public abstract class AbstractSequentialList<E> extends AbstractList<E>{};
public interface Deque<E> extends Queue<E>{};
public interface Queue<E> extends Collection<E>{};
public class LinkedListTest {
public static void main(String[] args) {
MyStack ms=new MyStack();
ms.add("hello");
ms.add("world");
ms.add("java");
ms.add("android");
while(!ms.isEmpty()) {
System.out.println(ms.get());// android java world hello
}
}
}
class MyStack{
private LinkedList<Object> link;
public MyStack() {
link=new LinkedList<Object>();
}
public void add(Object obj) {
link.addFirst(obj);
}
public Object get() {
return link.removeFirst();
}
public boolean isEmpty() {
return link.isEmpty();
}
}
9 泛型以及泛型高级(通配符)
JDK5 之后出现的机制。JDK5常见新特性:自动拆装箱,泛型,增强for循环,静态导入,可变参数,枚举。
泛型类
•把泛型定义在类上
•格式:public class 类名<泛型类型1,…>
•注意:泛型类型必须是引用类型
泛型方法
•把泛型定义在方法上
•格式:public <泛型类型> 返回类型 方法名(泛型类型 .)
泛型接口
•把泛型定义在接口上
•格式:public interface 接口名<泛型类型1…>
/*
* 泛型高级(通配符)
* ?:任意类型,如果没有明确,那么就是Object以及任意的Java类
* ? extends E:向下限定,E及其子类
* ? super E:向上限定,E极其父类
*/
public class GenericDemo {
public static void main(String[] args) {
// 泛型如果明确的写的时候,前后必须一致
Collection<Object> c1 = new ArrayList<Object>();
// Collection<Object> c2 = new ArrayList<Animal>();
// Collection<Object> c3 = new ArrayList<Dog>();
// Collection<Object> c4 = new ArrayList<Cat>();
// ?表示任意的类型都可以
Collection<?> c5 = new ArrayList<Object>();
Collection<?> c6 = new ArrayList<Animal>();
Collection<?> c7 = new ArrayList<Dog>();
Collection<?> c8 = new ArrayList<Cat>();
// ? extends E:向下限定,E及其子类
// Collection<? extends Animal> c9 = new ArrayList<Object>();
Collection<? extends Animal> c10 = new ArrayList<Animal>();
Collection<? extends Animal> c11 = new ArrayList<Dog>();
Collection<? extends Animal> c12 = new ArrayList<Cat>();
// ? super E:向上限定,E极其父类
Collection<? super Animal> c13 = new ArrayList<Object>();
Collection<? super Animal> c14 = new ArrayList<Animal>();
// Collection<? super Animal> c15 = new ArrayList<Dog>();
// Collection<? super Animal> c16 = new ArrayList<Cat>();
}
}
class Animal {
}
class Dog extends Animal {
}
class Cat extends Animal {
}
10 可变参数
JDK5 之后出现的机制。
/*
* 可变参数:定义方法的时候不知道该定义多少个参数
* 格式:
* 修饰符 返回值类型 方法名(数据类型… 变量名){
*
* }
*
* 注意:
* 这里的变量其实是一个数组
* 如果一个方法有可变参数,并且有多个参数,那么,可变参数肯定是最后一个
*
*public static <T> List<T> asList(T... a):把数组转成集合
* 注意事项:
* 虽然可以把数组转成集合,但是集合的长度不能改变。
*
*/
public class ArgsDemo {
public static void main(String[] args) {
int a = 10;
int b = 20;
int result = sum(a, b);
System.out.println("result:" + result);
int c = 30;
result = sum(a, b, c);
System.out.println("result:" + result);
int d = 30;
result = sum(a, b, c, d);
System.out.println("result:" + result);
result = sum(a, b, c, d, 40);
System.out.println("result:" + result);
result = sum(a, b, c, d, 40, 50);
System.out.println("result:" + result);
// 定义一个数组
// String[] strArray = { "hello", "world", "java" };
// List<String> list = Arrays.asList(strArray);
List<String> list = Arrays.asList("hello", "world", "java");
// UnsupportedOperationException
// list.add("javaee");
// UnsupportedOperationException
// list.remove(1);
list.set(1, "javaee");
for (String s : list) {
System.out.println(s);
}
}
public static int sum(int... a) {
// System.out.println(a);
//return 0;
int s = 0;
for(int x : a){
s +=x;
}
return s;
}
// public static int sum(int a, int b, int c, int d) {
// return a + b + c + d;
// }
//
// public static int sum(int a, int b, int c) {
// return a + b + c;
// }
//
// public static int sum(int a, int b) {
// return a + b;
// }
}
10 Set<E> 接口
public interface Set<E> extends Collection<E>{};
一个不包含重复元素的 collection。无序(存储顺序和取出顺序不一致),元素唯一。
11 HashSet<E>类
public class HashSet<E> extends AbstractSet<E>
implements Set<E>, Cloneable, java.io.Serializable{};
public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E> {};