Core Java Technology Section Index | Page 7
How can I insert an element into a List?
In order to insert an element into a List add(int index, E element) must be used since the List interface does not provide an insert method. If the index is out of rage ie. index < 0 || index &...more
What is a ThreadGroup?
A ThreadGroup is used to represent a group of Threads. A tree structure can
be formed from ThreadsGroups containing other ThreadGroups. Threads can only access the ThreadGroup to which they belon...more
What is a ThreadGroup?
A ThreadGroup is used to represent a group of Threads. A tree structure can be formed from ThreadsGroups containing other ThreadGroups. Threads can only access the ThreadGroup to which they belon...more
How can a Thread preempt another Thread?
Any Threads running within the JVM can be preempted by creating and starting a
Thread which is of a higher priority. A Threads priority can be set using the following method.
public void setPr...more
What is the difference between a Stack and a Queue?
First things first. Where can Stacks and Queue's be found?
Java provides a Stack class, which can be found within the java.util namespace. Within the same namespace you will find a Queue interf...more
How can I create an immutable List consisting of n Copies of an Object?
nCopies(int n, T o) can be used to create an immutable List which contains a specific number of copies of an Object. Care must be taken when trying to add elements to or extract elements from the...more
How can I customize the output of a stack trace?
The Throwable class has a getStackTrace() method that returns an array of StackTraceElement objects. With an element, you can then print line numbers, file names, and method names in any style you...more
How can I replace what is sent in a stack trace to "hide" internal details of a class library?
In addition to the typical obfuscation options, you can use the setStackTrace() method of Throwable to replace the StackTraceElement array. If you really want to, you can "move" line numbers and r...more
How do I change the starting directory for running a command with ProcessBuilder?
Use the directory() method of ProcessBuilder to change the starting / working directory. Once setting the directory, start() the sub-process.
ProcessBuilder processBuilder = new ProcessBuilder...more
How do I declare a method to accept a variable argument list?
Methods like printf allow you to pass multiple arguments to a method without having to explicitly declare versions for each "count" of arguments. For the final parameter type, use three periods, a...more
How do I enable assertions in a package and all its subpackages?
Add ... to the end of the top level package name, along with the required -ea / -enableassertions command line switch, as in java -ea:com.example.foo... FooClass.
How do I enable assertions in only a single package?
Use the -enableassertions, or -ea, command line switch, as in
java -ea:com.example.other MyClass.
What is a Thread identifer and how can I obtain it?
A Threads identifer is a positive unique long value which is generated when a Thread is created. Using the following method a Threads identifer can be obtained.
public long getId()
Here is a ...more
What's the difference between Runtime.exec() and ProcessBuilder?
JDK 5.0 added a new class, ProcessBuilder to do the equivalent of the exec() method of Runtime. In addition to running commands in forked off subprocesses, you can adjust environment variables and...more
When using ProcessBuilder, how do I set an environment variable in the subprocess?
Get the set of environment variables by getting a Map with the environment() method. Then, add variables with the put(key, value) method of Map.
ProcessBuilder processBuilder = new ProcessBuil...more