黑马程序员_java基础_集合(Collection和List、Set)
来源:互联网 发布:北漂 知乎 编辑:程序博客网 时间:2024/05/21 10:33
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
集合
(一)概述:
集合是一种容器,用来存储对象。集合的长度是可变的,可以存储不同类型的对象。
(二)集合与数组的区别:
数组也可以存储对象,但长度是固定的,集合长度是可变的。数组中可以存储基本数据类型,集合只能存储对象。
(三)常见的集合类:
常见的集合类有List集合,Set集合,Map集合,List、Set集合继承了Collection接口。
Collection
(一)Collection接口常见方法:
add(E e):将指定的对象添加到该集合中
remove(Object o):将指定的对象从该集合中移除
contains(Object o):判断是否存在这个元素
isEmpty():返回Boolean值,用于判断当前集合是否为空
size():返回int值,获取该元素的个数
iterator():迭代器,用于取出集合中元素
(二)iterator迭代器
1.迭代是取出集合中元素的一种方式。
2.创建iterator迭代器:
ArrayList a=new ArrayList();创建一个集合
Iterator it=a.iterator();获取一个迭代器
3.iterator迭代器常见方法:
hasNext();判断是否有下一个元素,返回boolean值
next();取出下一个元素
remove();移除元素
4.iterator迭代器取出元素的常见格式:
ArrayList a=new ArrayList();创建一个集合
Iterator it = a.iterator();获取一个迭代器
while(it.hasNext())循环判断是否有下一个元素
{
System.out.println(it.next());取出集合中的所有元素
}
List
(一)List的概述
1.List是Collection接口的子接口,具备了Collection的所有方法。
2.List:元素是有序的(元素存入集合的顺序和取出的顺序一致),元素可以重复。因为该集合体系有索引。
3.List集合中常见的子集合有ArrayList、LinkedList、Vector。它们各自的特点为:
ArrayList:底层使用的是数组数据结构,线程不同步。特点:查询速度很快,增删速度稍慢。
LinkedList:底层使用的是链表数据结构。特点:增删速度很快,查询速度稍慢。
Vector:底层使用的是数组数据结构,线程同步。增删查询速度都很慢。
(二)List特有方法:
凡是可以操作角标的方法都是该体系的特有方法。
add(index,element):在指定角标位置插入一个元素。
addAll(index,Collection):在指定的角标位置插入一个集合。
remove(index):删除指定位置的元素。
set(index,element):修改指定位置的元素。
get(index):通过角标获取元素。利用该方法遍历可以获取到所有元素。
indexOf(obj);获取元素第一次出现的位置,如果没有则返回-1
subList(from,to):从包含from到不包含to获取子列表。listIterator():List集合的迭代器。
LinkedList
(一)LinkedList特有的方法:
addFirst();addLast();
getFirst();getLast();获取元素,如果集合中没有元素,出现NoSuchElementException异常
removeFirst();removeLast();获取并删除元素,如果集合中没有元素出现NoSuchElementException异常
在JDK1.6出现了替代的方法:
offerFirst();offerLast():
peekFirst();peekLast():获取元素,如果集合中没有元素返回null。
pollFirst();pollLast():获取并删除元素,如果集合中没有元素返回null。
(二)LinkedList集合子类中一个常见的操作:堆栈和队列。代码如下:
import java.util.*;public class LinkedListTest1{public static void main(String[] args){Demo li=new Demo();li.myadd("zhangsan01");li.myadd("zhangsan02");li.myadd("zhangsan03");li.myadd("zhangsan04");//li.stack();//堆栈输出li.queue();//队列输出}}class Demo{private LinkedList link;Demo(){link=new LinkedList();}public void myadd(Object o){link.addFirst(o);}public void stack()//堆栈方法{while(!link.isEmpty())System.out.print(link.removeFirst()+" ");}public void queue()//队列方法{while(!link.isEmpty())System.out.print(link.removeLast()+" ");}}
Set
(一)Set概述:
1.Set是Collection接口的子接口,具备了Collection的所有方法。
2.元素是无序(存入和取出的顺序不一定一致),元素不可以重复。
3.Set接口常见的实现类是有HashSet类和TreeSet类。
(二)HashSet类:
底层数据结构是哈希表,线程非同步的。 保证元素唯一性的原理:判断元素的hashCode值是否相同。如果相同,还会继续判
断元素的equals方法,是否为true。
(三)TreeSet类:
可以对Set集合中的元素进行排序。默认按照字母的自然排序。底层数据结构是二叉树。保证元素唯一性的依据:compareTo方法return 0。
第一种排序方式:自然排序。让元素自身具备比较性。元素需要实现Comparable接口,覆盖compareTo方法。这种方式也被称为元素的自然顺序,或者叫做默认顺序。
第二种方式:比较器 。当元素自身不具备比较性时,或者具备的比较性不是所需要的。这时就需要让集合自身具备比较性。在集合初始化时,就有了比较方式。定义一个比较器,将比较器对象作为参数传递给TreeSet集合的构造函数。比较器构造方式:定义一个类,实现Comparator接口,覆盖compare方法。当两种排序都存在时,以比较器为主。
一个具有hashCode、equals、compareTo、compare的例子:
import java.util.*;class Student1 implements Comparable{private String name;private int age;Student1(String name,int age){this.name=name;this.age=age;} public String getname(){return this.name;}public int getage(){return this.age;}public int hashCode()//用于HashSet的比较{return name.hashCode()+age;}public boolean equals(Object o)//用于HashSet的比较{if(!(o instanceof Student1))return false;Student1 s=(Student1)o;return this.name.equals(s.name) && this.age==s.age;}//用于TreeSet的比较public int compareTo(Object obj) { Student1 s=(Student1)obj; if(this.age==s.age) return this.name.compareTo(s.name); return new Integer(this.age).compareTo(new Integer(s.age)); } }//比较器用于TreeSet的比较class MyCompare implements Comparator{public int compare(Object o1,Object o2){Student1 s1=(Student1)o1;Student1 s2=(Student1)o2;int mun=s1.getname().compareTo(s2.getname());if(mun==0)return new Integer(s1.getage()).compareTo(new Integer(s2.getage()));return mun;}}public class TreeSetTest{public static void main(String[] args){//创建TreeSet集合TreeSet tr=new TreeSet(new MyCompare());tr.add(new Student1("zhangsan1",20));tr.add(new Student1("zhangsan02",19));tr.add(new Student1("zhangsan3",25));tr.add(new Student1("zhangsan4",29));tr.add(new Student1("zhangsan3",25));Iterator it=tr.iterator();while(it.hasNext()){Student1 s=(Student1)it.next();System.out.println(s.getname()+" "+s.getage());}//创建HashSet集合HashSet ha=new HashSet();ha.add(new Student1("lisi1",20));ha.add(new Student1("lisi02",19));ha.add(new Student1("lisi3",25));ha.add(new Student1("lisi4",29));ha.add(new Student1("lisi3",25));Iterator it1=ha.iterator();while(it1.hasNext()){Student1 s=(Student1)it1.next();System.out.println(s.getname()+" "+s.getage());}}}
- 黑马程序员_java基础_集合(Collection和List、Set)
- 黑马程序员_毕向东JAVA基础_集合(2)Collection&&List&&Set&&泛型
- 黑马程序员_集合概述(Collection List&Set)
- 黑马程序员_java集合(1) Collection & List & Set & Map
- 黑马程序员_Java基础_集合框架成员Collection
- 黑马程序员—java基础_集合List和Set
- 黑马程序员_JAVA学习日记_JAVA中API:集合框架1(Collection,List,Set及其子类和迭代器的应用)
- 黑马程序员_java语言_集合框架(Collection)
- 黑马程序员----java基础第14天(集合Collection,List,Set)
- 黑马程序员_Java(对象数组,集合Collection,集合list)
- 黑马程序员_学习笔记:9) 集合框架1:Collection(List、Set)、Iterator、List(ArrayList、LinkedList、Vector)
- 黑马程序员_Java基础_集合框架
- 黑马程序员_Java基础_集合框架
- 黑马程序员_java基础_集合框架
- 黑马程序员___13java基础 集合框架 LIST和SET集合
- 黑马程序员_Java集合Collection
- 黑马程序员_集合中List和Set
- 黑马程序员_Java基础_集合(Map类)
- 变量、数组、泛型、集合
- linux 临时增加swap空间
- ISO C89 forbids mixed declarations and code
- ANDROID开发环境搭建
- 从线上走向线下的用户体验
- 黑马程序员_java基础_集合(Collection和List、Set)
- MFC 学习之 下拉列表框
- svd 推荐算法
- Dalvik opcodes
- 第十二周项目一:- 教师兼干部类
- word异常
- 钟表维修管理系统技术解析 工单查询(一)
- Android Studio开发基础之动态注册与注销BroadcastReceiver
- python idle 清屏问题的解决