Java Tips on Array / List / Set / For Each Loop

This is a basic Java Tips on when and how to use Array, List, Set, and for each loop.

When you have a set of data you want to store, you can use Array / List / Set.

Array is a fixed size structure, meant, when you initialise Array to be size of 10, then you cannot alter the size already.

To declare an Array, you type:


String[] someArray = new String[3];
someArray[0] = "one";
someArray[1] = "two";
someArray[2] = "three";

List is a variable size structure, meant, you can add and remove items without declare its size yet.

To declare a List, you type:


List someList = new ArrayList();
someList.add("one");
someList.add("two");
someList.add("three");

You may want to convert Array into List, you do


String[] someArray = new String[3];
List someList = Arrays.asList(someArray);

You may want to convert List back to an Array, you do


List someList = new ArrayList();
String[] someArray = someList.toArray();

Set is an unique, non repeated version of List. For example, using List, you can store [1,2,3,2,1], as 2 repeated 2 times. But, Set, you always cannot repeat the items.

The most common implementation is HashSet, you do


Set someSet = new HashSet();
someSet.add("one");
someSet.add("two");
someSet.add("three");
someSet.add("one");

Note, in the output, you only can find [“one”, “two”, “three”]

And you may not see “one”,”two”,”three” in insertion order. Actually, Java will hash each item, by converting the String into a hash key, then store inside the HashSet, so you may see “two”, “one”, “three” but not “one”, “two”, “three”.

If you want the Set in insertion order, you should use LinkedHashSet.


Set someSet = new LinkedHashSet();
someSet.add("one");
someSet.add("two");
someSet.add("three");
someSet.add("one");

Then you will see “one”, “two”, “three”.

If you want the Set in ordered, you can use TreeSet. Everytime you insert an item, it will auto sort.


Set someSet = new TreeSet();
someSet.add("one");
someSet.add("two");
someSet.add("three");
someSet.add("one");

The output will be “one”, “three”, “two”. Or more better example,


Set someSet = new TreeSet();
someSet.add(3);
someSet.add(2);
someSet.add(1);
someSet.add(3);

The output will be 1,2,3.

To iterate the array, list, set, you can use for each loop construct.

OK, normally, you may already know, something like this


String[] someArray = new String[3];
for(int i = 0; i < someArray.length; i++) { someArray[i] }

You can do this:


String[] someArray = new String[3];
for(String item : someArray) {
item
}

This is same applies to List and Set.

For example:


List someList = new ArrayList();
for(String item : someList) {
item
}

Sometimes, good to use Array, sometimes, good to use List / Set.

If using List and Set, you can do something like add(), remove(), contains().

For example, if you want to filter something, you can use contains(), you can check if the List “contains” the item you want to search.


someList.contains("some thing");

Finally, you can bookmark my blog to follow up more Java Tips posted in future.

Author: fyhao

Jebsen & Jessen Comms Singapore INTI University College Bsc (Hon) of Computer Science, Coventry University

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.