——Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ——-
集合
集合类:对象用于封装特有数据,对象多了需要存储;如果对象个数不确定就使用集合容器进行存储
集合的特点:
1.用于存储对象的容器
2.集合的长度是可变的
3.集合中不可以存储基本数据类型值
集合容器因为内部的数据结构不同,有多种具体容器,在不断的向上抽取中,就形成了集合框架。
数组和集合类同是容器,有何不同?
数组虽然可以存储对象,但长度是固定的;集合长度是可变的。
数组中可以存储基本数据类型,集合只能存储对象。
集合框架的构成及分类:
Collection接口
collection的常见方法:
1.添加:
boolean add(Object obj);
boolean addAll(Collection coll);
2.删除:
boolean remove(Object obj);
boolean removeAll(Collection coll);
void clear();
3.判断:
boolean contains(Object obj);
boolean containsAll(Collection coll);
boolean isEmpty();
4.获取:
int size()
Iterator iterator();
取出元素的方式:迭代器。
该对象必须依赖于具体容器,因为每一个容器的数据结构都不用,所以该迭代器对象是在容器中进行内部实现的,也就是iterator方法在每个容器中的实现方式都是不同的。
对于使用容器者而言,具体的实现不重要,只要通过容器获取到该实现的迭代器的对象即可,也就是iterator方法。
Iterator接口就是对所有的Collection容器进行元素的取出的公共接口。
5.其他:
boolean retainAll(Collection coll); 取交集
Objcet toArray(); 将集合转成数组
public static void main(String[] args){
Collection<String> coll =new ArrayList<String>();
coll.add("abc1");
coll.add("abc2");
coll.add("abc3");
System.out.println("coll:"+coll);
coll.remove("abc2");
System.out.println("coll:"+coll);
System.out.println("coll.contains(\"abc1\")="+coll.contains("abc1"));
Collection<String> coll2 = new ArrayList<String>();
coll2.add("abc1");
coll2.add("abc6");
coll2.add("abc7");
System.out.println("coll2="+coll2);
//将coll2中的元素添加到coll中
coll.addAll(coll2);
System.out.println("coll.addAll(coll2) = "+coll);
//将coll集合中删除与coll2集合相同的元素
coll.removeAll(coll2);
System.out.println("coll.removeAll(coll2) = "+coll);
boolean b1 = coll.containsAll(coll2);
System.out.println("coll.containsAll(coll2) = "+b1);
//取交集,保留两个集合中相同的元素。
coll.retainAll(coll2);
System.out.println("coll、coll2交集:"+coll);
}
List、Set
Collection
|–List:有序(存入和取出的顺序一致),元素都有索引的角标,允许重复元素。
|–Set:元素不能重复,无序。
List:特有常见方法。
有一个共同特点就是可以操作角标。
1.添加
void add(index,element);
void addAll(index,collection);
2.删除
Object remove(index);
3修改
Object set(index,element);
4获取
Object get(index);
int indexOf(objcet);
int lastIndexOf(objcet);
List subList(from,to);
List集合可以完成对元素的增删改查。
public static void show(){
List<String> list = new ArrayList<String>();
list.add("abc1");
list.add("abc2");
list.add("abc3");
System.out.println("list = "+list);
list.add(1,"abcadd");
System.out.println("list.add(1,\"abcadd\") ="+list);
list.remove(2);
System.out.println("list.remove(2)="+list);
list.set(1, "abcset");
System.out.println("list.set(1,\"abcset\")="+list);
System.out.println("list.get(0) = "+list.get(0));
System.out.println("list.subList(1,2)"+list.subList(1, 2));
System.out.println(list);
}
在迭代器过程中,不要使用集合操作元素,容易出现异常:
java.util.ConcurrentModificationException
可以使用Iterator接口的子接口ListIterator来完成在迭代中对元素进行更多的操作。
public static void iteratorlistDemo(){
List<String> list = new ArrayList<String>();
list.add("abc1");
list.add("abc2");
list.add("abc3");
System.out.println("list ="+list);
ListIterator<String> listIt = list.listIterator();
while(listIt.hasNext()){
String str = (String) listIt.next();
if(str.equals("abc2")){
listIt.add("abc0");
}
}
System.out.println("hasNext:"+listIt.hasNext());
System.out.println("hansPrevios:"+listIt.hasPrevious());
while(listIt.hasPrevious()){
System.out.println("prevoids :"+listIt.previous());
}
System.out.println("list :"+list);
System.out.println("test list.iterator()--------------------------");
Iterator<String> it = list.iterator();
while(it.hasNext()){
String str = (String) listIt.next();
if(str.equals("abc2")){
listIt.add("abc0");
}
}
}
vector、ArrayList\linkedList
List
|–Vector:内部是数组结构,是同步的,增删,查询都很慢。
|–ArrayList:内部是数组数据结构,是不同步的,替代了Vector。查询速度比Vector快
|–LinkedList:内部是链表数据结构,不同步,增删元素的速度快。
LinkedList方法:
addFirst();
addLast();
jdk1.6版本后新方法:
OfferFirst();与addFirst()方法没有区别。
offerLast();与addLast方法没有区别。
getFirst();获取但不移除,如果链表尾空,抛出NoSuchElementException.
getLast();
jdk1.6版本后新方法:
peekFirst();//获取但不移除,如果链表为空,返回null。
peekLast();
removeFirst();//获取并移除,如果链表尾空,抛出NoSuchElementException。
removeLast();
jdk1.6版本后新方法:
pollFirst();//获取并移除,如果链表为空,返回null;
pollLast();
public static void main(String[] args) {
System.out.println("vector--------------------");
Vector<String> v = new Vector<String>();
v.addElement("abc1");
v.add("abc2");
v.add("abc3");
v.addElement("abc4");
Enumeration<String> en = v.elements();
while(en.hasMoreElements()){
System.out.println("nextElement:"+en.nextElement());
}
Iterator<String> it =v.iterator();
while(it.hasNext()){
System.out.println("next:"+it.next());
}
System.out.println("linklist-------------------------------");
LinkedList<String> link = new LinkedList<String>();
link.addFirst("abc1");
link.offerFirst("abc2");
link.offerLast("abc3");
link.offer("abc4");
Iterator<String> itLink = link.iterator();
while(itLink.hasNext()){
System.out.println("next:"+itLink.next());
}
System.out.println(link);
System.out.println("getFirst:"+link.getFirst());
System.out.println("getLast:"+link.getLast());
System.out.println("removeFirst:"+link.removeFirst());
System.out.println("removeLast:"+link.removeLast());
while(!link.isEmpty()){
System.out.println(link.removeFirst());
}
}
Set
Set:元素不可以重复,是无序。
Set接口中的方法和collection一致。
|–HashSet:内部数据结构是哈希表,是不同步的。
|–TreeSet:可以对set集合中的元素进行排序,是不同步的。
哈希表确定元素是否相同
1.判断的是两个元素的哈希值是否相同。
如果相同,再判断两个对象的内容是否相同。
2。判断哈希值相同,其实判断的是对象的hashCode方法。判断内容相同,用的是equals方法。
往HashSet集合中存储Person对象,如果姓名和年龄相同,视为同一个人,视为相同元素。
public class SetDemo {
public static void main(String[] args) {
HashSet<Person> hs = new HashSet<Person>();
hs.add(new Person("list4", 24));
hs.add(new Person("forme", 34));
hs.add(new Person("set5", 32));
hs.add(new Person("forme", 34));
Iterator<Person> it = hs.iterator();
while(it.hasNext()){
Person p = (Person)it.next();
System.out.println(p.getName()+"..."+p.getAge());
}
}
}
class Person{
private String name;
private int age;
public Person(String name,int age){
this.name = name;
this.age = age;
}
public void setName(String name){
this.name = name;
}
public void setAge(int age){
this.age = age;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
public int hashCode(){
return name.hashCode()+age*39;
}
public boolean equals(Object obj){
if(this == obj) return true;
if(!(obj instanceof Person))
throw new ClassCastException("类型错误");
Person p= (Person)obj;
return this.name.equals(p.name)&&this.age==p.age;
}
}
LinkHashSet :按照输入顺序进行编排。
TreeSet
TreeSet判断元素唯一性的方式:就是根据比较方法的返回结果是否是0,是0,就是相同元素,不存。
TreeSet对元素进行排序的方式一:
让元素自身具备比较功能,元素就需要实现Comparable接口,覆盖compareTo方法。
可以使用TreeSet集合第二种排序方式:
让集合自身具备比较功能,定义一个类实现Comparator接口,覆盖compare方法。
将该类对象作为参数传递给TreeSet集合的构造函数。
P.S.
如果自定义类实现了Comparable接口,并且TreeSet的构造函数中也传入了比较器,那么将以比较器
的比较规则为准。
TreeSet集合的底层是二叉树进行排序的。
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet<String> ts= new TreeSet<String>(new ComparatorByLen());
ts.add("aaa");
ts.add("zzs");
ts.add("a");
ts.add("1234");
ts.add("2345");
Iterator<String> it = ts.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
class ComparatorByLen implements Comparator{
@Override
public int compare(Object arg0, Object arg1) {
String s1= (String)arg0;
String s2= (String)arg1;
int temp = s1.length() -s2.length();
return temp;
}
}