Developer

System calls and method collection in Java

This week's Java Q and A answers questions from members concerning how to make a system call from within a program and how to perform method collection.


This week in Java Q&A, we’ll tackle questions from two Builder.com members. Ever want to run a system command from within your Java application? In our first solution, we will show you how easy that can be. Our second question and solution explain the use of collections and summarize the Java Collection framework.

Using RunTime.exec
Question: Could you give an example of running an operating system command or a UNIX shell script within a Java program?
Builder.com: Within Java, you can invoke executables (or whatever your OS is willing to execute) via Runtime.exec(). When using Runtime.exec(),it may be preferable to use the Runtime.exec(String[]) with the parameter String array containing the command. This is convenient since most of the OS-specific argument escaping will be done for you. Here is an example of running an operating system command within a Java program.

 

Object collection
Question: I'm a new student of Java, and I'm trying to learn how to create a method in a class—for example, addCDs(). How would I create the instructions to make this method add CDs to a collection? I need any kind of example to learn how to do this. I have tried for days and haven't been able to figure this out.
Builder.com
: Included here is an example to illustrate the ability to add CompactDisc objects (CDs) to a CompacDiscHolder (a holder for CDs). In particular, consider the code snippet:
       private List CDs = new LinkedList();
public void addCDs(CompactDisc cdToAdd){
             CDs.add(cdToAdd);
}

The add operation appends the given CD object to the end of the list. The add method is defined such that it makes sense for collections that allow duplicates as well as those that don't.

About the Java Collection framework
Assuming Java 2 usage, the java.util package has several “container” classes/interfaces available, each suited for specific behavior requirements. The following is a quick summary of the classes available:

Collection—This is a basic interface that defines the operations that all the classes that maintain collections of objects typically implement.

Set—This extends the Collection interface for sets that maintain unique elements in no particular order; implemented by HashSet.

SortedSet—This augments the Set interface for sets that maintain their elements in a sorted order by using the comparable interface for ordering; implemented by TreeSet.

List—This extends the Collection interface for lists that maintain their elements in a sequence (i.e., elements that are in order and that can contain duplicates). List implementations include ArrayList, Vector, and LinkedList. Of the three, ArrayList and LinkedList are considered fast in terms of insertions and deletions but are not Thread safe, as Vectors are.

Map—This is a basic interface defining the operations that all classes that represent mappings of keys to values typically implement. A map is not a Collection. A map does not allow duplicate keys; each key maps to only one value. The objects contained within the map can be “viewed” three ways—as a key set, a value collection, and a key,value set. Map implementations include HashTable and HashMap.

SortedMap—This extends the Map interface for maps that maintain their mappings in key order by using the comparable interface for ordering, implemented by TreeMap.

Having Java problems?
Do you have any Java questions or suggestions? What would you like to see discussed here? Send us an e-mail with your thoughts or post a comment below.

 

Editor's Picks

Free Newsletters, In your Inbox