Solved

JAVA generics

Posted on 2014-02-22
8
474 Views
Last Modified: 2014-02-23
I was wondering if someone could explain this syntax.

I'm working on an implementation of a HashTable with chaining. My rehash function looks like this:

    private void rehash() {
        DoublyLinkedList<Entry<K, V>>[] oldTable = table;

        table = new DoublyLinkedList[2 * oldTable.length];
        numKeys = 0;

        for( DoublyLinkedList<Entry<K, V>> bucket : oldTable ){
            if( bucket != null ){
                for(Object entry : bucket){
                    Entry<K,V> e = (Entry<K,V>) entry;
                    put((K) e.getKey(), (V) e.getValue() );
                }
            }
        }
    }

Open in new window


This was the only way I was able to get it to compile.

What I don't understand is why in the inner loop I have to say entry is an Object, and then cast it to Entry<K,V> e inside the loop. It seems weird to do it this way. I *think* I have tried every other permutation, such as: for(Entry entry : bucket)... etc... to no avail.

Thanks,
Koza
0
Comment
Question by:Kyle Hamilton
  • 5
  • 3
8 Comments
 
LVL 26

Expert Comment

by:dpearson
ID: 39879882
I think you should be using Map.Entry<K,V> instead of just Entry<K,V>.

Then you can use:

    for(Map.Entry<K,V> entry : bucket)  { }

This compiles just fine for me:

import java.util.LinkedList;
import java.util.Map;

public class MyTest<K,V> {

	private LinkedList<Map.Entry<K, V>>[] table ;

	private void rehash2() {
		LinkedList<Map.Entry<K, V>>[] oldTable = table;

		table = new LinkedList[2 * oldTable.length];
		int numKeys = 0;

		for( LinkedList<Map.Entry<K, V>> bucket : oldTable ){
			if( bucket != null ){
				for(Map.Entry<K,V> entry : bucket){
					put(entry.getKey(),entry.getValue() );
				}
			}
		}
	}
	
	private void put(K key, V value) { }

}

Open in new window


Doug
0
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39879905
hi Doug,

the thing is, i'm trying to use my own *schmucky* data structures :), hense, DoublyLinkedList.

do you think the problem could be in my DoublyLinkedList?

thanks,
Koza
0
 
LVL 26

Accepted Solution

by:
dpearson earned 500 total points
ID: 39879957
I guess it's possible, but you should certainly be fine to replace LinkedList with your own class.

E.g. This also compiles:

public class MyTest<K,V> {
	public static class DoublyLinkedList<E> extends LinkedList<E> {
	}
	
	private void rehash() {
		DoublyLinkedList<Map.Entry<K, V>>[] table = null ;
		DoublyLinkedList<Map.Entry<K, V>>[] oldTable = table;

		table = new DoublyLinkedList[2 * oldTable.length];
		int numKeys = 0;

		for( DoublyLinkedList<Map.Entry<K, V>> bucket : oldTable ){
			if( bucket != null ){
				for(Object entry : bucket){
					Map.Entry<K,V> e = (Map.Entry<K,V>) entry;
					//put((K) e.getKey(), (V) e.getValue() );
				}
			}
		}
	}

	private DoublyLinkedList<Map.Entry<K, V>>[] table ;

	private void rehash2() {
		DoublyLinkedList<Map.Entry<K, V>>[] oldTable = table;

		table = new DoublyLinkedList[2 * oldTable.length];
		int numKeys = 0;

		for( DoublyLinkedList<Map.Entry<K, V>> bucket : oldTable ){
			if( bucket != null ){
				for(Map.Entry<K,V> entry : bucket){
					put(entry.getKey(),entry.getValue() );
				}
			}
		}
	}

	private void put(K key, V value) { }

}

Open in new window


And of course you could write your own DoublyLinkedList<E> class from scratch instead of just subclassing an existing list.

The key for supporting the "for x : y" syntax is whether you implement the iterable interface: http://docs.oracle.com/javase/7/docs/api/java/lang/Iterable.html

Doug
0
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39880012
yes. I implemented iterable (see snippet). That is the only piece from java.util I'm using.

@Override
	public Iterator<E> iterator(){
		return new Iter();
	};
	
	private class Iter extends DoublyLinkedList<E> implements Iterator<E> {
// more stuff

Open in new window


I would like to be able to do this without using java.util.Map; Is it possible?

It seems my DoubleLinkedList works for everything else I've done with it, so maybe I've screwed up my iterator implementation somehow.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 25

Author Closing Comment

by:Kyle Hamilton
ID: 39880047
ok, so I ran your code as is, and it compiles, of course. Then I used my implementation of DoublyLinkedList, and I get the error I'm getting in my own code. This would suggest there is something wrong with my linked list implementation. back to the drawing board.


thanks again.
Koza.
0
 
LVL 26

Expert Comment

by:dpearson
ID: 39881248
If you want to post the code for DoublyLinkedList (perhaps start another question?) I'll be happy to have a look and see if we can sort out where the problem is.

I'd focus on the type returned by the iterator() method to start with...

Doug
0
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39881325
Thanks Doug,

I'll post another question. I had several implementations of Iterator. all of them worked, and all of them with the same side effect. I thought the fault lay with my iterator, but I don't think so anymore. It's gotta be something else.
0
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39881346
OMG, I fixed it. I'm such a dope. I forgot to add the Type to Iterable, like this:

public class DoublyLinkedList<E> implements DoublyLinkedListInt<E>, Iterable<E>{}

 I'm gonna leave that other question open to see what else pops up :)

You're the best, Thank you!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 21
maven example error 3 52
mockito example issue 8 62
java jdbc batch example 8 21
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now