Because I am constantly busy working on something, I have never had time to actually put everything in words and pictures. But, since you got here, then you must have already seen some part of my work - and this is the way I’m talking.I'm 23, born in Romania, student at UPG Romania in software development field. I started from 0, mostly with basic stuff, and I’m evolving every day to an expert. I'm focused on freelancing projects, from small websites, to really heavy stuff. I know that I look and act differently from most developers, but this is why you will love to work with me! Constantin has posted 42 posts at DZone. You can read more from them at their website. View Full User Profile

Creating a Fixed Sized List in Java Using Apache Commons

03.02.2012
| 4531 views |
  • submit to reddit
There are many ways to create a fixed list in Java, one of them is by using the FixedSizeList class, found in the Apache org.apache.commons.collections package (http://commons.apache.org/collections/). FixedSizeList does not support the add, remove, clear and retain methods and the set method is allowed as far as it doesn’t change the list size:
package fixedsizelist.exemple;

import java.util.Arrays;
import java.util.List;
import org.apache.commons.collections.list.FixedSizeList;

public class FixedSizeListExemple {

    public static void main(String[] args) {

        List fixedList = FixedSizeList.decorate(Arrays.asList(new String[5]));

        fixedList.set(0, "[email protected]");
        fixedList.set(1, "[email protected]");
        fixedList.set(2, "[email protected]");
        fixedList.set(3, "[email protected]");
        fixedList.set(4, "[email protected]");

        System.out.println("\tDisplay list values...\n");
        for (String obj : fixedList) {
            System.out.println(obj.toString());
        }

        try {
            System.out.println("\n\tTrying to modify our fixed list...\n");

            fixedList.add("[email protected]");
            fixedList.remove(3);
            fixedList.clear();

        } catch (Exception e) {
          System.out.println
          (" The add, remove, clear and retain operations are unsupported."
          + "\nThe set method is allowed (as it doesn't change the list size).\n");
        }
    }
}


And the output is:

From http://e-blog-java.blogspot.com/2012/02/how-to-create-fixed-list-in-java-using.html

Published at DZone with permission of its author, Constantin Alin.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Adedayo Ominiyi replied on Fri, 2012/03/02 - 3:38am

I don't think that it is necessary to wrap the return value of Arrays.asList(newString[5]) in a FixedSizedList as the return value is already immutable.

Jean-Baptiste Nizet replied on Fri, 2012/03/02 - 1:19pm in response to: Adedayo Ominiyi

It's not immutable, but it's indeed fixed size already.

Neal Johnson replied on Sun, 2012/03/04 - 4:44am

 

Apache Collections does have some nice features but I find this one a little redundant. You can do exactly the same thing with standard collections and to some extent its a little bit more elegant:

        List<String> fixedList = Arrays.asList(
                "[email protected]",
                "[email protected]",
                "[email protected]",
                "[email protected]",
                "[email protected]");
        fixedList = Collections.unmodifiableList(fixedList); 
        ... rest of the test code here

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.