第五十八篇:commons-collections使用介绍之List
2017-08-31 11:45
1026 查看
在上一篇博客中介绍了commons-collections中的
使用示例:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
运行结果:
使用示例:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
运行结果:
`[11, 22, 33, 44]
列表最大长度:4
[55, 22, 33, 44]
使用示例:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
运行结果:
使用示例:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
运行结果:
使用示例:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
使用示例:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
运行结果:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
运行结果:
·TypedList·修饰了另一个列表,使其只能添加指定类型的实例,修饰后的列表本质上就是使用
Bag相关内容,这一篇将为大家介绍
List相关的实现类。
CursorableLinkedList
CursorableLinkedList是
List的一种实现,提供了一个列表迭代器并且允许修改列表。
CursorableLinkedList支持所有可选列表的操作。它继承自
AbstractLinkedList,提供了stack/queue/dequeue的操作。这个类的主要功能是能够在同一时间修改列表和迭代器。
listIterator()和
cursor()方法都提供了访问一个继承自
ListIterator的
Cursor实例。游标允许更改列表时并发修改迭代器。需要注意的是,
iterator()方法和子列表不会提供这种光标行为。
CursorableLinkedList不是线程同步的。
使用示例:
package com.gujin.collections; import java.util.Iterator; import org.apache.commons.collections.list.CursorableLinkedList; import org.apache.commons.collections.list.CursorableLinkedList.Cursor; import org.junit.Test; public class CursorableLinkedListTest { @Test 4000 public void test() { CursorableLinkedList linkedList = new CursorableLinkedList(); linkedList.add("center"); linkedList.addFirst("first"); linkedList.addLast("last"); System.out.println(linkedList); Cursor cursor = linkedList.cursor(); if (cursor.hasNext()) { Object o = cursor.next(); System.out.println("使用游标移除元素:" + o); cursor.remove(); } cursor.close(); System.out.println(linkedList); Iterator<?> iterator = linkedList.iterator(); if (iterator.hasNext()) { Object o = iterator.next(); System.out.println("使用迭代器移除元素:" + o); iterator.remove(); } System.out.println(linkedList); } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
运行结果:
[first, center, last] 使用游标移除元素:first [center, last] 使用迭代器移除元素:center [last]
FixedSizeList
FixedSizeList修饰另一个列表防止添加/删除并且固定列表大小。add、remove、clear和retain操作是不被支持的,
set方法是允许的但是不会影响列表大小。
使用示例:
package com.gujin.collections; import java.util.ArrayList; import org.apache.commons.collections.list.FixedSizeList; import org.junit.Test; public class FixedSizeListTest { @Test public void test() { ArrayList<String> src = new ArrayList<String>(); src.add("11"); src.add("22"); src.add("33"); src.add("44"); FixedSizeList fixedSizeList = (FixedSizeList) FixedSizeList.decorate(src); System.out.println(fixedSizeList); System.out.println("列表最大长度:" + fixedSizeList.maxSize()); // 更改列表指定索引位置元素 fixedSizeList.set(0, "55"); System.out.println(fixedSizeList); // 移除元素,不被支持 // fixedSizeList.remove(0); } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
运行结果:
`[11, 22, 33, 44]
列表最大长度:4
[55, 22, 33, 44]
GrowthList
GrowthList修饰另一个列表,可以使其在因set或add操作造成索引超出异常时无缝的增加列表长度,可以避免大多数的
IndexOutOfBoundsException。
使用示例:
package com.gujin.collections; import java.util.ArrayList; import org.apache.commons.collections.list.GrowthList; import org.junit.Test; public class GrowthListTest { @Test public void test() { ArrayList<String> src = new ArrayList<String>(); src.add("11"); src.add("22"); GrowthList growthList = (GrowthList) GrowthList.decorate(src); System.out.println(growthList); // 索引超出,自动增长 growthList.set(3, "44"); System.out.println(growthList); } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
运行结果:
[11, 22] [11, 22, null, 44]
LazyList
LazyList修饰另一个列表,当调用
get方法时,如果索引超出列表长度,列表会自动增长,我们可以通过一个工厂获得超出索引位置的值。
LazyList和
GrowthList都可以实现对修饰的列表进行增长,但是
LazyList发生在
get时候,而
GrowthList发生在
set和
add时候,我们也可以混合使用这两种列表。
使用示例:
package com.gujin.collections; import java.util.ArrayList; import org.apache.commons.collections.Factory; import org.apache.commons.collections.list.LazyList; import org.junit.Test; public class LazyListTest { @Test public void test() { ArrayList<String> src = new ArrayList<String>(); src.add("11"); src.add("22"); Factory factory = new Factory() { @Override public Object create() { return "new"; } }; LazyList lazyList = (LazyList) LazyList.decorate(src, factory); System.out.println(lazyList); // 获得超出索引位置的元素,自动增长 System.out.println(lazyList.get(3)); System.out.println(lazyList); } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
运行结果:
[11, 22] new [11, 22, null, new]
PredicatedList
PredicatedList修饰另一个列表,在执行
add的时候对添加元素进行校验,校验不通过则抛出
IllegalArgumentException异常。
commons-collections已经为我们提供了一些验证的实现:
类名 | 说明 |
---|---|
AllPredicate | 多个断言组合,如果断言数组为空则允许所有元素,否则只有当所有断言都为真才允许添加 |
AndPredicate | 两个断言组合,当断言都为真时允许添加,否则不允许 |
AnyPredicate | 多个断言组合,如果断言数组为空则不允许所有元素,否则只要有一个断言为真则允许添加 |
EqualPredicate | 如果添加的元素和断言中的元素相等(equals),则允许添加 |
ExceptionPredicate | 总是会抛出一个FunctorException异常 |
FalsePredicate | 不允许添加任何元素 |
IdentityPredicate | 如果添加的元素和断言中的元素为同一个(==),则允许添加 |
InstanceofPredicate | 允许添加指定类型的实例 |
NonePredicate | 多个断言组合,如果断言数组为空则允许所有元素,否则只要有一个断言为真则不允许添加, |
NotNullPredicate | 元素不为null则允许添加 |
NotPredicate | 断言为真则不允许添加 |
NullIsExceptionPredicate | 元素为null,则抛出一个FunctorException异常,否则断言为真则允许添加 |
NullIsFalsePredicate | 元素为null,则不允许添加,否则断言为真则允许添加 |
NullIsTruePredicate | 元素为null或断言为真允许添加 |
NullPredicate | 仅允许添加null元素 |
OnePredicate | 多个断言组合,如果断言数组为空则不允许所有元素,否则只有一个断言为真时允许添加 |
OrPredicate | 两个断言组合,当断言有一个为真时允许添加,否则不允许 |
TransformedPredicate | 对元素进行变换后断言为真则允许添加 |
TransformerPredicate | 对元素进行变换后,变换结果为真则允许添加 |
TruePredicate | 允许添加所有元素 |
UniquePredicate | 不允许添加重复元素 |
package com.gujin.collections; import java.util.ArrayList; import org.apache.commons.collections.functors.NotNullPredicate; import org.apache.commons.collections.list.PredicatedList; import org.junit.Test; public class PredicatedListTest { @Test public void test() { PredicatedList predicatedList = (PredicatedList) PredicatedList.decorate( new ArrayList<String>(), NotNullPredicate.INSTANCE); predicatedList.add("123"); // 抛出异常 // predicatedList.add(null); } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
SetUniqueList
SetUniqueList实现了一个不允许重复元素的列表,有点和
Set类似。
使用示例:
package com.gujin.collections; import java.util.ArrayList; import org.apache.commons.collections.list.SetUniqueList; import org.junit.Test; public class SetUniqueListTest { @Test public void test() { SetUniqueList setUniqueList = SetUniqueList .decorate(new ArrayList<String>()); setUniqueList.add("11"); setUniqueList.add("11"); setUniqueList.add("22"); System.out.println(setUniqueList); } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
运行结果:
[11, 22]
SynchronizedList
SynchronizedList修饰另一个列表,使其可以在多线程环境下同步。和
Vector类似。
TransformedList
TransformedList修饰另一个列表,在
add和
set的时候对元素进行转换后在执行相关操作。
package com.gujin.collections; import java.util.ArrayList; import org.apache.commons.collections.functors.StringValueTransformer; import org.apache.commons.collections.list.TransformedList; import org.junit.Test; public class TransformedListTest { @Test public void test() { TransformedList transformedList = (TransformedList) TransformedList .decorate(new ArrayList<String>(), StringValueTransformer.getInstance()); // 添加时会将其转换为字符串 transformedList.add(new Integer(20)); System.out.println(transformedList.get(0).getClass()); System.out.println(transformedList); } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
运行结果:
class Java.lang.String [20]
TreeList
TreeList实现了优化的快速插入和删除任何索引的列表。这个列表内部实现利用树结构,确保所有的插入和删除都是O(log n)。
TypedList
·TypedList·修饰了另一个列表,使其只能添加指定类型的实例,修饰后的列表本质上就是使用InstanceofPredicate断言的
PredicatedList。
UnmodifiableList
UnmodifiableList修饰另一个列表,使列表不允许被修改。
相关文章推荐
- commons-collections使用介绍之List
- 第五十九篇:commons-collections使用介绍之Map
- 关于使用工具类org.apache.commons.collections.ListUtils合并List的问题
- commons-collections使用介绍之Bag HashBag
- 使用org.apache.commons.collections.CollectionUtils进行List与List比较
- commons-collections使用介绍之Bag
- commons-collections使用介绍之Map
- 第五十七篇:commons-collections使用介绍之Bag
- org/apache/commons/collections/map/ListOrderedMap
- Collections.unmodifiableList方法的使用与场景
- 使用Collections对list的内容进行排序
- layer-list -- layer-list的基本使用介绍
- Java 集合系列之 LinkedList详细介绍(源码解析)和使用示例
- Apache commons net 包介绍和简单使用
- Java collections使用介绍
- Java初学习 - Collections.unmodifiableList方法的使用与场景
- Python使用os.listdir()函数来得目录内容的介绍
- python之列表(list)的使用方法介绍
- JSON调试找不到 net.sf.ezmorph.Morpher与org/apache/commons/collections/map/ListOrderedMap这个异常。
- commons-beanutils使用介绍