------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
为什么会出现集合类?
Java语言是面向对象的语言,所有命令都是操作对象的,有集合类的出现就可以操作对象
数组和集合类的区别是什么呢?
数组中可以存储基本类型的数据,也可以存储对象,集合类只能存储对象
集合类的特点有什么?
集合只适用于存储对象,长度是可变化的,集合可以存储不同类型的集合。
集合类Collection接口,子类接口有List接口和Set接口,List接口下含有ArrayList,LinkedList,Vector接口,
Set接口下有HashSet和TreeSet接口
为什么会出现这么多不同的容器接口?
因为每一个容器的对数据的存储方式不一样,这个存储方式叫做数据结构,也就是说,每种容器存储数据的数据结构不一样,那
什么是数据结构呢?数据结构就是数据元素之间存储的关系。
Collection的接口方法
a1.add(Object obj);增加对象进容器
a1.size();显示数据个数
a1.remove(Object obj)移除对象数据
a1.clear();清除所有对象数据
a1.retainAll(a2),取交集
a1.contains(Object obj)是否含有obj
a1.removeAll(a2)移除相同元素
集合中存储的都是对象的引用(地址);
集合的迭代器用于取出元素
Iterator ite = arr.iterator();
while(ite.hasNext())
sop(ite.next());
Collection接口中的List接口:元素是有序的,元素可以重复,因为该体系有索引
Set接口:元素是无序的,元素不可以重复.
List接口:凡是可以操作角标的方法都是该接口的特有方法
特有方法:
import java.util.*;
class ListTest
{
public static void main(String[] args)
{
ArrayList a1 = new ArrayList();
a1.add("arr1");
a1.add("arr2");
a1.add("arr3");
a1.add(1,"arr4"); //在角标位置上增加arr4对象元素
sop(a1);
remove(2); //移除角标为2的元素
a1.set(2,"arr7"); //修改角标为2的元素
sop(a1);
sop(a1.indexOf("arr1")); // 查找对象元素的位置
sop(a1.subList(1,3));// 打印1角标到3角标的元素(不包括3角标元素)
sop(a1.get(3)); //获取角标为3 的元素
for(int x = 0; x < a1.size(); x++) //打印元素
{
sop(a1.get(x));
}
Iterator ite = arr.iterator();
while(ite.hasNext())//打印元素
sop(ite.next());
}
static void sop(Object obj)
{
System.out.println(obj);
}
}
Iterator ite = a1.iterator();
while(ite.hasNext())
{
Object obj = ite.next();
if( obj.equals("arr7"))
{
ite.remove();
//不能在这写a1.add(Objext obj),
sop( obj);
}
}
ListIterator ite1 = a1.listIterator();
while(ite1.hasNext())
{
Object x = ite1.next();
if(x.equals("arr7"))
ite1.set("arr10");
sop(x);
}
ArryList:底层是数组数据结构,特点:查询速度快,但增删稍慢,线性不同步
LinkedList:底层使用的是链表结构,特点:增删速度快,查询稍慢
Vector:底层是数组数据结构,线性同步,被ArrayList代替了
Enumeration en = vec.elements();
while(en.hasMoreElements())
{
System.out.println(en.nextElement());
}//Vector独有的取出方法
List集合判断元素是否相同,依据的是元素的equals()方法
Set集合的特点:元素是无序的,元素不可重复
HashSet:底层结构是哈希表
HashSet是如何保证数据的唯一性的?
HashSet中的元素先判断哈希值是否一样,如果不一样,元素不一样
如果一样,调用equals方法判断
TreeSet中用Comparable中的compareTo(Object obj)方法
或者Comparator接口中的compare(Object obj1,Object obj2)实现排序
1 练习 2 package blogtest2; 3 import java.util.*; 4 public class ArrayListTest { 5 6 /** 7 * 将自定义对象传入到ArrayList集合中,并去除重复元素 8 */ 9 public static void main(String[] args) { 10 ArrayList<People> arraylist = new ArrayList<People>(); 11 arraylist.add(new People("zhangshan",22)); 12 arraylist.add(new People("lishan",24)); 13 arraylist.add(new People("wangwu",15)); 14 arraylist.add(new People("zhangshan",22)); 15 ArrayList<People> singlist = singleName(arraylist); 16 for(People stu : singlist){ 17 System.out.println(stu); 18 } 19 } 20 21 22 public static ArrayList<People> singleName(ArrayList<People> arraylist){ 23 ArrayList<People> list = new ArrayList<People>(); 24 Iterator<People> ite = arraylist.iterator(); 25 while(ite.hasNext()){ 26 People stu = ite.next(); 27 if(!list.contains(stu)){ 28 list.add(stu); 29 } 30 } 31 return list; 32 } 33 } 34 class People{ 35 private String name; 36 private int age; 37 People(String name,int age){ 38 this.name = name; 39 this.age = age; 40 } 41 public String getName(){ 42 return name; 43 } 44 public int getAge(){ 45 return age; 46 } 47 public String toString(){ 48 return name +"...."+age; 49 } 50 public boolean equals(Object stu){ 51 if(!(stu instanceof People)){ 52 return false; 53 } 54 People stucopy = (People)stu; 55 return this.name.equals(stucopy.name) && this.age == stucopy.age; 56 } 57 }
1 练习 2 package blogtest2; 3 import java.util.*; 4 public class LinkedListTest { 5 6 /** 7 * 用LinkedList模拟堆栈或者队列结构 8 */ 9 public static void main(String[] args) { 10 LinkedList<String> linkedlist = new LinkedList<String>(); 11 linkedlist.addFirst("java1"); 12 linkedlist.addFirst("java2"); 13 linkedlist.addFirst("java3"); 14 linkedlist.addFirst("java4"); 15 System.out.println(linkedlist); 16 17 LinkedList<String> linkedlist2 = new LinkedList<String>(); 18 linkedlist2.addLast("java1"); 19 linkedlist2.addLast("java2"); 20 linkedlist2.addLast("java3"); 21 linkedlist2.addLast("java4"); 22 System.out.println(linkedlist2); 23 24 25 } 26 27 }
1 练习 2 package blogtest2; 3 import java.util.HashSet; 4 public class HashSetTest { 5 6 /** 7 * 将自定义对象存入HashSet中,同姓名和年龄为重复元素 8 */ 9 public static void main(String[] args) { 10 HashSet<Student> hashset = new HashSet<Student>(); 11 hashset.add(new Student("zhangshan",22)); 12 hashset.add(new Student("lishan",24)); 13 hashset.add(new Student("wangwu",15)); 14 hashset.add(new Student("zhangshan",22)); 15 for(Student stu : hashset){ 16 System.out.println(stu); 17 } 18 19 } 20 21 } 22 class Student implements Comparable<Student>{ 23 private String name; 24 private int age; 25 Student(String name,int age){ 26 this.name = name; 27 this.age = age; 28 } 29 public String getName(){ 30 return name; 31 } 32 public int getAge(){ 33 return age; 34 } 35 public String toString(){ 36 return name +"...."+age; 37 } 38 public int hashCode(){ 39 return this.name.hashCode() + this.age * 30; 40 } 41 public boolean equals(Object stu){ 42 if(!(stu instanceof Student)){ 43 return false; 44 } 45 Student stucopy = (Student)stu; 46 return this.name.equals(stucopy.name) && this.age == stucopy.age; 47 } 48 public int compareTo(Student stu){ 49 int num = new Integer(this.age).compareTo(new Integer(stu.age )); 50 if(num == 0){ 51 return this.name.compareTo(stu.name); 52 } 53 return num; 54 } 55 }
1 练习 2 package blogtest2; 3 import java.util.*; 4 public class TreeSetTest { 5 6 /** 7 * 往TreeSet中存入自定义对象学生,按学生的年龄排序 8 */ 9 public static void main(String[] args) { 10 // TODO Auto-generated method stub 11 TreeSet<Student> treeset = new TreeSet<Student>(new MyComparator()); 12 treeset.add(new Student("zhangshan",22)); 13 treeset.add(new Student("lishan",24)); 14 treeset.add(new Student("wangwu",15)); 15 treeset.add(new Student("zhangshan",22)); 16 for(Student stu : treeset){ 17 System.out.println(stu); 18 } 19 20 } 21 } 22 class MyComparator implements Comparator<Student>{ 23 public int compare(Student stu1,Student stu2){ 24 int num = stu1.getName().compareTo(stu2.getName()); 25 if(num == 0){ 26 return new Integer(stu1.getAge()).compareTo(new Integer(stu2.getAge())); 27 } 28 return num; 29 } 30 } 31 class Student implements Comparable<Student>{ 32 private String name; 33 private int age; 34 Student(String name,int age){ 35 this.name = name; 36 this.age = age; 37 } 38 public String getName(){ 39 return name; 40 } 41 public int getAge(){ 42 return age; 43 } 44 public String toString(){ 45 return name +"...."+age; 46 } 47 Student stucopy = (Student)stu; 48 return this.name.equals(stucopy.name) && this.age == stucopy.age; 49 } 50 public int compareTo(Student stu){ 51 int num = new Integer(this.age).compareTo(new Integer(stu.age )); 52 if(num == 0){ 53 return this.name.compareTo(stu.name); 54 } 55 return num; 56 } 57 }
1 练习 2 package blogtest2; 3 import java.util.*; 4 public class HashMapTest { 5 /** 6 *演示Map集合两种取出方式 7 */ 8 public static void main(String[] args) { 9 Map<String,String> map = new HashMap<String,String>(); 10 map.put("zhangshan", "22"); 11 map.put("lisi", "33"); 12 map.put("wangwu", "44"); 13 map.put("xiaoming", "55"); 14 //第一种取出方式 15 Set<Map.Entry<String, String>> set = map.entrySet(); 16 Iterator<Map.Entry<String, String>> ite = set.iterator(); 17 while(ite.hasNext()){ 18 Map.Entry<String, String> mapentry = ite.next(); 19 String key = mapentry.getKey(); 20 String value = mapentry.getValue(); 21 System.out.println(key + "=" + value); 22 } 23 //第二种取出方式 24 Set<String> set2 = map.keySet(); 25 Iterator<String> ite2 = set2.iterator(); 26 while(ite2.hasNext()){ 27 String key2 = ite2.next(); 28 String value2 = map.get(key2); 29 System.out.println(key2 + "=" + value2); 30 } 31 } 32 }