JavaWorld's in-house Java Tutor takes questions on core Java programming topics in this blog dedicated to Java beginners everywhere.
A: The easiest way to create a list is to use the
<T> List<T> asList(T... a) generic class method, which returns a fixed-size list backed by the specified array. For example, the following code fragment creates a list of inner planet names:
List<String> innerPlanetNames = Arrays.asList("Mercury", "Venus", "Earth", "Mars");
Collection interface declares the following pair of methods for copying a collection's elements to an array:
Object toArray(): Return a new array containing all of this collection's elements.
<T> T toArray(T a): Return the specified array or a new array (when
ais too small to hold all of the elements) containing all of this collection's elements. Because this method is generic, you don't need to use a cast operator, which you have to use with the former method unless the target array has
The following code fragment uses the second method to copy the previously created
innerPlanetNames list to a new array:
String _innerPlanetNames = innerPlanetNames.toArray(new String);
A: Assuming that
c is of type
java.util.Collection, execute the following expression to remove a collection's null elements:
Collections.singleton(null) expression returns an immutable set consisting of a single
null reference. This set is passed as an argument to
boolean removeAll(Collection<?> c) method, which removes all of this collection's elements that are also contained in the specified collection (
c). In this case, all null elements are removed. This method returns true when this collection is changed as a result of the call; otherwise, false returns.
removeAll() is an optional operation, this method throws
java.lang.UnsupportedOperationException when it's not supported by a class that implements the
Collection interface. For example, because
asList() returns a list based on an array, and because arrays don't support the addition or removal of elements, calling
removeAll() on such a list results in
UnsupportedOperationException being thrown.
A: To remove a list's duplicate elements, pass it to a
java.util.HashSet instance (when you don't care about the resulting order) or a
java.util.LinkedHashSet instance (when order matters). Consider the following code fragment:
List<String> names = Arrays.asList("John", "Jane", "Tom", "Jane", "Tom", "Brian"); System.out.println(names); // Output: [John, Jane, Tom, Jane, Tom, Brian] System.out.println(new HashSet<>(names)); // Output: [Brian, Jane, Tom, John] System.out.println(new LinkedHashSet<>(names)); // Output: [John, Jane, Tom, Brian]
A: To iterate over a map's entries, employ the following idiom:
for (Map.Entry<KeyType, ValueType> e: m.entrySet()) System.out.println(e.getKey()+": "+e.getValue());
This idiom invokes
Set<Map.Entry<K,V>> entrySet() method to return a
java.util.Set view of the mappings contained in this map. Each mapping is represented by an object whose class implements
Entry<K,V> interface, whose
K getKey() method returns the key and
V getValue() method returns the value.
The follow code fragment offers a more concrete example:
Map<String, String> capitals = new TreeMap<>(); capitals.put("United States", "Washington"); capitals.put("Russia", "Moscow"); capitals.put("England", "London"); capitals.put("France", "Paris"); for (Map.Entry
entry: capitals.entrySet()) System.out.println(entry.getKey()+": "+entry.getValue());
How much do you know about the Java language and platform? I've developed a 150-question quiz to help you assess your Java knowledge level. Below is an example question:
Which one of the following is not a Javadoc tag?
This quiz is organized into interleaved true/false and multiple choice categories, and focuses on Java Platform, Standard Edition (SE) 1 through Java SE 7.
The quiz is available for Amazon Kindle devices only. Check out Amazon's The BIG Java Quiz to obtain a copy.