2

I have to create an array of ArrayList which is storing an object of the generic type.

ArrayList<Entry>[] bucket;

when I initialize this to

bucket=(ArrayList<Entry>[])new Object[m];

I get java.lang.ClassCastException at runtime.

Entry class uses generics. Below is the code:

class Entry<K,V>{
    K key;
    V value;

    public Entry(K key,V value){
        this.key=key;
        this.value=value;
    }

After going through several posts on why Array of Objects cannot be cast to an ArrayList of a generic type, I have understood my problem. But I am not able to get my head around this to solve my particular case.

Some of the solutions involved:

Changing it FROM "an array of ArrayList" TO "an ArrayList of ArrayList",
but I can not do that in my case.

Full code:

import java.util.ArrayList;
class MyHashMap<K,V>  {

    int m;
    int loadFactor;
    int n;

    ArrayList<Entry>[] bucket;

    public MyHashMap(){
        this(10,2);
    }
    public MyHashMap(int m){
        this(m,2);
    }
    public MyHashMap(int m,int loadFactor){
        this.m=m;
        this.loadFactor=loadFactor;
        n=0;

        bucket=(ArrayList<Entry>[])new Object[m];

    }

    class Entry{
        K key;
        V value;

        public Entry(K key,V value){
            this.key=key;
            this.value=value;
        }

        public int hashCode(){
            return 0;
        }
    }


    public void put(K key, V value){    
        Entry entry=new Entry(key,value);
        int hash=entry.hashCode();
        int index=hash%m;
        bucket[index].add(entry);
    }

    public static void main(String[] args) {
        MyHashMap hashMap=new MyHashMap();

        hashMap.put("faisal",2);
    }
}
6
  • 1
    Why did you create a class called Entry which looks awfully similar to Java's Map.Entry? Commented Dec 26, 2016 at 7:26
  • 1
    why do you think you can cast a Object array into an array of ArrayList<Entry> ? Commented Dec 26, 2016 at 7:27
  • Please show the code in context where you plan to use this ArrayList. Commented Dec 26, 2016 at 7:27
  • Polymorphic type declaration doesn't work with collections.The type should be exact type.it cant be parent or subtype.but this is diff from with arrays.Entry[] bucket=new Object[m]; Commented Dec 26, 2016 at 7:28
  • @Tim I am actually implementing HashMap here,and I am using chaining. Commented Dec 26, 2016 at 7:28

2 Answers 2

2

You can't create arrays of generic type. Use this instead:

ArrayList<Entry>[] bucket = new ArrayList[m];

It shows unchecked warning though, which you can suppress using @SuppressWarnings("unchecked") like so:

@SuppressWarnings("unchecked")
ArrayList<Entry>[] bucket = new ArrayList[m];
Sign up to request clarification or add additional context in comments.

2 Comments

The compiler does not complain nor there is any problem at runtime. I am now wondering if it is okay to do that.! I mean is this the only option left.
It's fine to do it if you know you are going to store only the ArrayList<Entry> objects in it. Better not to expose it outside your API class.
0

Object can be cast to ArrayList only if the actual object it holds is an arrayList. i.e let's look at the below code,

Object obj = new ArrayList<String>();   // object variable holding array list type.
ArrayList<String> arr = (ArrayList<String>) obj;

1 Comment

Question is about array of array list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.