集合框架(三)

来源:互联网 发布:巨龙之巢网络波动 编辑:程序博客网 时间:2024/05/16 16:01
android培训、java培训、期待与您交流! ----------


集合框架(三):
collection:
|--List:有序的,可以插入元素,每个元素都有下标
|--ArrayList:底层是数组结构,查询快,增删慢
|--LinkedList:底层是链表结构,查询慢,增删快(先进后出)
|--Vector: 可变数组结构,线程同步的,线程安全


--Iterator:
|--hasNext()有元素返回真
|--next()返回下一个可以迭代的元素
|--remove 移除集合中元素
--ListIterator
|--List特有的迭代器
|--add()添加元素
|--remove()移除元素
|--set()
|--hasPrevious()逆向遍历集合,但是必须先正向迭代
|--privious()返回逆向遍历的下一个可以迭代的元素


|--Set: 无序的,存储和取出,不存储重复的元素,包括对象和自定义对象
取出的方式只有迭代器
|--TreeSet
|--对对象进行自然的排序
|--参考的是对象是否具有比较功能,对象实现的是comparable接口,复写compareTO方法
|--参考compareTo方法
|--第二种排序,treeSet集合,构造函数接收的是比较器,需要实现comparetor接口,复写comppare方法
|--参考的是compare方法中的返回值
|--HashSet: 
|--无序的,不存储重复的元素
|--判断是否重复的依据是对象中的hashCode方法和equals方法
|--当哈希值一致时,集合会再次调用equals方法,当queals返回true时,为同一个对象
|--当哈希值不一致时,不是同一个对象,不需要在调用equals
|--当哈希值一致时,集合会再次调用equals,当equals返回false,为不同的对象
|--hashSet数据结构是哈希表,也叫筒子结构


