JavaBlog.fr / Java.lu DEVELOPMENT,Java Apache: Commons collection (example ListUtils lazyList)

Apache: Commons collection (example ListUtils lazyList)

A simple post concerning a very good class ListUtils in the library commons-collections-3.2.jar of Apache for the collection’s manipulation.

The method lazyList returns a “lazy” list whose elements will be created on demand.

public static void main(String[] args) {
		
	// Example n°1 with a customized Factory
	{
		System.out.println("------ Example 1 with a customized Factory -------");
		Factory factory = new Factory() {
		     public Object create() {
		    	 try{Thread.currentThread().sleep(1000);}catch (Throwable e) {e.printStackTrace();};
		    	 return new GregorianCalendar().getInstance();
		     }
		 };
		 Object obj = null;
		 
		List<Calendar> listCal = new ArrayList<Calendar>();
		System.out.println("1. listCal.size()="+listCal.size());
		listCal = ListUtils.lazyList(listCal, factory);
		System.out.println("2. listCal.size()="+listCal.size());
		//
		obj = listCal.get(3);
		System.out.println(obj instanceof GregorianCalendar);
		System.out.println(((GregorianCalendar)obj).getTime().toString());
		System.out.println("3. listCal.size()="+listCal.size());
		//
		obj = listCal.get(5);
		System.out.println(obj instanceof GregorianCalendar);
		System.out.println(((GregorianCalendar)obj).getTime().toString());
		System.out.println("4. listCal.size()="+listCal.size());
		//
		listCal.remove(1);
		System.out.println("5. listCal.size()="+listCal.size());
		listCal.remove(3);
		System.out.println("6. listCal.size()="+listCal.size());
		// 
		obj = listCal.get(4);
		System.out.println(obj instanceof GregorianCalendar);
		System.out.println(((GregorianCalendar)obj).getTime().toString());
		System.out.println("7. listCal.size()="+listCal.size());
	}

	// Example n°2 with factory from Apache library
	{
		System.out.println("------ Example 2 with factory from Apache library -------");
		List<Calendar> listCal = new ArrayList<Calendar>();
		listCal = ListUtils.lazyList(listCal, FactoryUtils.instantiateFactory(GregorianCalendar.class));
		Object obj1 = listCal.get(1);
		System.out.println(obj1 instanceof GregorianCalendar);
		System.out.println(((GregorianCalendar)obj1).getTime().toString());
		//
		Object obj12 = listCal.get(12);
		System.out.println(obj12 instanceof GregorianCalendar);
		System.out.println(((GregorianCalendar)obj12).getTime().toString());
	}		
}	

So, the outputs in console would be:

------ Example 1 with a customized Factory -------
1. listCal.size()=0
2. listCal.size()=0
true
Mon May 14 13:40:32 CEST 2012
3. listCal.size()=4
true
Mon May 14 13:40:33 CEST 2012
4. listCal.size()=6
5. listCal.size()=5
6. listCal.size()=4
true
Mon May 14 13:40:34 CEST 2012
7. listCal.size()=5
------ Example 2 with factory from Apache library -------
true
Mon May 14 13:40:34 CEST 2012
true
Mon May 14 13:40:34 CEST 2012

Some comments:
– after the code

 obj = listCal.get(3); 

, the list is initialized with 4 elements ( from index 0 to index 3).
– after the removing of an element in list, the objects with an index more great than the index of element removed (coming after in the list), are “moved” in the list,
– this component could be used in the customized need of “pool of objects” whose the number of elements could be increased on demand…

Source: Apache Commons Collections

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

Related Post