Friday, April 23, 2010

Finding Intersections Destructively / Nondestructively - Java Collections

What is an intersection?


Mathematical set theory states an intersection is the common elements between given sets. It's stated as such, A ∩ B, where A and B are different sets. 


Visually it looks like this: 















When would you need to calculate intersects?


Given n collections, you need to identify only the elements they have in common and ignore the rest.


Admittedly, I'm a bit spoiled coming from a web technology wherein most cases you can just write up some snazzy SQL to find the commonality and return the record set. In a good sized Java application though you'll often have a good amount of objects and data stored using collections in cache, so SQL is out of the question. You may be tempted to go down the road and create some sort of method that looks like so numberOfOccurrences(Set, String)that returns the number of occurrences and you use that number to determine what to drop and what to keep. This could work but there is a better, more efficient way to achieve this using a built in Java method - retainAll() that resides in the Collections interface. Lets see how:


The code







































  1. We create two sets each holding fruits. You'll see that Apple and Orange are the common elements between the two sets. Make believe these two sets are your collections sitting in cache.
  2. We create an HashSet called intersection to hold our common values. Since we are created a holding set, we are finding the intersection nondestructively, which means without modifying the any of sets being compared. Simply instantiate the intersection set passing in set1 as the initializer.
  3. To find the intersection, all we do is call retainAll(Collection c) passing in the collection to be compared.
  4. Then we print out the intersection results.
  5. Next I show you how to destructively find the intersection by using set2 against set1, again using retainAll() and printing out the both sets. You'll see that set2 has been altered.
You should see the following results...









So, there you have it. Pretty painless right?

Thursday, April 8, 2010

Helpful IntelliJ IDEA Shortcuts

IntelliJ IDEA Shortcuts

After settling in at the new gig, the IDE of choice for my team is IntelliJ IDEA. I've been using Eclipse for many many years now so there has been growing pains because of the simple fact that I was extremely familiar with all the ins-and-outs of Eclipse and the shortcuts they offered. After a month or so of using IntelliJ, I'm getting more comfortable with it. I found myself writing these notes on a scratch pad and figured I post an entry on it before I spill hot coffee all over my notepad and start crying like a little baby. Without further ado, heres my list:

Various Fixes - alt+enter

This tool is equivalent to ctrl+1 in Eclipse and acts as a kind of quick fix tool for you. You'll probably use it the most for the following:
  1. Adding import declarations.
  2. If code is highlighted yellow, IntelliJ is suggesting that you can possibly write your code in a more efficient manner. Simply double click the piece of code and hit alt+enter. This will give you some refactoring options.
Code Complete - ctrl+shift+enter

This nifty shortcut will complete syntactical code for you such as closing braces during a method declaration, or by typing "if" then hitting ctrl+shift+enter will close add the parentheses for evaluation as well as the open and close brackets for the if block. Probably does a lot more but that's what I use this for the most.

Dumb Auto Complete - ctrl+space

This "dumb" autocomplete usually appears automatically after you type the "." after an object variable. For instance assume someArrayList is of type ArrayList and when typing "someArrayList.", suggestions are offered based off of that object appear.

Smart Auto Complete - ctrl+shift+space

This is really useful one. It acts similar to the dumb complete key but there's is a crucial difference. This shortcut narrows suggested methods down by type. For instance, let's take our "someArrayList." example again. Shortly after the period you will get suggested methods based off of the entire ArrayList object. This is the the expected "dumb" auto complete view as I explained in the previous shortcut. However when you instead press ctrl+shift+space, IntelliJ will actually filter those methods based on the type in which the return is being assigned to. So if we were create a integer variable to store the size of someArrayList like so "int size = someArrayList.", smart complete will only expose methods off of the ArrayList object which will only return an int value like size(), hashCode(), lastIndexOf() and indexOf()... Very useful!

View Java Docs - highlight method or class+ctrl+q

Nuff' said.

Highlight instances of a term - highlight item+ctrl+shift+f7

Nifty shortcut that will search, locate and highlight all the matching terms you've selected within a file.

Refactor a piece of code - shift+f6

Highlight code and shift+f6 to view suggested refactoring options.

Extract expression to a method - ctrl+alt+m

There are times where you find yourself writing inline, procedural code that should be in its own method but you just don't get around to extracting it because you're lazy. This awesome shortcut provides a very simple way to highlight code, turn it into a method update your references to it. Just like Geico says, "so easy a caveman could do it". Do it!

Select word the cursor is currently on - ctrl+w

When you really don't feel like using your mouse... geek.

Cycle through open editors - ctrl+left arrow or ctrl+right arrow

Nuff' said.

Cycle through methods in a file -ctrl+up arrow or ctrl+down arrow

Nuff' said.

Once debug servers are setup to launch debug shift+f9+enter

Launches debug server.

Terminate the debug server - ctrl+f2

Nuff' said.

And finally, the usual suspects...
Search for a Java Class - ctrl+n
Search for file in project - ctrl+shift+n
Search for term within project - ctrl+shift+f
Comment out code - highlight lines+ctrl+/

There you have it. Hopefully this can help guide IntelliJ noobs like myself in the right direction. I'm still feeling my way through this tool so if anyone has any other useful tips for IntelliJ, please post a comment. I'd love to hear them!

PEACE.
-Mick