Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4400
  • Last Modified:

Override HashMap put method

I have a HashMap declared as so:
HashMap <String, MyTableClass> tableMap = new LinkedHashMap<String, MyTableClass>();

I want to override the put method, so I created its own class:

public class TableHashMap extends LinkedHashMap {

      @Override
      public MyTableClass put (Object key, Object value) {
            MyTableClass table = (MyTableClass ) super.put(key, value);
            return table;
      }

}

As a class this compiles, but then the declaration statement doesn't compile, saying that it cannot be parameterized.
If I change both of the "Object" types to String and MyTableClass, then it has a compile error that says that the method put must override a supertype method.
How do I do this?
0
allelopath
Asked:
allelopath
2 Solutions
 
for_yanCommented:
It is probably not a very good idea to extend HashMap and override put method ?
Why do you need it?
0
 
for_yanCommented:
Too complex thing. Just find all puts and set breakpoints
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
You can do
public class TableHashMap<K, V> extends LinkedHashMap<K, V> {
        @Override
        public V put(K key, V value) {
            return super.put(key, value);
        }
    }

Open in new window

0
 
CEHJCommented:
You can do that as a temporary thing
0
 
fhdragonCommented:
you can do it:
        public class TableHashMap<String,MyTableClass> extends LinkedHashMap<String, MyTableClass> {

	      @Override
	      public MyTableClass put (String key, MyTableClass value) {
	            return super.put(key, value);
	      }

	}

Open in new window

0
 
CEHJCommented:
:)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now