This is a collection of cool things to do with lists as they are encountered and I get excited enough to write them down. (I'm getting long enough in the Java tooth that I don't always recognize when I should note something for general interest.)
Here's a neat trick that works real well. It's just complicated enough that it might not occur to you. This is Java 5+ stuff, of course.
import java.util.List; import java.util.ArrayList; import java.util.Collections; public class Songs { /* method 1: */ public static final List< String > songs = new ArrayList<>();; static { songs.add( "Ina Gaddah Da Vida" ); songs.add( "I Saw Her Diary" ); songs.add( "The Sounds of Silence" ); songs.add( "Dead Skunk in the Middle of the Road" ); songs.add( "Cripplecreek Ferry" ); } /* method 2: */ public static final List< String > songs = Arrays.asList( "Ina Gaddah Da Vida", "I Saw Her Diary", "The Sounds of Silence", "Dead Skunk in the Middle of the Road", "Cripplecreek Ferry" ); }
import java.util.List; import java.util.ArrayList; import java.util.Collections; public class Songs { /* initialize a new list of 1 element only */ public static final List< String > songs = Collections.singletonList( "Cripplecreek Ferry" ); public void add( final String another ) { songs.add( another ); } }
Traversing a list is child's play. Here are two methods.
import java.util.List; import java.util.ArrayList; import java.util.Iterator; public static final List< String > songs = Arrays.asList( "Ina Gaddah Da Vida", "I Saw Her Diary", "The Sounds of Silence", "Dead Skunk in the Middle of the Road", "Cripplecreek Ferry" ); /* method 1: since Java 5 */ for( String song : songs ) { System.out.println( song ); } /* method 2: use an Iterator */ Iterator< String > it = songs.iterator(); while( it.hasNext() ) { String song = it.next() System.out.println( song ); }
It's less obvious how to traverse two lists at the same time because you need to walk them together and interact between them.
import org.junit.Test; import static org.junit.Assert.assertEquals; import java.util.List; import java.util.ArrayList; import java.util.Iterator; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Logger; public class LoggingTest { private static final Logger log = LogManager.getLogger( LoggingTest.class ); @Test public void testGetLevelFromString() { LoggingUtilities.setLevel( log, Level.OFF ); List< Level > levels = setUpExpectedLevels(); List< String > tries = setUpLevelTries(); // here's where it happens... Iterator< Level > level = levels.iterator(); Iterator< String > shot = tries.iterator(); while( level.hasNext() && shot.hasNext() ) { Level expected = level.next(); Level actual = LoggingUtilities.levelFromString( shot.next() ); System.out.println( " Actual: " + actual ); System.out.println( "Expected: " + expected ); assertEquals( actual, expected ); } } private List< Level > setUpExpectedLevels() { List< Level > levels = new ArrayList<>(); levels.add( Level.TRACE ); levels.add( Level.TRACE ); levels.add( Level.DEBUG ); levels.add( Level.DEBUG ); levels.add( Level.INFO ); levels.add( Level.INFO ); levels.add( Level.WARN ); levels.add( Level.WARN ); levels.add( Level.ERROR ); levels.add( Level.ERROR ); levels.add( Level.FATAL ); levels.add( Level.FATAL ); levels.add( Level.OFF ); levels.add( Level.OFF ); levels.add( Level.OFF ); return levels; } private List< String > setUpLevelTries() { List< String > tries = new ArrayList<>(); tries.add( "6" ); tries.add( "TRACE" ); tries.add( "5" ); tries.add( "dEbUg" ); tries.add( "4" ); tries.add( "InFo" ); tries.add( "3" ); tries.add( "warn" ); tries.add( "2" ); tries.add( "error" ); tries.add( "1" ); tries.add( "fatal" ); tries.add( "0" ); tries.add( "off" ); tries.add( "Uncle Skeezix" ); return tries; } }