1

I am trying to implement my own version of HashMap. FDor storing the items I would like to use an ArrayList of LinkedLists and for that I am taking "program to interface" approach. So the items type is List <List<MyEntry<k,v>>>

but

I wanna instantiate it by ArrayList< LinkedList< MyEntry< k,v>>>()

what is wrong with my code that I get compilation error

Type mismatch: cannot convert from ArrayList< LinkedList< MyHashMap1< k,v>.MyEntry<k,v>>> to List<List<MyHashMap1<k,v>.MyEntry<k,v>>>

My question is

If I change the type to List< LinkedList< MyEntry< k, v>>> It works but List< LinkedList< MyEntry< k, v>>> is not %100 interface, LInkedList is not an interface

package maps;
import java.util.ArrayList;
import java.util.List;
import java.util.LinkedList;
public class MyHashMap1<k,v> {
    //Type mismatch: cannot convert from ArrayList<LinkedList<MyHashMap1<k,v>.MyEntry<k,v>>> to List<List<MyHashMap1<k,v>.MyEntry<k,v>>>
    List <List<MyEntry<k,v>>> items = new ArrayList<LinkedList<MyEntry<k,v>>>(); 
    public class MyEntry<a,b>{
        a key;
        b value;

        public MyEntry(a key,b value){
            this.key = key;
            this.value = value;
        }
    }
}
donfuxx
  • 11,277
  • 6
  • 44
  • 76
C graphics
  • 7,308
  • 19
  • 83
  • 134

1 Answers1

1

Can you try this one

List<LinkedList<MyEntry<k,v>>> items = new ArrayList<LinkedList<MyEntry<k,v>>>();

or try this one if ArrayList can accept any type of List

List<List<MyEntry<k,v>>> items = new ArrayList<List<MyEntry<k,v>>>(); 

Always code to interface rather than concrete class.

Braj
  • 46,415
  • 5
  • 60
  • 76