View RSS Feed


Java Collection

  1. Java Collection Subtypes

    by , 02-18-2012 at 05:10 PM
    Collection interface subtypes are given below. They all makes the collection interface extended.

    1. List
    2. Queue
    3. SortedSet
    4. Set
    5. NavigableSet
    6. Deque

    One of its subtypes shall be used when Collection interface are needed to be used. At Collection interface, implementation is not supported by Java. Just a set of methods are present in it. Specific Collection types are ignored by it.

    To operate at a Collection, ...
  2. Checking if a Collection Contains a Certain Element

    by , 02-18-2012 at 05:08 PM
    In collection interface, 2 methods have been implemented to check if certain elements are present in it. Two methods are as following:

    1. contains()
    2. containsAll()

    This code explains these 2 methods:
    Java Code: This is the code to explain the contains and containsAll methods
    Collection collection   = new HashSet();
    boolean containsElement = collection.contains("an element");
    Collection elements     = new HashSet();
    boolean containsAll     = collection.containsAll(elements);
  3. Iterating a Collection

    by , 02-18-2012 at 05:07 PM
    Collection’s all elements are iteration is done by getting an iterator from collection. Use the iterator through collection.

    It is explained by this given code:
    Java Code: This is the code to explain the collection iteration
    Collection myCollection = new HashSet();
    //... add elements to the collection
    Iterator iterator = myCollection.iterator();
        Object object =;
        //do something to object;    
    //You can also use the
  4. Using Collections.sort and Comparator in Java

    by , 02-18-2012 at 05:05 PM
    Sorting a collection in Java is easy, just use Collections.sort(Collection) to sort your values. For example:
    Collection sorting is easier in Java. To sort the values, use Collection.sort (Collection). E.g

    Java Code: This is the code to explain collection sort and comparator methods
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    public class SimpleSort {
        public static void main(String[] args) {
            List list = new ArrayList();
  5. The Collection Classes

    by , 02-18-2012 at 05:03 PM
    Standard collection classes are provided by the Java which does the implementation of the Collection interfaces. Few classes give full implementations that are being used as the abstract class, skeletal implementations are provided which are the starting points to make the concrete collections.

    Here is the list given of standard collection classes.

    • AbstractCollection: Collection interfaces are implemented by it.
    • AbstractList: Extension of AbstractCollection is
  6. Java Interfaces

    by , 02-18-2012 at 05:02 PM
    Core collection interfaces are shown in this figure, which are basically encapsulated by various collections. Such interfaces are used for manipulation of maintenance of the interfaces, independent of details. Core collection interfaces are taken as the basis of Java collection Framework.

    Core collection interfaces are detailed by this list:

    • Collection —a collection presents objects group which are known as its elements.
    • Set — a collection in which duplicate elements
  7. Benefits of the Java Collections Framework

    by , 02-18-2012 at 05:01 PM
    Java Collections Framework offers these benefits:

    • Reducing the efforts of Programming: Useful data structures and algorithms are present in Collection Framework. Built in data structures time and efforts of a programmer gets reduced. It also allows you interoperability to the other APIs. There is no need of conversion codes or writing of the adapter objects.
    • Increase speed and quality of program: Higher quality algorithms, high performance and data structures are provided by
  8. Collections Framework

    by , 02-18-2012 at 04:59 PM
    Unified architecture to manipulate and present the collections is known as collections framework. Following is present in all collections framework:

    • Interfaces: Interfaces present the collections and are considered to be an abstract data type. Connections are allowed by interfaces to be manipulated independent of their presentation details. A hierarchy is formed by interface in the object oriented language.
    • Implementations: They are collection interface’s concrete implementation.
  9. Use Lists instead Arrays in Generics

    by , 11-30-2011 at 12:20 AM
    Generics were added to java in release 1.5. It allows you to avoid needing to cast objects as they are read from a collection. Instead you tell the compiler what types of objects that you want to hold in the collection. Often it is difficult to know which generic to use when you have two or more who seem to be able to complete the same function. Consider the choice between whether to use a list or an array. Now a key thing to remember is that arrays are covariant and know and enforce their element ...
  10. Wildcards with Generics

    by , 11-30-2011 at 12:16 AM
    Unlike with arrays, it is important to remember with generics that is not possible to upcast an object from its superclass to its subclass. So for example, this will create a compile time error.

    Java Code:
    import java.util.*;
    public class NonCovariantGenerics { 
    // Compile Error: incompatible types: 
    List<Shape> slist = new ArrayList<Rectangle>();
    In this listing List of Rectangle is not type-equivalent to a List of Shape, even if an Rectangle ...
  11. Supertype Wildcards with Generics

    by , 11-30-2011 at 12:12 AM
    In my last tip, I showed how you can use a wildcard with generics. It is also possible to use a supertype wildcards. In this case the wildcard is bounded by any base class of a particular class, by specifying <? super MyClass> or even using a type parameter: <? super T> (although you cannot give a generic parameter a supertype bound; that is, you cannot say <T super MyClass>). This allows you to safely pass a typed object into a generic type. Thus, with supertype wildcards you ...
  12. Unbounded Wildcards with Generics

    by , 11-30-2011 at 12:09 AM
    When you as a programmer decide to use an unbounded wildcard <?>, it appears to mean anything. and so using an unbounded wildcard seems equivalent to using a raw type. There are time that the compiler is indifferent as to using raw type or <?>. If you use <?> it can be considered as a decoration. When you are using the raw type, the generic parameter can hold any type. In this example I show an important use of unbounded wildcards. If you are dealing with multiple generic parameters, ...
  13. Dynamic Type Safety in Pre Java 5 Code Collections

    by , 11-29-2011 at 11:40 PM
    Sometimes is might be necessary to pass generic containers to pre-Java5 code. In those cases, it is possible that the old-style code can corrupt your containers. In order to address this issue, there are a set of utilities in the java.util.Collections package to address the issues around type-checking. There are a number of static methods designed for this. They all take the container that you are checking as the first argument and the type you want to enforce as the second argument. They are shown ...
  14. Comparing Objects and HashCode

    by , 11-29-2011 at 11:30 PM
    Another thing that many programmers forget when they make objects that they need to compare in their applications is that it is not enough to assume that if your object is a subclass of Object, that the hashCode method that you can use it in a HashMap, Hashtable or HashSet. In fact, the Java specification states that you must always override the hashCode when ever you override the equals method. Otherwise, you have violated one of the key tenants of Java in not ensuring that equal objects have equal ...
  15. Generics - Exceptions

    by , 11-29-2011 at 04:35 AM
    Due to erasure, exception use is limited with generics. As exceptions are know at both compile and at run time, it renders redundant the use of catch clauses. This also means that a generic class cannot inherit directly or indirectly from Throwable.

    One can though use type parameters in the throws clause of a method declaration. This will allow you to write generic code that varies with the type of a checked exception:

    Java Code:
    import java.util.List;
    public interface