标签:move 多线程 eal lag return for循环 string 技术 print
转载至:
Java的集合就像是一种容器,可以把对个对象的引用放入容器中,其中不断可以存储不等的多个对象,还可以用于保存具有映射关系的关联数组。其中Java的集合可以分为三种体系:
Collection接口是List,Set和Queue接口的父接口,该接口中定义的方法可以用户操作List,Set和Queue集合;其中主要有以下的一些方法:

这里给出测试上述部分方法的举例:
首先给出Person类,用于放入集合中
1 package collection;
2
3 /**
4 * Created by : Zhong
5 * DATE : 2017/3/2
6 * Time : 23:06
7 * Funtion :
8 */
9 public class Person {
10 public String name;
11 public int age;
12
13
14 public Person() {
15
16 }
17
18 public Person(String name, int age) {
19 this.name = name;
20 this.age = age;
21 }
22
23
24 public String getName() {
25 return name;
26 }
27
28 public void setName(String name) {
29 this.name = name;
30 }
31
32
33 public int getAge() {
34 return age;
35 }
36
37 public void setAge(int age) {
38 this.age = age;
39 }
40
41 @Override
42 public String toString() {
43 return "Person{" +
44 "name=‘" + name + ‘\‘‘ +
45 ", age=" + age +
46 ‘}‘;
47 }
48
49 @Override
50 public boolean equals(Object o) {
51 if (this == o) return true;
52 if (o == null || getClass() != o.getClass()) return false;
53
54 Person person = (Person) o;
55
56 if (age != person.age) return false;
57 return name != null ? name.equals(person.name) : person.name == null;
58
59 }
60
61 @Override
62 public int hashCode() {
63 int result = name != null ? name.hashCode() : 0;
64 result = 31 * result + age;
65 return result;
66 }
67 }
然后举例说明Collection接口的相关方法:
1 package collection;
2
3 import org.junit.Test;
4
5 import java.util.ArrayList;
6 import java.util.Collection;
7 import java.util.Iterator;
8
9 /**
10 * Created by :Infaraway
11 * DATE : 2017/3/26
12 * Time : 12:35
13 * Funtion :
14 */
15 public class CollectionTest {
16
17
18
19 @Test
20 public void testTool(){
21 Collection collection = new ArrayList();
22 Collection collection2 = new ArrayList();
23 Person person = new Person("Tom", 22);
24 collection.add(person);
25 collection.add(new Person("Jerry", 25));
26 collection.add(new Person("Mike", 32));
27 collection.add("infaraway");
28
29 //1. contains(Object obj):利用equals方法比较,查看集合中有没有指定元素
30 boolean flag = collection.contains(new Person("Mike", 32));
31 System.out.println(flag);
32 flag = collection.contains(person);
33 System.out.println(flag);
34
35 //2. containsAll(Collection coll)利用equals方法比较,查看集合中有没有指定元素的集合
36 collection2.add(person);
37 collection2.add("infaraway");
38 flag = collection.containsAll(collection2);
39 System.out.println(flag);
40
41 //3. isEmpty():检查集合是否为空
42 flag = collection.isEmpty();
43 System.out.println(flag);
44
45 //4. toArray()转化集合为数组
46 Object [] object = collection.toArray();
47 System.out.println(object.length);
48
49 //toArray(T[] a)
50 }
51
52
53 /**
54 * 1. clear() 清空集合
55 * 2. remove() :移除指定的元素,通过equals() 方法在集合中查找指定的元素
56 * 如果元素存在,则移除
57 * 3. removeAll(Collection coll) 移除coll中有的元素
58 * 4. retainAll(Collection coll) 保存coll中有的元素
59 */
60 @Test
61 public void testRemove(){
62
63 Collection collection = new ArrayList();
64 Collection collection2 = new ArrayList();
65 Person person = new Person("Tom", 22);
66 collection.add(person);
67 collection.add(new Person("Jerry", 25));
68 collection.add(new Person("Mike", 32));
69 collection.add("infaraway");
70
71 /* System.out.println(collection.size());
72 collection.remove(person);*/
73
74 System.out.println(collection.size());
75
76 collection2.add(person);
77 collection2.add("infaraway");
78
79 collection.removeAll(collection2);
80 System.out.println(collection.size());
81 }
82
83 /**
84 * 在Collection中无法获取指定的元素,但是可以遍历所有的元素
85 * 1. 使用增强for循环遍历
86 * 2. 使用Iterator迭代器遍历
87 * 2.1 获取迭代器对象,调用Collection的iterator方法,获取Iterator接口的实现
88 * 2.2 调用Iterator接口方法进行迭代
89 */
90 @Test
91 public void testIterator(){
92
93 Collection collection = new ArrayList();
94 Collection collection2 = new ArrayList();
95 collection.add("infaraway");
96
97 Person person = new Person("Tom", 22);
98 collection.add(person);
99 collection.add(new Person("Jerry", 25));
100 collection.add(new Person("Mike", 32));
101
102 //使用addAll方法
103 collection2.addAll(collection);
104
105 /* System.out.println(collection.size());
106 for (Object o : collection) {
107 System.out.println(o);
108 }*/
109
110 Iterator it = collection2.iterator();
111 while(it.hasNext()){
112 System.out.println(it.next());
113 }
114
115
116 }
117
118 /**
119 * add()方法
120 */
121 @Test
122 public void testCollection(){
123 Collection collection = new ArrayList();
124 collection.add("infaraway");
125 collection.add(new Person("Tom", 22));
126 collection.add(new Person("Jerry", 25));
127 collection.add(new Person("Mike", 32));
128 }
129
130 }
Set集合不允许包含相同的元素,如果把两个相同的元素添加进入Set集合,则添加操作失败。Set集合中判断两个对象是否相同不是使用“==” 而是根据equals()方法的返回值决定;
HashSet 是 Set 接口的典型实现,大多数时候使用 Set 集合时都使用这个实现类。
HashSet 按 Hash 算法来存储集合中的元素,因此具有很好的存取和查找性能。
HashSet 具有以下特点:
当向 HashSet 集合中存入一个元素时,HashSet 会调用该对象的 hashCode() 方法来得到该对象的 hashCode 值,然后根据 hashCode 值决定该对象在 HashSet 中的存储位置。如果两个元素的 equals() 方法返回 true,但它们的 hashCode() 返回值不相等,hashSet 将会把它们存储在不同的位置,但依然可以添加成功。
重写 hashCode() 方法的基本原则
package collection; import org.junit.Test; import java.util.HashSet; import java.util.Set; /** * Created by :Infaraway * DATE : 2017/3/26 * Time : 13:18 * Funtion : 测试HashSet * * 关于HashSet * 1. HashSet是Set的最典型的实现 * 2. HashSet中不能有重复的元素,判断两个元素相等的标准是equals()方法返回true * 3. HashSet根据hashCode()值来存储元素,因此不能保证元素的顺序 * 4. 如果两个对象通过equals()方法返回true 这两个对象的hashcode值应该是相同的 * 5. HashSet 是线程不安全的 */ public class HashSetTest { @Test public void testAdd(){ Set set = new HashSet(); set.add(new Person("AAA",22)); set.add(new Person("BBB",22)); set.add(new Person("CCC",27)); set.add(new Person("DDD",29)); System.out.println(set.size()); for (Object person : set) { System.out.println(person.toString()); } } }
LinkedHashSet 集合根据元素的hashCode值来决定元素的存储位置,但它同时使用链表维护元素的次序,这使得元素看起来是以插入顺序保存的。
LinkedHashSet 性能插入性能略低于 HashSet,但在迭代访问 Set 里的全部元素时有很好的性能。
LinkedHashSet 不允许集合元素重复。
TreeSet 是 SortedSet 接口的实现类,TreeSet 可以确保集合元素处于排序状态。
其中TreeSet还有以下的一些常用的操作:这里就不一一列举实现了
TreeSet 支持两种排序方法:自然排序和定制排序。默认情况下,TreeSet 采用自然排序。
排序:TreeSet 会调用集合元素的 compareTo(Object obj) 方法来比较元素之间的大小关系,然后将集合元素按升序排列,如果试图把一个对象添加到 TreeSet 时,则该对象的类必须实现 Comparable 接口。实现 Comparable 的类必须实现 compareTo(Object obj) 方法,两个对象即通过 compareTo(Object obj) 方法的返回值来比较大小。
实现Compareable接口代码示例:
1 package collection;
2
3 /**
4 * Created by : Zhong
5 * DATE : 2017/3/2
6 * Time : 23:06
7 * Funtion :
8 */
9 public class PersonCompare implements Comparable{
10 public String name;
11 public int age;
12 public PersonCompare() {
13 }
14 public PersonCompare(String name, int age) {
15 this.name = name;
16 this.age = age;
17 }
18 public String getName() {
19 return name;
20 }
21 public void setName(String name) {
22 this.name = name;
23 }
24 public int getAge() {
25 return age;
26 }
27 public void setAge(int age) {
28 this.age = age;
29 }
30 @Override
31 public String toString() {
32 return "Person{" +
33 "name=‘" + name + ‘\‘‘ +
34 ", age=" + age +
35 ‘}‘;
36 }
37
38 @Override
39 public boolean equals(Object o) {
40 if (this == o) return true;
41 if (o == null || getClass() != o.getClass()) return false;
42
43 PersonCompare person = (PersonCompare) o;
44
45 if (age != person.age) return false;
46 return name != null ? name.equals(person.name) : person.name == null;
47
48 }
49 @Override
50 public int hashCode() {
51 int result = name != null ? name.hashCode() : 0;
52 result = 31 * result + age;
53 return result;
54 }
55
56 @Override
57 public int compareTo(Object o) {
58
59 if (o instanceof PersonCompare){
60 PersonCompare personCompare = (PersonCompare) o;
61 return this.name.compareTo(personCompare.name);
62 }else{
63 throw new ClassCastException("转换类型失败!");
64 }
65 }
66 }
自然排序方法的使用示例:
/** * 自然排序情况 * * 默认情况下TreeSet要求集合中的元素必须实现Comparable接口 * Comparable接口中只有一个方法: * public int compareTo(T o) * 如果返回0 代表两个元素相等 * 如果返回正数,代表当前元素大 * 如果返回负数,代表当前元素小 * TreeSet 会调用每个元素的compareTo()方法去和集合中的每个已经有的元素比较,进而决定当前元素在集合中的位置 * */ @Test public void testTreeSet(){ TreeSet treeSet = new TreeSet(); treeSet.add(new PersonCompare("AAA",16)); treeSet.add(new PersonCompare("BBB",13)); treeSet.add(new PersonCompare("CCC",11)); treeSet.add(new PersonCompare("DDD",15)); for (Object o : treeSet) { System.out.println(o.toString()); } }
Comparable 的典型实现:
因为只有相同类的两个实例才会比较大小,所以向 TreeSet 中添加的应该是同一个类的对象。当需要把一个对象放入 TreeSet 中,重写该对象对应的 equals() 方法时,应保证该方法与 compareTo(Object obj) 方法有一致的结果:如果两个对象通过 equals() 方法比较返回 true,则通过 compareTo(Object obj) 方法比较应返回 0
如果需要实现定制排序,则需要在创建 TreeSet 集合对象时,提供一个 Comparator 接口的实现类对象。由该 Comparator 对象负责集合元素的排序逻辑。定制排序的优点就是让需要排序的对象不需要实现Compareable接口,减少了耦合性,是程序更加简单。
定制排序方法代码示例:
/** * 定制排序 * */ @Test public void testTreeSet2(){ //创建comparator接口的实现类对象 Comparator comparator = new Comparator() { @Override public int compare(Object o1, Object o2) { if (o1 instanceof Person && o2 instanceof Person){ Person p1 = (Person)o1; Person p2 = (Person)o2; return p1.getAge() - p2.getAge(); } throw new ClassCastException("类型转换异常"); } }; //创建TreeSet对象,传入Comparator接口的实现类对象 TreeSet treeSet = new TreeSet(comparator); treeSet.add(new Person("AAA",16)); treeSet.add(new Person("BBB",13)); treeSet.add(new Person("CCC",11)); treeSet.add(new Person("DDD",15)); for (Object person : treeSet) { System.out.println(person.toString()); } }
ArrayList 和 Vector 是 List 接口的两个典型实现
区别:
Arrays.asList(…) 方法返回的 List 集合即不是 ArrayList 实例,也不是 Vector 实例。 Arrays.asList(…) 返回值是一个固定长度的 List 集合。
常用方法:
LinkedHashMap 是 HashMap 的子类
LinkedHashMap 可以维护 Map 的迭代顺序:迭代顺序与 Key-Value 对的插入顺序一致
1 @Test
2 public void testLinkedHashMap(){
3 Map map = new LinkedHashMap<>();
4
5 map.put("CC", new Person("CC", 18));
6 map.put("AA", new Person("AA", 10));
7 map.put("DD", new Person("DD", 12));
8 map.put("BB", new Person("BB", 16));
9
10 /*Iterator it = map.keySet().iterator();
11 while (it.hasNext()){
12 Object key = it.next();
13 Object value = map.get(key);
14 System.out.println(key+":"+value);
15 }
16 System.out.println();*/
17
18 for (Object key : map.keySet()){
19 Object value = map.get(key);
20 System.out.println(key+":"+value);
21 }
22 }
TreeMap 存储 Key-Value 对时,需要根据 Key 对 key-value 对进行排序。TreeMap 可以保证所有的 Key-Value 对处于有序状态。
TreeMap 的 Key 的排序:
同样这里的key值也需要具有可比性
1 @Test
2 public void testTreeMap(){
3 Map map = new TreeMap<>();
4
5 //这里使用加入Compareable接口的Person类,这样才可以进行排序
6 map.put(new PersonCompare("CC", 18),"CC");
7 map.put(new PersonCompare("AA", 10),"AA");
8 map.put(new PersonCompare("DD", 12),"DD");
9 map.put(new PersonCompare("BB", 16),"BB");
10
11 for (Object key : map.keySet()){
12 Object value = map.get(key);
13 System.out.println(key+":"+value);
14 }
15 }
Collections 是一个操作 Set、List 和 Map 等集合的工具类
Collections 中提供了大量方法对集合元素进行排序、查询和修改等操作,还提供了对集合对象设置不可变、对集合对象实现同步控制等方法
Collections 类中提供了多个 synchronizedXxx()方法,该方法可使将指定集合包装成线程同步的集合,从而可以解决多线程并发访问集合时的线程安全问题
代码示例:
1 package collection;
2
3 import org.junit.Test;
4
5 import java.util.*;
6
7 /**
8 * Created by :Infaraway
9 * DATE : 2017/3/26
10 * Time : 16:39
11 * Funtion :
12 */
13 public class CollectionsTest {
14
15 @Test
16 public void testList(){
17 List list = new ArrayList<>();
18 list.add(new Person("AAA",16));
19 list.add(new Person("BBB",13));
20 list.add(new Person("CCC",11));
21 list.add(new Person("DDD",15));
22
23 for (Object o : list) {
24 System.out.println(o.toString());
25 }
26 //按年龄升序排列
27 //实现Comparator类进行排序操作
28 Collections.sort(list, new Comparator() {
29 @Override
30 public int compare(Object o1, Object o2) {
31 if (o1 instanceof Person && o2 instanceof Person){
32 Person p1 = (Person)o1;
33 Person p2 = (Person)o2;
34 return p1.getAge() - p2.getAge();
35 }
36 throw new ClassCastException("类型转换异常");
37 }
38 });
39
40 System.out.println();
41 for (Object o : list) {
42 System.out.println(o.toString());
43 }
44
45
46 //获取list中最小的元素
47 //要求集合中的元素都实现Compareable接口
48 Set set = new HashSet();
49
50 set.add(new PersonCompare("AAA",20));
51 set.add(new PersonCompare("BBB",34));
52 set.add(new PersonCompare("CCC",27));
53 set.add(new PersonCompare("DDD",29));
54
55 for (Object person : set) {
56 System.out.println(person.toString());
57 }
58 System.out.println();
59
60 Object object = Collections.min(set);
61 System.out.println(object.toString());
62 }
63
64 }
本文所涉及的代码可以在这里找到: https://git.oschina.net/infaraway/basisJava/tree/master/src/collection
标签:move 多线程 eal lag return for循环 string 技术 print
原文地址:http://www.cnblogs.com/alsf/p/6624721.html