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

来源:互联网 发布:雅马哈合成器 知乎 编辑:程序博客网 时间:2024/06/05 11:01

在上一篇博客中介绍了commons-collections中的Bag相关内容,这一篇将为大家介绍List相关的实现类。

CursorableLinkedList

CursorableLinkedListList的一种实现,提供了一个列表迭代器并且允许修改列表。CursorableLinkedList支持所有可选列表的操作。它继承自AbstractLinkedList,提供了stack/queue/dequeue的操作。这个类的主要功能是能够在同一时间修改列表和迭代器。listIterator()cursor()方法都提供了访问一个继承自ListIteratorCursor实例。游标允许更改列表时并发修改迭代器。需要注意的是,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);   }}
  • 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方法时,如果索引超出列表长度,列表会自动增长,我们可以通过一个工厂获得超出索引位置的值。LazyListGrowthList都可以实现对修饰的列表进行增长,但是LazyList发生在get时候,而GrowthList发生在setadd时候,我们也可以混合使用这两种列表。

使用示例:

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修饰另一个列表,在addset的时候对元素进行转换后在执行相关操作。

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修饰另一个列表,使列表不允许被修改。

阅读全文
0 0
原创粉丝点击