您的位置:首页 > 编程语言 > Java开发

第五十八篇:commons-collections使用介绍之List

2017-08-31 11:45 881 查看
在上一篇博客中介绍了commons-collections中的
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
修饰另一个列表,使列表不允许被修改。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java