------- android培训、java培训、期待与您交流! ----------
集合类:
集合是存储对象最常用的方式
数组和集合的不同:
数组是固定长度且存的为同一个类型,集合是无限制的
集合的结构框架如下:
collection
list
arraylist
linkedlist
set
hashset
linkedhashset
treeset
map
hashmap
linkedhashmap
treemap
List:元素是有序的,元素是可以重复的,因为该集合体系有索引
set:元素是无序的
set集合的功能和collection是一样的
List特有的方法:
增:add(index,element);
删:remove(index);
改:set(index,element);
查:get(index,element);
List集合特有的迭代器ListIterator是Iterator的子接口,在迭代时,不可以通过集合的方法操作集合中的对象,会发生并发修改异常。所以,在迭代是,只能用迭代器的方法操作元素。可是Iterator中的方法是有限的,只能对元素进行判断,取出,删除的操作,如果想要其它操作,如:修改,添加等,就需要使用其子接口(列表迭代器)ListIterator,该接口只能通过List集合的ListIterator方法获取。
ArrayList和LinkedList使用选择:ArrayList底层数据结构是基于数组结构的,查询速度比较快,不过增删比较慢,另外线程是不同步的。
LinkedList底层数据结构是基于链表数据结构,增删速度比较快,不过查询速度比较慢,线程也是不同步的.
vector:底层是数组数据结构 线程同步
枚举是vector特有的去除方式
LinkedList中特有的方法:
addFirst(); addLast(); getFirst(); getLast(); removeFirst(); removeLast();
JDK1.6出现了替代方法:
offerFirst(); offerLast(); peekFirst(); peekLast(); pollFirst(); pollLast();
List集合判断元素是否相同一句是元素的equals方法.
Set
HashSet:底层数据结构是哈希表,线程是非同步的
HashSet是如何保证元素唯一性?
是通过元素的两个方法:hashCode和equals来完成
往集合里面存入元素的时候,会先调用hashCode方法去获取所存元素的HashCode值,如果HashCode值与集合中已经存入元素的 HashCode值 不相同,就会把元素存入集合中;如果HashCode值和已存入的某个元素的HashCode值相同,然后就会调用equals方法去比较这两个HashCode值相同的元 素是否为同一个元素。
import java.util.*;
class HashSetDemo {
public static void main(String[] args){
HashSet<Student> hs = newHashSet<Student>();
hs.add(new Student("zhangsan",26));
hs.add(new Student("lisi",25));
hs.add(new Student("wangwu",21));
hs.add(new Student("wangwu",21));
hs.add(new Student("zhangsan",26));
Iterator<Student> it = hs.iterator(); //使用迭代器取出元素
while(it.hasNext()){
Student s = it.next();
System.out.println(s.getName()+".."+s.getAge());
}
}
}
//定义一个学生类
class Student{
private String name;
private int age;
Student(String name,int age){
this.name = name;
this.age = age;
}
public int hashCode(){ //覆写hashCode方法
System.out.println("hashCode"+"..."+this.name);
return name.hashCode()+age*21; //*21为提高效率
}
public boolean equals(Object obj){ //如果hashCode方法return值为0,然后调用equals方法判断
if(!(obj instanceof Student))
return false;
Student s = (Student)obj;
System.out.println(this.name+".."+s.name);
System.out.println(this.age+".."+s.age);
returnthis.name.equals(s.name)&&this.age==s.age;
}
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;
}
}
TreeSet:
特点:可以对set集合中的元素进行排序
comparable该接口强制对子类进行排序,排序是当主要条件相同时一定要判断一下次要条件
底层数据结构是二杈数
保证元素唯一性的依据是:compareTo方法return
TreeSet第一种排序方式:让元素自身具备比较性元素需要实现comparable接口覆盖compareTo方法,这种方式也称为元素的自然顺序
TreeSet第二种排序:当元素本身不具备比较性时,或者具备的比较性不是所需要的,这时就需要让集合自身具备比较性
在集合初始化时就有了比较方式,当2种排序都存在时以比较器为主.
import java.util.*;
class TreeSetDemo {
public static void main(String[] args){
TreeSet<Person> ts = newTreeSet<Person>(new myComparator());
ts.add(new Person("zhangsan1",25));
ts.add(new Person("zhangsan3",22));
ts.add(new Person("zhangsan2",21));
ts.add(new Person("zhangsan4",26));
ts.add(new Person("zhangsan1",21));
Iterator<Person> it = ts.iterator();
while(it.hasNext()){
Person p =it.next();
System.out.println(p.getName()+".."+p.getAge());
}
}
}
class myComparator implements Comparator<Object>
{
public int compare(Object obj1,Object obj2){
Person p1 =(Person)obj1;
Person p2 =(Person)obj2;
int num = new Integer(p1.getAge()).compareTo(newInteger(p2.getAge()));
if(num==0)
returnp1.getName().compareTo(p2.getName());
return num;
}
}
class Person implements Comparable<Object>
{
private String name;
private int age;
public int compareTo(Object obj){
if(obj instanceof Person){
Person p =(Person)obj;
int num2 = this.getName().compareTo(p.getName());
if(num2==0){
return newInteger(this.getAge()).compareTo(new Integer(p.getAge()));
}
return num2;
}
else
throw new RuntimeException();
}
Person(String name,int age){
this.name = name;
this.age = age;
}
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;
}
}
泛型(在以后的java基础加强里面会有更详细的讲解)
泛型JDK1.5版本出现的新特性用于解决安全问题是一个安全机制
ArrayList<String> al = new ArrayList<String>();
好处:
1.将运行时期出现的问题转移到了编译时期
2.避免了强制转换
格式:
通过<>来定义要操作的引用数据类型
什么时候用泛型?
通常在集合框架中很常见,看到<>(尖括号)基本都要用到
当类中要操作的引用数据类型不确定时,早起定义object来完成扩展现在定义泛型来完成
为了让不同方法可以操作不同类型,而且类型还不确定,可以将泛型定义在方法上
特殊之处:
static方法不可以访问类上定义的泛型,如果静态方法操作的数据类型不确定可以将泛型定义在方法上
Map
Map<K,V>K:键的类型 V:映射值的类型
Mapde 子类
HashTable:底层是哈希表结构,不可以存入null键null值 线程是同步的
HashMap:底层是哈希表结构允许使用null 线程是非同步的
TreeMap:底层二叉数结构,线程是不同步的,可以给map集合中的键进行排序
Map集合的两种取出方式:
1 .Set<K> keySet: 将Map集合中的键存入到Set集合,因为Set集合可以使用迭代器取出所有的键,把键取出之后可以使用get(key)方法获取每一个键对应的值。
2 .Set<Map.Entry<k,v>> entrySet: 将Map集合的映射关系存入到Set集合中,而这个关系的数据类型就是Map.Entry。用迭代器把所有的映射关系取出以后,可以使用getKey和getValue方法取出对应的键和值.
集合的取出原理:
将map集合转化为set集合然后通过迭代器取出
map集合被使用是因为具备映射关系
需求:获取"sdfgcvasdf"字母出现的次数
import java.util.*;
class TreeMapDemo {
public static void main(String[] args) {
String s= getStringMethod("sdfgcvasdf");
System.out.println(s);
}
public static String getStringMethod(String s){
char[] ch = s.toCharArray();
TreeMap<Character,Integer> sm = newTreeMap<Character,Integer>();
for(char chs : ch){
if(sm.get(chs)==null)
sm.put(chs, 1);
else{
int value = sm.get(chs);
sm.put(chs,value+1 );
}
}
Set<Map.Entry<Character,Integer>> se=sm.entrySet();
StringBuilder sb = new StringBuilder();
Iterator<Map.Entry<Character,Integer>>it = se.iterator();
while(it.hasNext()){
Map.Entry<Character,Integer>me=it.next();
Character key =me.getKey();
Integer value=me.getValue();
sb.append(key+"("+value+")"+",");
}
StringBuildersb1=sb.deleteCharAt(sb.length()-1);
return sb1.toString();
}
}