19

What I'm trying to do is this:

List<String> list = new 

and then hit Ctrl+Space and get ArrayList<String>() (among others) to show up in the type proposal.

I thought I had this working previously, but I recently had to reinstall and can't find the setting for it.

This is Eclipse Java EE helios, but I can upgrade to indigo if need be.

I tried looking here for help, but didn't find the info I was looking for. I've tried checking all of the boxes under "Default Proposal Kinds" (Java -> Editor -> Content Assist -> Advanced" to no avail.

falsarella
  • 12,217
  • 9
  • 69
  • 115
Kevin McCarpenter
  • 786
  • 1
  • 7
  • 22
  • Really? That used to work? I've never seen that. I'm interested to see how it's done. – Steve J Oct 18 '11 at 15:53
  • 1
    That is the standard behavior in IntelliJ – Robin Dec 29 '11 at 08:48
  • That does not appear possible even in the link you forwarded. I have never seen that behavior. Are you sure it was working before? – Saish Dec 29 '11 at 18:36
  • And it is standard Netbeans behavior. Why so popular IDE like Eclipse doesn't have so useful feature. Without this I have to deep in to API that sometimes is unknown. – kospiotr Dec 30 '11 at 09:17
  • Is this the same problem? http://stackoverflow.com/q/1582969/1061499 – davioooh Jan 02 '12 at 17:24
  • 2
    Did you make sure that you imported java.util.List first. – Roy Kachouh Jan 02 '12 at 18:00
  • Using 3.5, Eclipse IDE for Java Developers: If I import `java.util.List` first (e.g. by using ctrl+space on `List`), it gives me the suggestions `ArrayList` and `LinkedList` (and some more) when I hit space after `List list = new `. With Indigo the same thing does not work. A fix is given for the question posted above, but it seems still dependent on 'popular' choices (e.g. it gave me the `LinkedList` only after I had selected it once myself). – The Nail Jan 04 '12 at 23:51

6 Answers6

6

Eclipse doesn't know which class implement the interface, and will not load them for all interfaces it has. BUT, Eclipse can learn what you use and show it to you on next use, maybe that's what happened to you, with time you taught Eclipse the implemented classes!

Here's an example of Eclipse before learning/and after learning what classes implement Map.

enter image description here

As you can see in the image, the first time, Eclipse didn't know anything other than HashMap, which I used before.

After that, I used TreeMap and LinkedHashMap by typing them manually (first time only) and Eclipse now cached them.

As the guys suggested, you can put the point on Map and click Ctrl+T it will give all the classes the implements this. Will be helpful the first time.

UPDATE in 2014!

As @K.Carpenter noticed, this feature is disabled in newer Eclipse versions. To re-enable it. Go to Window->Preferences->Java->Editor->Content Assist->Advanced.

Under Default Proposal Kinds, you will need to check Java Type Proposals

Community
  • 1
  • 1
mohdajami
  • 9,604
  • 3
  • 32
  • 53
2

Code like this is one of my pet hates of Java Generics. I use Google's Guava libraries to make my generics code more readable, and as a side-effect don't need this particular feature in Eclipse (though I agree it should be implemented). Guava has similar support for Sets too.

For example, I would normally declare my code as follows:

import com.google.common.collect.Lists;
...
List myList<String> = Lists.newArrayList();
ifx
  • 561
  • 2
  • 13
1

I would like to see Eclipse doing this, but I guess that the content assist never worked without a start character (unless there is some hidden feature we don't know).

Well, having this in mind, what I do to workaround from 'getting locked with the most used implementations' is to look at the javadoc in the All Known Implementing Classes section, to see other possibilities that I could use.

I know it is not an in-Eclipse solution, but it may help some users that got stuck in the same problem.

falsarella
  • 12,217
  • 9
  • 69
  • 115
1

Not pretend to be an answer to your question but I use Quick Fix (Ctrl+1/Ctrl+2) to define a new local variable or field.

First, I type (maybe using Ctrl+Space for Content Assist):

new ArrayList<String>();

Then I press Ctrl+2 and L which assigns statement to new local variable by generating variable definition with type being instantiated:

ArrayList<String> arrayList = new ArrayList<String>();

Finally, I use tab (one may also use Enter) to navigate between inserted arrayList and ArrayList to specify the exact variable name and its type from drop-down list:

List<String> list = new ArrayList<String>();

Pressing tab for third time moves cursor to the end of statement.

Hope, you'll find this way useful too.

Hulk1991
  • 3,079
  • 13
  • 31
  • 46
Eldar Abusalimov
  • 24,387
  • 4
  • 67
  • 71
1

Here's a way that you can add a new template to eclipse, then all you have to do is type arraylist, press ctrl + space and it creates the entire declaration for you. All you have to do is add the type and name.

Save this file, then import it to eclipse

Here’s how to import/export a template

  • Go to Window > Preferences > Java > Editor > Templates

  • Select the templates you want. NB! The checkboxes don’t indicate what’s selected; they are used to enable/disable a template. A template is selected if the whole row in the table is selected, so use Ctrl+Left Click or method specific to your OS to multi-select templates.

  • Click Import… and choose the XML file you received. Or Export… and provide a filename.

Type arrayList

Type arraylist

Press ctrl+space and choose arraylist

Press Ctrl + Space

Fill in the type and name Fill in the type and name

Logan
  • 2,369
  • 19
  • 20
0

I am able to do this but it doesn't show up in the type proposal. Try typing:

List<String> list = new Ar

Hit Ctrl-Space and just accept the first suggestion. It completes to ArrayList<String>() for me (this is using the SpringSource Tool Suite Eclipse distribution).

lucrussell
  • 5,032
  • 2
  • 33
  • 39
  • I can do that, but I don't want to be locked into ArrayList. I want all of the possible ones. I don't want to have to know what the implemented class begins with. Thanks. – Kevin McCarpenter Oct 18 '11 at 16:28