How to use Iterator, for and foreach-loop in Java

Here is a very good example on how to use the Iterator, for and foreach-loop in Java:

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
public class JavaApplication {
 
  public static void main(String[] args) throws IOException {
    List<String> names = new ArrayList<>();
    names.add("Benny");
    names.add("Sarah");
    names.add("Daniel");
    names.add("Maria");
 
    // Iterator
    Iterator<String> iterator = names.iterator();
    while (iterator.hasNext()) {
      System.out.println(iterator.next());
    }
 
    // for
    for (int i = 0; i < names.size(); i++) {
      System.out.println(names.get(i));
    }
 
    // foreach
    for (String name : names) {
      System.out.println(name);
    }
  }
}

JavaScript AMD Example with RequireJS

One line of code is worth ten thousand words.

./index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Asynchronous Module Definition Example with RequireJS</title>
  </head>
  <body>  
    <!-- Let's use RequireJS as module loader (there are also others!) -->
    <script src="./js/require-1.0.4.js"></script>
    <script>
      require(['js/my_amd_module'], // Requires ./js/my_amd_module.js
      function(myModule)
      {
        myModule.sayHello();
        myModule.doSomething();        
      });
    </script>
</html>

./js/my_amd_module.js

define('js/my_amd_module', // module name, has to match filename (without .js)
  ['js/jquery-1.7.min'], // requirements of this module (./js/jquery-1.7.min.js)
  function($) // $ for jQuery
  {
    return {
      sayHello: function()
      {
        alert('Hello World!');
      },
      doSomething: function()
      {
        alert('I did.');
      }
    };
  });

Facebook Authentication with JavaScript SDK

Over the years many ways have been created in order to authenticate through Facebook. One of the easiest ways is to use the Facebook Markup Language (FBML). However, from January 2012 is FBML is no longer supported. Who wants to use Facebook Connect to login and logout then, should use the JavaScript SDK provided by Facebook. This supports authentication via OAuth 2.0, which is the recommended way to handle authentication. I programmed an example, that shows how to login and logout with the JavaScript SDK. This example is based on the current JavaScript SDK methods and does not use deprecated methods (because in July 2011 there were some changes).
Facebook Authentication with JavaScript SDK weiterlesen

String Byte Buffer Stream Example

Dieses grundlegende Beispiel zeigt, wie man eine Zeichenkette (String) in einem Puffer (Buffer), der ganze 5 Bytes aufnehmen kann, abspeichert. Das Einlesen erfolgt dabei über einen Strom (Stream):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.io.ByteArrayInputStream;
import java.io.InputStream;
 
  public static void main(String[] args) throws Exception
  {
    // Create a 5-Byte (40-Bit) buffer
    // Note: 5-Bytes are 5 characters in UTF-8 (8-Bit per character)
    int bufferSize = 5;
    byte[] buffer = new byte[bufferSize];
    // Read bytes of a string into buffer
    String text = "Hello World!";
    InputStream is = new ByteArrayInputStream(text.getBytes());
    int bytesRead = is.read(buffer, 0, bufferSize);
    // Print result
    String bufferContent = new String(buffer);
    System.out.println("Bytes read: " + bytesRead); // Bytes read: 5
    System.out.println("Result: " + bufferContent); // Result: Hello
    // Print result with a stream reader
    ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
    BufferedReader br = new BufferedReader(new InputStreamReader(bais));
    bufferContent = br.readLine();
    System.out.println("Bytes read: " + bytesRead); // Bytes read: 5
    System.out.println("Result: " + bufferContent); // Result: Hello
  }
}

Java HashMap Beispiel

Ein Beispiel für das Durchlaufen einer HashMap in Java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.HashMap;
 
public class HashMapExample
{
    public static void main(String[] args)
    {
        HashMap<String,Double> myHashMap = new HashMap<String,Double>();
 
	// HashMap durchlaufen
        for(String key : myHashMap.keySet())
        {
            System.out.println("Key: "+key);
            System.out.println("Value: "+myHashMap.get(key));
        }
 
	// Exemplarisches Einfügen:
        Double myDouble = myHashMap.get("Dagobert");
 
        if(myDouble != null)
        {
            System.out.println("Es gibt einen Double-Wert, 
            welcher unter dem Key 'Dagobert' abgelegt ist.");
        }
        else
        {
	    /* Es gibt keinen Wert für den Key 'Dagobert',
            also fügen wir den Double-Wert '100.00' für diesen Key ein. */
            myHashMap.put("Dagobert", 100.00);
        }
    }
}