Sunday, November 16, 2014

Java 8 - List conversion to List

OK,

So we all know how we would do this in Java 6 right... something similar to:

public class ConvertListType1 {

 public static void main(String[] args) {
  List<String> items1 = Arrays.asList("1111", "2222", "3333", "4444", "5555", "6666");
  List<Long> items2 = new ArrayList<Long>();
  for (String string : items1) {
   items2.add(Long.parseLong(string));
  }
 }
}

Now lets break it down a bit:

public class ConvertListType2 {

 public static void main(String[] args) {
  List<String> items1 = Arrays.asList("1111", "2222", "3333", "4444", "5555", "6666");
  List<Long> items2 = collectConvertedItems(items1);
 }

 protected static long function(String string) {
  return Long.parseLong(string);
 }

 protected static List<Long> collectConvertedItems(List<String> items1) {
  List<Long> items2 = new ArrayList<Long>();
  for (String string : items1) {
   items2.add(function(string));
  }
  return items2;
 }
}

Now this seems to make no sense in Java-6, but have great meaning with the usage of Streams in Java-8.

public class ConvertListType3 {

 public static void main(String[] args) {
  Function<String, Long> function = new Function<String ,Long>() {
   @Override
   public Long apply(String string) {
    return Long.parseLong(string);
   }
  };
  List<Long> items2 = Arrays.asList("1111", "2222", "3333", "4444", "5555", "6666").stream().map(function).collect(Collectors.toList());
 }
}

So what is going on?

Stream is a new entity in the later Java version, that allows functional manipulation of a collection of items(it does not make coffee just yet), so we take the list of strings and create a stream of it for further manipulation.

NOTE that function is now an anonymous type, assigned as a local variable that is passed to the mapping, which returns a new stream with the new generic type defined by the return type of the map Function... This is where the conversion really happens, they called it map (which also makes some sense, mapping from one type to another).

Later there is a call to collect, which in the most abstract description, wraps your new stream in a new wrapper, in our simple case just returns a list with the new type Long.
NOTE, collect can do much more than just make a list.

That is it. I was trying to keep it simple as I recall it was hard for me to get my head around the transition from Java-6 to Java-8, Hope I saved you a few minutes :).

Some Git Quick Command Line Action Reference

Create new repository:
git init

Add remote repository url:
git remote add origin [url-to-repository]

Set remote repository url:
git remote set-url origin [url-to-repository]

Get current remote repository url:
git config --get remote.origin.url

Be sure to know which one to use!!
git reset [--soft|--mixed|--hard|--merge|--keep]

Add Sub-Module/Sub-Repo:
git submodule add [url-to-repository]

Update Sub-Module/Sub-Repo:
git submodule update

Remove reference to a Sub-Module:
git rm -rf --cached [folder-name]

Creates a new local branch:
git checkout -b [branch-name]

Switch to branch:
git checkout [branch-name]

Delete local branch:
git branch -d [branch-name]

Delete local branch(FORCED):
git branch -D [branch-name]

Pushes the current branch, -u sets also the pull branch:
git push -u origin [branch-name]

See the list of your branches:
git branch

Add all new files:
git add -A

Commit changes:
git commit -m"[Your message here]"

Commit changes as someone else(blame - not really):
git commit -m"[Your message here]" --author="John Doe <john@doe.com>"

Push Commits:
git push

If when checking you git status you see "Head detached from xxxxx" using the following is the simplest solution:
git config --global push.default simple

Fetch remote commits:
git fetch

Update local copy:
git pull

Create Tag:
git tag your-tag-label

Push tag: (Use -f to override existing tags in the remote repo)
git push origin ["your tag label"] -f

I know there are far more commands, but as a starting point this is a good collection of actions.
If you have some common commands you use that I've left out, let me know.

Tuesday, April 1, 2014

Setup your Java 8 Eclipse Project

If the time had come and you can easily download and install Eclipse with Java 8 support, great... if not, try this out: Setup Eclipse with Java 8 and Lambda expressions

Setup a Java 8 project:
  • Create a Java project
  • Make sure you have a Java 8 JDK installed in Eclipse goto: window > preferences > Java > Installed JREs, and if not add it!
  • If you would like your entire workspace to be configured to work with Java 8 perform the flowing action in window > preferences. Otherwise perform these on the project properties.
  • You would have to make sure that your compiler compliance level match 1.8:
    **> Java Compiler, and make sure the compliance level is set to 1.8.
  • Make sure the Java library used in the project is the Java 8 JDK at: **> Java Build Path
  • Go to this link and this link, copy the classes as string(Yeah simply mark it all and copy it)

    And now...
  • Click on a package in your project and paste the text(Yeah simply paste the "text" onto the package in the Package Explorer!)
  • Poof... you have a two classes in that package(I just love Eclipse don't you!?).
  • It should compile and be executable.

Let me know if you had issues, so I can make this better!

Setup Eclipse with Java 8 and Lambda expressions

I thought this would be a walk in the park... but this was annoying as F***, I've spent a half a day searching Google only to find a pile of rubbish that lead to dead links and wrong instructions to download versions of Eclipse that don't do the trick or take over 3 hours to download(DEAR GOD 3 HOURS), and a bunch instruction about a pre-release Eclipse versions which does not work!

So... what does work:


Took exactly a couple of minutes to get it all set up...