自定义对象包含方法:
1. 构造方法
2. set()
3. get()
4. toString() 不是必须的
5. hashCode()
6. equals()
7. compareTo()
自定义对象基本比较功能,复写

 
//ArrayList代码演示
import java.util.*;
class ArrayListTest
{
public static void main(String[] args)
{
//创建ArrayList集合
ArrayList<Person> al = new ArrayList<Person>();
//LinkedList<Person> al = new LinkedList<Person>();
//Vector<Person> al = new Vector<Person>();


//给集合添加对象元素
al.add(new Person("lidehua",20));
al.add(new Person("zhangbaizhi",21));
al.add(new Person("caizhuoyan",22));
al.add(new Person("ajiao",23));
al.add(new Person("xiatingfeng",24));

//集合获取迭代器
Iterator<Person> it = al.iterator();


//循环得带集合中的元素
while(it.hasNext())
{
Person p = it.next();
System.out.println(p.getName()+"..."+p.getAge());


}
}
}
class Person
{
private String name;
private int age;
Person(String name, int age)
{
this.name = name;
this.age = age;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setAge()
{
this.age = age;
}
public int getAge()
{
return age;
}

}
 


 
去除集合中重复元素
思路:
1. 建立一个集合
2. 从老集合中提取元素,想新集合添加
3. 添加之前先判断一下新集合中是否已经存在了这个元素.
4. 新集合中存在就不添加,不存在就添加
5. 返回新的集合
 

 
//去除重复元素代码示例1
import java.util.*;
class Test06
{
public static void main(String [] args)
{
ArrayList  al  = new ArrayList ();

al.add(new Person("liudehua",40));
al.add(new Person("zhangbaizhi",42));
al.add(new Person("liuruoying",44));
al.add(new Person("liuruoying",44));
al.add(new Person("liruotong",38));
al.add(new Person("liruotong",38));
al.add(new Person("zhaowei",32));


ArrayList newList = method(al);
System.out.println(newList);
}
public static ArrayList method(ArrayList al)
{
ArrayList  newList= new ArrayList ();
Iterator  it = al.iterator();
while(it.hasNext())
{
Object obj= (Object)it.next();
if(!(newList.contains(obj)))
{
newList.add(obj);
}
}
return newList;
}


}
class Person
{
private String name;
private int age;
Person(String name, int age)
{
this.name = name;
this.age = age;
}
//建立对象自己的比较方式,比较年龄和姓名,
public boolean equals(Object obj)
{
Person p = (Person)obj;
return this.name.equals(p.name) && this.age==p.age;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setAge()
{
this.age = age;
}
public int getAge()
{
return age;
}
 
public String toString() 
{
return "name=" + name + "...age=" + age;
 
}

}
 
//去除重复元素代码示例2
import java.util.*;
class HashSetTest
{
public static void main(String[] args)
{
HashSet  hs = new HashSet();
hs.add(new Person("张柏芝",40));
hs.add(new Person("刘德华",34));
hs.add(new Person("刘德华",34));
hs.add(new Person("周润发",45));
hs.add(new Person("成龙",46));
hs.add(new Person("成龙",46));
hs.add(new Person("周华健",39));
Iterator it = hs.iterator();
while(it.hasNext())
{
Person p = (Person)it.next();
//Person p = it.next();
//System.out.println(p.getName()+".."+p.getAge());
System.out.println(p);
}
}
}
class Person
{
private String name;
private int age;
Person(String name, int age)
{
this.name = name;
this.age = age;
}
//复写hahsCode方法,依据的是对象中的年龄和姓名,若年龄姓名一样那么哈希值也是一样的
public int hashCode(Object obj)
{
Person p =(Person)obj;
return this.name.hashCode()+this.age*12;
}
//建立对象自己的比较方式,比较年龄和姓名,
public boolean equals(Object obj)
{
Person p = (Person)obj;
return this.name.equals(p.name) && this.age==p.age;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setAge()
{
this.age = age;
}
public int getAge()
{
return age;
}
 
public String toString() {
return "name=" + name + "...age=" + age;
 
}

}
 




 
//TreeSet代码示例
//实现了Comparable接口,并覆盖compareTo方法,这个对象,就具备排序功能
import java.util.*;
 class TreeSetDemo 
{
public static void main(String[] args) 
{
TreeSet ts = new TreeSet();
//ArrayList ts = new ArrayList();
ts.add(new Person("wangwu1",21));
ts.add(new Person("wangwu2",22));
ts.add(new Person("wangwu2",20));
ts.add(new Person("wangwu3",20));
ts.add(new Person("wangwu1",21));
//System.out.println(ts);
Iterator it = ts.iterator();
while(it.hasNext())
{
//System.out.println(it.next());
Person p = (Person)it.next();
System.out.println(p.getName()+"..."+p.getAge());
}
}
}




//实现comparable接口,复写compareTo方法和equals方法
class Person implements Comparable
{
private String name;
private int age;
Person(String name, int age)
{
this.name = name;
this.age = age;


}
public int compareTo(Object obj)
{
Person p = (Person)obj;


int num = this.age-p.age;
return num==0?this.name.compareTo(p.name):num;
}
//复写hahsCode方法,依据的是对象中的年龄和姓名,若年龄姓名一样那么哈希值也是一样的

//建立对象自己的比较方式,比较年龄和姓名,
public boolean equals(Object obj)
{
Person p = (Person)obj;
return this.name.equals(p.name) && this.age==p.age;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setAge()
{
this.age = age;
}
public int getAge()
{
return age;
}
 
public String toString() {
return "name=" + name + "...age=" + age;
 
}

}
//自定义比较方法示例,实现Comparetor接口
class MyCom implements Comparator
{
public int compare(Object o1,Object o2)
{
Car c1 = (Car)o1;
Car c2 = (Car)o2;
 
int num =c1.getDoor()-c2.getDoor();
return num==0?c1.getName().compareTo(c2.getName()):num;
}
}
 


 
 * Map<K,V>双列集合
 * HashMap 底层是哈希表数据结构,保证对象的唯一性。键必须唯一
 
import java.util.*;
 class HashMapTest
 {
public static void main(String[] args)
{
HashMap<Integer , String> hm = new HashMap<Integer , String>();


hm.put(1, "zhanghua");
hm.put(2," zhangsan");
hm.put(3, "lisi");
hm.put(4, "wanger");
hm.put(5, "zhaoliu");
hm.put(6," sunqi");


 
* entrySet方法,返回集合中键值得对应关系
* 这个对象关系,用Map.Entry对象描述
* Map.Entry的泛型,就应该是这个集合中,键和值得泛型
* 得到这个关系后,需要将这个关系存储到Set集合
* Set集合存储的是这个关系 ,Set集合的泛型,就是这个关系
 
Set<Map.Entry<Integer,String>> set=hm.entrySet();


Iterator<Map.Entry<Integer,String>> it= set.iterator();
while(it.hasNext())
{
Map.Entry<Integer,String> me = it.next();
Integer i = me.getKey();
String s = me.getValue();
System.out.println(i+"..."+s);
}
}
 }
 

 


 
 * HashMap存储自定义对象
 * 自定义对象,人的姓名,年龄, 家庭住址
 * 
 * 键,是自定的人的对象,值,家庭住址
 
import java.util.*;
public class HashMapDemo1 {


public static void main(String[] args) {
HashMap<Person,String> hm = new HashMap<Person, String>();
hm.put(new Person("lisi1",21), "china");
hm.put(new Person("lisi2",22), "USA");
hm.put(new Person("lisi3",23), "GB");
hm.put(new Person("lisi4",24), "Korea");
hm.put(new Person("lisi4",24), "Korea");

//使用Map.Entry获取这个集合中的元素

Set<Map.Entry<Person, String>> set =hm.entrySet();
Iterator<Map.Entry<Person, String>> it = set.iterator();
while(it.hasNext()){
Map.Entry<Person, String> me = it.next();

Person p = me.getKey();
String s = me.getValue();

System.out.println(p+"..."+s);
}
}


}
class Person implements Comparable<Person>{
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public int compareTo(Person p){
int num = this.age - p.age;
return num==0?this.name.compareTo(p.name):num;
}
public int hashCode(){
final int X = 434;
return this.name.hashCode()+this.age*X;
}

public boolean equals(Object obj){
if(this==obj)
return true;
if(!(obj instanceof Person))
return false;
Person p = (Person)obj;
return this.name.equals(p.name) && this.age == p.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;
}
public String toString(){
return name+".."+age;
}



}









 
 
