Tutorial by Examples

Examples of using Default Methods introduced in Java 8 in Map interface Using getOrDefault Returns the value mapped to the key, or if the key is not present, returns the default value Map<Integer, String> map = new HashMap<>(); map.put(1, "First element"); map.get(1); ...
Under most circumstances, it is simpler from a code-design standpoint to use existing generic Exception classes when throwing exceptions. This is especially true if you only need the exception to carry a simple error message. In that case, RuntimeException is usually preferred, since it is not a che...
Map<Integer, String> map = new HashMap<>(); map.put(1, "First element."); map.put(2, "Second element."); map.put(3, "Third element."); map.clear(); System.out.println(map.size()); // => 0
Consider the following JSON string: { "title": "test", "content": "Hello World!!!", "year": 2016, "names" : [ "Hannah", "David", "Steve" ] } This JSON object can...
Create the JSONObject using the empty constructor and add fields using the put() method, which is overloaded so that it can be used with different types: try { // Create a new instance of a JSONObject final JSONObject object = new JSONObject(); // With put you can add a name/va...
// Create a new instance of a JSONArray JSONArray array = new JSONArray(); // With put() you can add a value to the array. array.put("ASDF"); array.put("QWERTY"); // Create a new instance of a JSONObject JSONObject obj = new JSONObject(); try { // Add the JSONAr...
Android logs can be filtered directly from the UI. Using this code public class MainActivity extends AppCompatActivity { private final static String TAG1 = MainActivity.class.getSimpleName(); private final static String TAG2 = MainActivity.class.getCanonicalName(); @Override p...
Custom filters can be set and save from the UI. In the AndroidMonitor tab, click on the right dropdown (must contains Show only selected application or No filters) and select Edit filter configuration. Enter the filter you want And use it (you can selected it from the same dropdown) Important...
First example reimplemented in Kotlin and using RxJava for cleaner interaction. import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.support.v7.widget.RecyclerView import rx.subjects.PublishSubject public class SampleAdapter(private val ite...
Two methods in java.util.Collection create an array from a collection: Object[] toArray() <T> T[] toArray(T[] a) Object[] toArray() can be used as follows: Java SE 5 Set<String> set = new HashSet<String>(); set.add("red"); set.add("blue"); ...
Methods can also have generic type parameters. public class Example { // The type parameter T is scoped to the method // and is independent of type parameters of other methods. public <T> List<T> makeList(T t1, T t2) { List<T> result = new ArrayList<T&...
When we create a button in layout, we can use the android:onClick attribute to reference a method in code to handle clicks. Button <Button android:width="120dp" android:height="wrap_content" android:text="Click me" android:onClick="handleCl...
Java SE 5 Since Java 1.5 you can get a String representation of the contents of the specified array without iterating over its every element. Just use Arrays.toString(Object[]) or Arrays.deepToString(Object[]) for multidimentional arrays: int[] arr = {1, 2, 3, 4, 5}; System.out.println(Arrays.toS...
Java 7 introduced the very useful Files class Java SE 7 import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.Path; Path path = Paths.get("path/to/file"); try { byte[] data = Files.readAllBytes(path); } catch(IOException e) { e.printStackTrace();...
When we create any View in layout, we can use the android:onClick attribute to reference a method in the associated activity or fragment to handle the click events. XML Layout <Button android:id="@+id/button" ... // onClick should reference the method in your activity or fra...
Strings can be internationalised by defining a different strings.xml for each language you support. You add a new language by creating a new values directory with the ISO language code as a suffix. For example, when adding a German set your structure might look like follows: When the system look...
Strings are typically stored in the resource file strings.xml. They are defined using a <string> XML element. The purpose of strings.xml is to allow internationalisation. You can define a strings.xml for each language iso code. Thus when the system looks for the string 'app_name' it first che...
In order to define a string array write in a resources file res/values/filename.xml <string-array name="string_array_name"> <item>text_string</item> <item>@string/string_id</item> </string-array> for example res/values/arrays.xml <?x...
Dimensions are typically stored in a resource file names dimens.xml. They are defined using a <dimen> element. res/values/dimens.xml <?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="small_padding">5dp</dimen> &l...
Integers are typically stored in a resource file named integers.xml, but the file name can be chosen arbitrarily. Each integer is defined by using an <integer> element, as shown in the following file: res/values/integers.xml <?xml version="1.0" encoding="utf-8"?> &...

Page 17 of 1336