网站建设方案下载,网站添加支付宝,wordpress适合外贸的主题,廊坊视频优化排名---------------------- android培训、java培训、期待与您交流#xff01; ---------------------- 一#xff0e;概述#xff1a; Java的集合类是一种特别有用的工具类#xff0c;它可以用于存储数量不等的多个对象#xff08;实际上是对象的引用#xff09;#xff0c…---------------------- android培训、java培训、期待与您交流 ---------------------- 一概述 Java的集合类是一种特别有用的工具类它可以用于存储数量不等的多个对象实际上是对象的引用并可以实现常用的数据结构如栈队列等。除此之外还可以用于保存具有映射关系的关联数组。Java的集合大致上可分为SetList和Map三种体系其中Set代表无序不可重复的集合List代表有序重复的集合而Map则代表具有映射关系的集合。 Java的集合类主要由两个接口派生而出Collection和Map。HashSetArrayList和HashMap是最常用的实现类 二Collection和Iterator接口 1. Collection接口定义的操作集合元素的常用方法 2. Iterator接口主要用于遍历即迭代访问Collection中的元素Iterator对象也被称为 迭代器。Iterator接口定义了如下三个方法 三Set接口 Set集合与Collection基本上完全一样它没有提供任何额外的方法。实际上Set就是Collection只是行为不同Set不允许出现重复元素。 Set判断两个对象相同不是使用运算符而是根据equals方法。也就是说如果只要两个对象用equals方法比较返回trueSet就不会接受这两个对象。 1. HashSet类 HashSet是Set接口的典型实现。 HashSet具有以下特点 a. 不能保证元素的排列顺序顺序有可能发生变化。 b. HashSet不是同步的。 c. 集合元素值可以是null。 HashSet集合判断两个元素相等的标准是两个对象通过equals方法比较相等并且两 个对象的hashCode方法返回值也相等。 注意如果需要某个类的对象保存到HashSet集合中重写这个类的equals方法和 hashCode方法时应该保证两个对象通过equals比较返回true时他们的hashCode 方法返回值也相等。 重写hashCode方法的基本原则 1. 当两个对象通过equals方法比较返回true时这两个对象的hashCode应该相等。 2. 对象中用作equals比较标准的属性都应该用来计算hashCode值。 2. TreeSet类 TreeSet是SortedSet接口的唯一实现TreeSet可以保证集合元素处于排序状态。 与HashSet相比TreeSet还提供了几个额外的方法 注意如果试图把一个对象添加进TreeSet时则该对象的类必须实现Comparable接口否 则程序可能跑出异常。 四List接口 List集合代表一个有序集合允许元素重复。List集合默认按元素的添加顺序设置元素的索引。 1. List接口和ListIterator接口 List集合常用方法 ListIterator接口继承了Iterator接口提供了专门操作List的方法。ListIterator接口在Iterator接口的基础上增加了以下方法 ListIterator增加了向前迭代的功能。 2. ArrayList和Vector实现类 ArrayList和Vector类都是基于数组实现的List类所以ArrayList和Vector类封装了 一个动态再分配的Object[ ]数组。 ArrayList是线程不安全的程序必须手动保证该集合的同步性Vector是线程安全的。 各种容器的性能比较 五Map Map用于保存具有映射关系的数据。Map的key不允许重复即同一个Map对象的任何两个key通过equals方法比较总是返回false。 Map接口定义的常用方法 Map中包含一个内部类Entry。该类封装了一个key--value对Entry包含三个方法 1. HashMap和Hashtable实现类 Hashtable和HashMap的区别 1. Hashtable是一个线程安全的实现但HashMap是线程不安全的实现。 2. Hashtable不允许使用null作为key和value如果试图把null值放进Hashtable 中将会引发NullPointrException异常但HashMap可以使用null作为key和value。 1 /*2 Map集合的两种取出方式3 1.Setk keySet()将Map集合中的所有键存入Set中疑问Set具备迭代器。4 所以可以根据迭代方式取出键再根据get方法获取每一个键对应的值。5 6 2.SetMap.Entryk,v entrySet()将Map集合中的映射关系存入到Set集合中7 而这个映射关系的数据类型就是Map.Entry。8 9 */
10
11 import java.util.*;
12
13 class Test1
14 {
15 public static void main(String[] args)
16 {
17 HashMapString,String hm new HashMapString,String();
18 hm.put(null,null);
19 hm.put(java01,3344);
20 hm.put(java02,3444);
21 hm.put(java03,33244);
22
23 SetString s hm.keySet();
24 IteratorString it s.iterator();
25 while (it.hasNext())
26 {
27 String key it.next();
28 System.out.println(key:hm.get(key));
29 }
30 }
31 }
32
33
34 class Test2
35 {
36 public static void main(String[] args)
37 {
38 HashMapString,String hm new HashMapString,String();
39 hm.put(null,null);
40 hm.put(java01,3344);
41 hm.put(java02,3444);
42 hm.put(java03,33244);
43 //将Map集合中的映射关系取出存入到Set集合中。
44 SetMap.EntryString,String s hm.entrySet();
45 IteratorMap.EntryString,String it s.iterator();
46 while (it.hasNext())
47 {
48 Map.EntryString,String me it.next();
49 String key me.getKey();
50 String value me.getValue();
51 System.out.println(key:value);
52 }
53 }
54 } 1 /*2 练习3 每一个学生都有对应的归属地4 学生Student地址String5 注意姓名和年龄相同的视为同一学生6 保证学生的唯一性。7 8 1. 描述学生类重写equals和hashCode方法。(有序则重写Comparable接口中的compareTo方法)9 2. 定义Map容器将学生作为键地址作为值存入。
10 3. 获取Map集合中的元素两种方式keySet和entrySet。
11
12 */
13
14
15 import java.util.*;
16
17 class Student
18 {
19 private String name;
20 private int age;
21
22 Student(String name, int age)
23 {
24 this.name name;
25 this.age age;
26 }
27 public String toString() //重写toString方法
28 {
29 return name:age;
30 }
31 public boolean equals(Object obj) //重写equals方法
32 {
33 if (!(obj instanceof Student))
34 {
35 throw new ClassCastException(类型不匹配);
36 }
37 Student s (Student)obj;
38 return this.name.equals(s.name)this.ages.age; //名字和年龄都相等则返回true
39 }
40 public int hashCode() //重写hashCode方法
41 {
42 return name.hashCode()age*17;
43 }
44
45 }
46
47 public class Practice
48 {
49 public static void main(String[] args)
50 {
51 MapStudent,String hm new HashMapStudent,String();
52 hm.put(new Student(xiaok,22),Beijing);
53 hm.put(new Student(xiaok,22),Nanjing);
54 hm.put(new Student(xiaoh,19),Tianjing);
55 hm.put(new Student(xiaol,23),Fuzhou);
56
57 //获取Map元素第一种方式keySet
58 SetStudent s hm.keySet();
59 IteratorStudent it s.iterator();
60 while (it.hasNext())
61 {
62 Student key it.next();
63 System.out.println(key:::hm.get(key));
64 }
65
66 System.out.println(###########################);
67
68 //获取Map元素第二种方式entrySet
69
70 SetMap.EntryStudent,String entrySet hm.entrySet();
71 IteratorMap.EntryStudent,String it2 entrySet.iterator();
72 while (it2.hasNext())
73 {
74 Map.EntryStudent,String me it2.next();
75 Student ss me.getKey();
76 String addr me.getValue();
77 System.out.println(ss.....addr);
78 }
79 }
80 } 1 /*2 练习3 获取字符串中字母出现的次数4 如adfadff 打印a(2)d(2)f(3)...5 6 思路7 1. 将字符串转换成字符数组因为要对每一个字母进行操作。8 2. 定义一个Map集合因为打印结果的字母有顺序所以使用TreeMap集合。9 3. 遍历字符数组
10 将每一个字母作为键去查Map集合,get(key)方法。
11 如果返回null将该字母和1存入到Map集合
12 如果返回不是null说明该字母在Map集合中存在并有对应次数。
13 那么就获取该次数并进行自增然后将该字母和自增后的次数存入到Map集合中
14 4. 将Map集合中的数据编程指定的字符串形式返回。
15
16 */
17 import java.util.*;
18
19 public class Practice2
20 {
21 public static void main(String[] args)
22 {
23 charCount(adfadfads);
24 }
25
26 public static void charCount(String str)
27 {
28 char[] chArr str.toCharArray(); //将字符串转换成字符数组
29 TreeMapCharacter,Integer tm new TreeMapCharacter,Integer(); //定义一个Map集合
30 for (int i0; ichArr.length ; i ) //遍历字符数组
31 {
32 Integer valuetm.get(chArr[i]);
33 if (valuenull)
34 {
35 tm.put(chArr[i],1);
36 }
37 else
38 {
39 value;
40 tm.put(chArr[i],value);
41 }
42 }
43 SetMap.EntryCharacter,Integer entrySet tm.entrySet();
44 IteratorMap.EntryCharacter,Integer it entrySet.iterator();
45 while (it.hasNext())
46 {
47 Map.EntryCharacter,Integer me it.next();
48 Character key me.getKey();
49 Integer value me.getValue();
50 System.out.print(key(value));
51 }
52
53 }
54 } ---------------------- android培训、java培训、期待与您交流 ---------------------- 转载于:https://www.cnblogs.com/MercyK/archive/2013/04/29/3014737.html