 * 有一个字符串  fecgtrdefgbrde
 * 计算出,字符串中字母出现的次数
 * 
 * 思路:
 *  1. 字符串,先搞成字符数组
 *  2. 建立一个空的TreeMap集合
 *  3. 遍历集合
 *    遍历的过程中,取出的每一个字符,都去集合中查询,看看有还是没有啊
 *    有,就存储字符,和累计出现次数++
 *    没有,就存储字符,和累计出现次数1
 *    
 *  4. 字符串中有其他特殊字符,# @  & * 过滤它
 
import java.util.*;
public class TreeMapTest {


public static void main(String[] args) {


String s = "f6h9ec-gtrd+*&efg**br4de";


TreeMap<Character,Integer> tm = new TreeMap<Character, Integer>();


char[] ch = s.toCharArray();

for(int x = 0 ; x <ch.length;x++){
if((ch[x]<'a' || ch[x]>'z') && (ch[x]<'A' || ch[x]>'Z')){
continue;
}
Integer i =tm.get(ch[x]);
if(i==null){
tm.put(ch[x], 1);
}else{
i++;
tm.put(ch[x], i);
}
}

Set<Map.Entry<Character, Integer>> set = tm.entrySet();
Iterator<Map.Entry<Character, Integer>> it = set.iterator();
while(it.hasNext()){
Map.Entry<Character, Integer> ma = it.next();
char c = ma.getKey();
int  i = ma.getValue();

System.out.println(c+"出现了"+i+"次");
}
}
}

原创粉丝点击