Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

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 :).

Sunday, July 28, 2013

Check if the device has Internet

On many platform, and specifically on mobile devices, there is a constant need to know if there is a connection to the internet (or the outer world).

I've seen countless Android implementations, trying to figure and configure the network states, attempting to understand if the underline network can access the outer world or not, I've also wrote one... :(

Thing about all these implementations, they all fail the scenario where the device is connected to a network, which is not connected to the outer world, for example a router which is not connected to the wall...

Today, I bring you a very simple very easy way to perform this check:

private void checkWebConnectiity()
  throws IOException {
 Socket socket = null;
 try {
  socket = new Socket("your.domain.com", 80);
 } catch (IOException e) {
  throw e;
 } finally {
  if (socket != null)
   try {
    socket.close();
   } catch (IOException e) {
    logWarning("Ignoring...", e);
   }
 }
}

This covers most of the cases you would need, and is an absolute check, to whether or not you have access to the internet.

You can also return a boolean for your satisfaction, I required an exception to determine the cause of error...