Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a class that implements java.util.collections

I am trying to create a class say MyStack that would implement a java.util.collections class. MyStack will override some methods of the collections class like add(similar to push), remove(similar to pop) etc.. I intend to model the class on the same lines as a Set or other interfaces of the collection class except that MyStack would not be an interface or abstract class and that we can create objects of type MyStack.

I have problems with the syntax as I am not sure if I am proceeding in the right direction.All I have so far is something like this - NOTE - None of the methods have been defined so far - I am trying to get teh skeleton right before proceeding to define the methods.

import java.util.*;


public class MyStak implements java.util.Collection<E>{

    public boolean add(E o){

               return false;        
        }

       public boolean addAll(Collection c){
        return false; 

        }

       public void clear() {

       }

        public boolean contains(Object o){
          return false;

        }

        public boolean containsAll(Collection o){
          return false;

        }

        public boolean equals(Object c){
          return false; 
        }

        public int hashcode(){
          return 0; 
        }

        public boolean isEmpty(){
          return false; 
        }

        public Iterator iterator(){
          return null;

        }

        public boolean remove(Object o){
          return false; 
        }

        public boolean removeAll(Collection o){
          return false; 
        }

        public boolean retainAll(Collection o){
          return false; 
        }

        public int size(){
          return 1; 
        }

         public Object[] toArray(){
           return null;

         }

         public Object[] toArray(Object[] a){
           return null; 
         }

    }

I have a couple of compile time errors like -

    +public class MyStak implements java.util.Collection<E>{
Multiple markers at this line
    - The type MyStak must implement the inherited abstract method 
     Collection<E>.add(E)
    - E cannot be resolved to a type

    +public boolean add(E o){
Multiple markers at this line
    - E cannot be resolved to a type
    - implements 
     java.util.Collection<E>.add

Any code modifications, examples , corrections to my code , links to tutorials etc will we highly appreciated.

like image 972
Eternal Learner Avatar asked Nov 29 '10 05:11

Eternal Learner


People also ask

Is Java Util collections a class?

Collections class is one of the utility classes in Java Collections Framework. The java. util package contains the Collections class. Collections class is basically used with the static methods that operate on the collections or return the collection.

What implements a collection in Java?

The Collection interface is implemented by AbstractCollection, AbstractList, AbstractQueue, AbstractSequentialList, AbstractSet, ArrayBlockingQueue, ArrayDeque, ArrayList, AttributeList, BeanContextServicesSupport, BeanContextSupport, ConcurrentHashMap.

What is Java Util collections in Java?

The java.util.Collections class consists exclusively of static methods that operate on or return collections.Following are the important points about Collections − It contains polymorphic algorithms that operate on collections, "wrappers", which return a new collection backed by a specified collection.


2 Answers

Make sure to throw on a <E> specification on your class as well:

public class MyStak<E> implements java.util.Collection<E>
                   ^^^

If you want to make life easier on yourself try sub-classing AbstractCollection instead of implementing Collection directly. It provides reasonable default implementations for most of the methods to minimize the amount of code you need to write.

java.util

Class AbstractCollection<E>

This class provides a skeletal implementation of the Collection interface, to minimize the effort required to implement this interface.

To implement an unmodifiable collection, the programmer needs only to extend this class and provide implementations for the iterator and size methods. (The iterator returned by the iterator method must implement hasNext and next.)

To implement a modifiable collection, the programmer must additionally override this class's add method (which otherwise throws an UnsupportedOperationException), and the iterator returned by the iterator method must additionally implement its remove method.

The programmer should generally provide a void (no argument) and Collection constructor, as per the recommendation in the Collection interface specification.

like image 194
John Kugelman Avatar answered Nov 15 '22 05:11

John Kugelman


You were VERY close!

You just need to define E in your subclass as well:

public class MyStak<E> implements java.util.Collection<E>

The idea is that you could have a subclass with, say, <E, F, G>, and you implement two different interfaces, one using E, one using F. That, or MyStak could be specialized and use a specific class for Collection, instead of a generic E.

like image 39
EboMike Avatar answered Nov 15 '22 07:11

EboMike