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

xiaoxiao2021-02-28  89

在上一篇博客中介绍了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 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); } } 12345678910111213141516171819202122232425262728293031323334353637 12345678910111213141516171819202122232425262728293031323334353637

运行结果:

[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); } } 12345678910111213141516171819202122232425262728 12345678910111213141516171819202122232425262728

运行结果:

`[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); } } 12345678910111213141516171819202122 12345678910111213141516171819202122

运行结果:

[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); } } 12345678910111213141516171819202122232425262728293031 12345678910111213141516171819202122232425262728293031

运行结果:

[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); } } 1234567891011121314151617181920 1234567891011121314151617181920

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); } } 1234567891011121314151617181920 1234567891011121314151617181920

运行结果:

[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); } } 12345678910111213141516171819202122 12345678910111213141516171819202122

运行结果:

class Java.lang.String  [20]

TreeList

TreeList实现了优化的快速插入和删除任何索引的列表。这个列表内部实现利用树结构,确保所有的插入和删除都是O(log n)。

TypedList

·TypedList·修饰了另一个列表,使其只能添加指定类型的实例,修饰后的列表本质上就是使用InstanceofPredicate断言的PredicatedList。

UnmodifiableList

UnmodifiableList修饰另一个列表,使列表不允许被修改。

转载请注明原文地址: https://www.6miu.com/read-31995.html

最新回复(0)