In this post, I'll explain few refactoring tricks and we will use the help of Eclipse as much as possible. Here we go!
01 | Map<String, List<String>> personalItemsMap = new HashMapMap<String, List<String>>(); |
05 | List<string> vaderList = new ArrayList<string>(); |
06 | vaderList.add( "lightsaber" ); |
07 | vaderList.add( "helmet" ); |
08 | vaderList.add( "armor" ); |
09 | personalItemsMap.put( "darth vader" , vaderList); |
12 | List<string> soloList = new ArrayList<string>(); |
13 | soloList.add( "blaster" ); |
15 | personalItemsMap.put( "han solo" , soloList); |
18 | List<string> bobaList = new ArrayList<string>(); |
19 | bobaList.add( "blaster" ); |
20 | bobaList.add( "mandalorian armor" ); |
21 | personalItemsMap.put( "boba fett" , bobaList); |
23 | System.out.println(personalItemsMap); |
Above we create a list of items for each character and add them to an owner-item map. This piece of code is begging for refactoring.
First, I choose the piece of code I'd like to extract as a method.

Then I choose refactor>extract method.

I enter "generateVaderList" as method name and here's our refactored code.
01 | public static void main(String[] args) { |
02 | Map<String, List<String>> personalItemsMap = new HashMap()<String, List<String>>; |
05 | List<string> vaderList = createVaderList(); |
06 | personalItemsMap.put( "darth vader" , vaderList); |
09 | List<string> soloList = new ArrayList<string>(); |
10 | soloList.add( "blaster" ); |
12 | personalItemsMap.put( "han solo" , soloList); |
15 | List<string> bobaList = new ArrayList<string>(); |
16 | bobaList.add( "blaster" ); |
17 | bobaList.add( "mandalorian armor" ); |
18 | personalItemsMap.put( "boba fett" , bobaList); |
20 | System.out.println(personalItemsMap); |
23 | private static List<string> createVaderList() { |
24 | List<string> vaderList = new ArrayList<string>(); |
25 | vaderList.add( "lightsaber" ); |
26 | vaderList.add( "helmet" ); |
27 | vaderList.add( "armor" ); |
30 | </string></string></string> |
No need for a temporary reference assignment so I can edit the below part;
1 | List<string> vaderList = createVaderList(); |
2 | personalItemsMap.put( "darth vader" , vaderList); |
to
1 | personalItemsMap.put( "darth vader" , createVaderList()); |
If I do this refactoring for every character my code will be clearer. But think about it. If I do that I'll have three different methods with similar functionality. These methods create a list, add necessary elements to the list and return the list. What if I take these methods and write a more generic one instead of three methods with similar functionality? Later, if I'd like to add a new character I won't write a createNewCharacterList method. I'll use the generic one instead.
Lets see how we can make the method more generic. It creates a list and adds hard-coded strings to it. This method will be general if I hand it the item strings to add. First I choose the name of "createVaderList" and then right click. Then refactor>rename. I rename it as "createItemList". I'm writing a more general method, remember? I can change the method signature from the menu but this is not so handy so I'll skip that and edit my method by hand.
1 | private static List<string> createItemList() { |
2 | List<string> vaderList = new ArrayList<string>(); |
3 | vaderList.add( "lightsaber" ); |
4 | vaderList.add( "helmet" ); |
5 | vaderList.add( "armor" ); |
will become
1 | private static List<string> createItemList(String... items) { |
2 | List<string> itemList = new ArrayList<string>(); |
4 | for (String item : items) |
In this new method, we -again- create a list. Then we take an arbitrary number of strings. I assure this behavior by using var-args. Then in our beautiful for-loop we add the items to the item list and return the list.
After that we will do the necessary changes in the code. Eclipse can't help us in this case. After we do the necessary changes in our code, our main method will be such as below;
01 | public static void main(String[] args) { |
02 | Map<String, List<String>> personalItemsMap = new HashMapMap<String, List<String>>(); |
05 | personalItemsMap.put( "darth vader" , createItemList( "lightsaber" , "helmet" , "armor" )); |
08 | personalItemsMap.put( "han solo" , createItemList( "blaster" , "vest" )); |
11 | personalItemsMap.put( "boba fett" , createItemList( "blaster" , "mandalorian armor" )); |
13 | System.out.println(personalItemsMap); |
Much clearer and more usable right?