Home | FAQ | Contact me

Java Collections

Introduction

A collection represents a group of objects, called elements, bound together in different, useful ways. Elements can be added to, removed from or iterated over in a collection. Collections must provide ways to query for state and existence. Here is the set of responsibilities:

Correspondingly, any collection will:

More practically...

Here's an example of using Collections with a list or array. This take input as a String or String[], turns it into a List< String >, optionally sorts it and optionally creates a String[] or String for output purposes.

(There's a great deal more to collections than what's here. I'll come back and expand it some another time.)

Summary

The important bits of what's going on below here are:

  1. Create a List from an array (with Java 5 generics)...
    List< String > list = new ArrayList< String >(); for( int i = 0; i < this.array.length; i++ ) list.add( this.array[ i ] );
  2. Using Collections to sort the List...
    Collections.sort( list );
  3. How to create String[] from List...
    int arrayLength = list.size(); String[] stringArray = new String[ arrayLength ]; for( int i = 0; i < arrayLength; i++ ) stringArray[ i ] = list.get( i );
  4. Using Java 5's StringBuilder to create a string efficiently...
    StringBuilder sb = new StringBuilder(); for( int i = 0; i < list.size(); i++ ) sb.append( list.get( i ) + " " ); String string = sb.toString();

The examples...

package com.etretatlogiciels.examples.collections;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * This class demonstrates some possible somersaults in the life-cycle of a
 * String becoming String array becoming sorted Collection.
 *
 * To stifle perniciousness, the object must start off with a String which
 * cannot be changed thereafter: one initial value per one instantiation of
 * this class.
 */
public class StringArrayToCollection
{
  private String[]       array = null;
  private List< String > list  = null;

  /**
   * Easier start by supplying a simple, space-delimited string such as
   * a sentence.
   *
   * @param toParse - the string to be parsed into an array of String.
   */
  public StringArrayToCollection( String toParse )
  {
    if( array == null )
      array = toParse.split( " " );
    makeList();
  }

  /**
   * The basic characteristic of the initial state of an object of this
   * class is the String array.
   *
   * @param array - String[] of any number of elements.
   */
  public StringArrayToCollection( String[] array )
  {
    if( array == null )
      this.array = array;
    makeList();
  }

  private void makeList()
  {
    if( this.array == null )
      return;

    List< String > list = new ArrayList< String >();

    for( int i = 0; i < this.array.length; i++ )
      list.add( this.array[ i ] );

    this.list = list;
  }

  /**
   * Sort the string array. This is destructive in the sense that the
   * original order is lost. Note that words beginning with an upper-case
   * letter will sort in front since we're not doing anything to neutralize
   * case, though we could easily do that by using the version of sort()
   * that accepts a Comparator.
   *
   * @return the sorted string array.
   */
  public List< String > sort()
  {
    Collections.sort( this.list );

    return this.list;
  }

  /**
   * Turn the string (list) into an array of String--seems to satisfy the
   * requirements.
   *
   * @return the string array.
   */
  public String[] toArray()
  {
    int      arrayLength = this.list.size();
    String[] stringArray = new String[ arrayLength ];

    for( int i = 0; i < arrayLength; i++ )
      stringArray[ i ] = this.list.get( i );

    return stringArray;
  }

  /**
   * Return the string (list) to a simple string, easier to display using
   * System.out.println(). If sorted, this will be little more than a
   * jumble of words.
   */
  public String toString()
  {
    StringBuilder sb = new StringBuilder();

    for( int i = 0; i < this.list.size(); i++ )
      sb.append( this.list.get( i ) + " " );

    String  string = sb.toString();

    if( string.endsWith( " " ) )
      string = string.substring( 0, string.length() - 1 );

    return string;
  }
}