Internet_worm
Internet_worm的博客

导航

 

这一次主要是Java集合中collection接口以及list&set接口一次主要是Java集合中collection接口以及list&set接口,下一篇是泛型和Map

上图是Java集合中的集合接口之间的父子关系

collection接口具有的方法主要是:

因为collection是一个接口,所以不能创建对象,因此,只能用它的实现类来创建对象

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;


public class collection {
public static void main(String[] args) {

Collection collection1=new ArrayList();
Collection collection2=new ArrayList();

//添加元素的方法 boolean add(Object obj)
collection1.add("aaa");
collection1.add("bbb");

collection2.add("aaa");
collection2.add("bbb");
collection2.add("ccc");

//删除元素的方法 boolean remove(Object obj) boolean removeAll()
collection1.remove("aaa");

//判断一个集合中是否含有某个元素boolean contains(Object obj)
collection1.contains("aaa");
//判断一个集合是否含有另外一个集合中所有的元素boolean contains(collection coll)
collection2.containsAll(collection1);

//除去两个集合中的相同元素的boolean retainAll(Collection coll)
collection2.retainAll(collection1);
System.out.println(collection2);
//遍历集合中所有的元素,方法1: Iterator(迭代器)

Iterator it=collection2.iterator();
while(it.hasNext()){
System.out.println(it.next());
}

//遍历集合中所有的元素,方法2:使用增强的for循环

for (Object object : collection2) {
System.out.println(object);
}
//判断集合是否为空
collection1.isEmpty();
//清空集合

collection1.clear();


}

}

 

List和Set也只是接口,所以,直接进行ArrayLIst、linkedlist、hashset和treeset的知识

ArrayList的内部结构是数组,LinkedList的内部结构是链表,所以ArrayList会存在分配的内存空间不够用的情况,会分配新的数组,大小增加一半

HashSet的内部结构是数组和链表

 

 

import java.util.ArrayList;
import java.util.ListIterator;


public class Collection2 {
public static void main(String[] args) {

ArrayList arrayList=new ArrayList();
//因为ArrayList是Collection接口子接口的实现类,所以collection接口所具有的方法,ArrayLIst都是有的
//因此只写一些ArrayLIst所具有的新的方法


//在指定索引位置添加元素,也可以在指定位置添加另外一个集合
arrayList.add(0, "宝宝");
arrayList.add(1,"大宝");
arrayList.add(2,"二宝");
arrayList.add(3,"小宝");

//删除指定索引位置的元素
arrayList.remove(0);
//返回指定元素的索引
arrayList.indexOf("大宝");
//获取指定索引位置的元素
arrayList.get(0);
//arrayList内部的数据结构是数组,他也有自己特有的遍历迭代器,listIterator
ListIterator listiterator=arrayList.listIterator();
//判断是否有先驱,与hasNext对应
listiterator.hasPrevious();
//因为在list中,元素时可重复的,indexOf方法返回的是元素在集合中第一次出现的索引,lastindexof()返回的是最后一次出现的位置
arrayList.lastIndexOf("大宝");

}

}

 

LinkedList与ArrayList的方法差不多,多出的是addFirst()  addLast()   getFirst() getLast() removeFirst()  removeLast()

 

 

 

Set:

在Set的只是种,主要以TreeSet为主,因为treeSet和HashSet中添加元素的时候,都是会实现自动排序的,HashSet内部按照哈希函数排序,TreeSet内部按照元素的自然排序 排序,但是在显示使用中,元素的自然排序是不够用的,因此需要自己写比较器

实体类Student:


public class Student implements Comparable{
private int id;
private String name;
private int age;

public Student(){

}
public Student(int id,String name,int age){
this.id=id;
this.name=name;
this.age=age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}


public String toString(){
return "姓名:"+name+" 学号:"+id+" 年龄:"+age;
}
/*@Override
//添加的学生元素的排序方式,按照学生号id升序排序
public int compareTo(Object o) {
Student stu=(Student)o;
return this.id-stu.getId();
}

*/

@Override
//添加学生的顺序按照年龄升序排序,年龄相同时按照姓名排序
public int compareTo(Object o) {

Student stu=(Student)o;
if(stu.getAge()==this.age){
return name.compareTo(stu.getName());
}else{

return this.age-stu.age;

}



}

}

 

实现类:

import java.util.Iterator;
import java.util.TreeSet;


public class Collection4 {
public static void main(String[] args) {
TreeSet treeSet=new TreeSet();
treeSet.add(new Student(0003,"赵三宝",22));
treeSet.add(new Student(0004,"赵四宝",20));
treeSet.add(new Student(0001,"赵大宝",20));
treeSet.add(new Student(0002,"赵二宝",21));
treeSet.add(new Student(0005,"赵小宝",15));


Iterator it=treeSet.iterator();
while(it.hasNext()){
System.out.println(it.next());
}



}

}

 

 

比较器:

在现实的编码环境下,我们拿到的一般不会是别人写的源码,而是打包后的文件,所以我们不能改在源码中进行改变(在Student类中实现的compareTo方法),所以在一般情况下需要我们自己写比较器

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;


public class Collection5 {
public static void main(String[] args) {
MyComparator2 myComparator2=new MyComparator2();
TreeSet treeSet=new TreeSet(myComparator2);

treeSet.add(new Student(0001,"张大宝",20));
treeSet.add(new Student(0002,"张二宝",21));
treeSet.add(new Student(0001,"张小宝",14));
treeSet.add(new Student(0003,"张三宝",22));
treeSet.add(new Student(0004,"张四宝",20));
Iterator it=treeSet.iterator();
while(it.hasNext()){
Object obj=it.next();
System.out.println(obj);
}

}

}


class MyComparator2 implements Comparator{

@Override
public int compare(Object o1, Object o2) {
Student stu1=(Student)o1;
Student stu2=(Student)o2;
int age1=stu1.getAge();
int age2=stu2.getAge();
if(stu1.getAge()==stu2.getAge()){
String name1=stu1.getName();
String name2=stu2.getName();
return name1.compareTo(name2);

}else{

return age1-age2;

}

}


}

 

LinkedHashSet:

内部结构是哈希表和链表的结合体

因为哈希表要求内部元素无序而且是不可重复的,而链表的内部,元素时可重复的但是是有序的,所以LinkedHashSet要求元素有序,不可重复

import java.util.Iterator;
import java.util.LinkedHashSet;


public class LinkedHashSetTest {
public static void main(String[] args) {
LinkedHashSet linkedHashSet=new LinkedHashSet();
linkedHashSet.add("大宝");
linkedHashSet.add("二宝");
linkedHashSet.add("小宝");
linkedHashSet.add("大宝");
linkedHashSet.add("二宝");
linkedHashSet.add("小宝");
Iterator it=linkedHashSet.iterator();

while (it.hasNext()){
System.out.println(it.next());
}

}

}

 

posted on 2015-07-24 00:08  Internet_worm  阅读(90)  评论(0)    收藏  举报