Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different indications of generics types in Java #1

Open
vladkinoman opened this issue Aug 29, 2019 · 0 comments
Open

Different indications of generics types in Java #1

vladkinoman opened this issue Aug 29, 2019 · 0 comments
Labels

Comments

@vladkinoman
Copy link
Owner

Different indications of generics types in Java

Description

I wonder what are the advantages of using different types of generic type indications? You can implement class in two ways:

  1. You can use generic type parameters in the inner classes and in the corresponding instance variables.
  2. or you might not want it.

To reproduce

First version

This version uses generic type parameter in the implementation of the class Deque:

public class Deque<Item> implements Iterable<Item> {
    private Node<Item> first;
    private Node<Item> last;
    private int count;

 	private static class Node<Item> {
        private Node<Item> next;
        private Node<Item> prev;
        private Item item;
    }
	...
	public void addFirst(Item item) {
        ...
        Node<Item> newNode = new Node<>();
		...
	}
	...
	public Iterator<Item> iterator() {
		// I don't use parameter in this version
        return new DoubleLinkedListIterator();
    }
	
	// somehow you can leave DoubleLinkedListIterator without <Item> parameter
    private class DoubleLinkedListIterator implements Iterator<Item> {

        private Node<Item> current = first;
		// we don't need constructor this time
		...
        @Override
        public Item next() {
            if (!hasNext()) throw new NoSuchElementException();
            Item currItem = current.item;
            current = current.next;
            return currItem;
        }
    }
	...
}

Notice that we use static inner class Node. In that way we can save 8 bytes getting rid of inner class extra overhead. Also, somehow you can leave DoubleLinkedListIterator without parameter.

Second Version

This version doesn't use generic type parameters in the implementation of the class Deque:

public class Deque<Item> implements Iterable<Item> {
    private Node first;
    private Node last;
    private int count;

 	private class Node {
        private Node next;
        private Node prev;
        private Item item;
    }
	...
	public void addFirst(Item item) {
        ...
        Node newNode = new Node();
		...
	}
	...
	public Iterator<Item> iterator() {
        return new DoubleLinkedListIterator(first);
    }

    private class DoubleLinkedListIterator implements Iterator<Item> {

        private Node current;

        public DoubleLinkedListIterator(Node first) {
            current = first;
        }
		...
        @Override
        public Item next() {
            if (!hasNext()) throw new NoSuchElementException();
            Item currItem = current.item;
            current = current.next;
            return currItem;
        }
    }
	...
}

Possible answer

1.Here?
2.Maybe here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant