Tutorial by Examples: ce

Use the orElseThrow() method of Optional to get the contained value or throw an exception, if it hasn't been set. This is similar to calling get(), except that it allows for arbitrary exception types. The method takes a supplier that must return the exception to be thrown. In the first example, the...
Enum can be considered to be syntax sugar for a sealed class that is instantiated only a number of times known at compile-time to define a set of constants. A simple enum to list the different seasons would be declared as follows: public enum Season { WINTER, SPRING, SUMMER, FA...
One use of SharedPreferences is to implement a "Settings" screen in your app, where the user can set their preferences / options. Like this: A PreferenceScreen saves user preferences in SharedPreferences. To create a PreferenceScreen, you need a few things: An XML file to define the av...
Pagination p = new Pagination(10); Timeline fiveSecondsWonder = new Timeline(new KeyFrame(Duration.seconds(5), event -> { int pos = (p.getCurrentPageIndex()+1) % p.getPageCount(); p.setCurrentPageIndex(pos); })); fiveSecondsWonder.setCycleCount(Timeline.INDEFINITE); fiveSecondsWon...
A simple example of producer-consumer problem solution. Notice that JDK classes (AtomicBoolean and BlockingQueue) are used for synchronization, which reduces the chance of creating an invalid solution. Consult Javadoc for various types of BlockingQueue; choosing different implementation may drastica...
First of all for a JAX-RS application must be set a base URI from which all the resources will be available. For that purpose the javax.ws.rs.core.Application class must be extended and annotated with the javax.ws.rs.ApplicationPath annotation. The annotation accepts a string argument which defines...
format() from SimpleDateFormat class helps to convert a Date object into certain format String object by using the supplied pattern string. Date today = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy"); //pattern is specified here System.out.println(date...
// Obtain an instance of JavaScript engine ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("nashorn"); // Setup a custom writer StringWriter stringWriter = new StringWriter(); // Modify the engine context so that the custom writ...
Annotation @XmlAccessorType determines whether fields/properties will be automatically serialized to XML. Note, that field and method annotations @XmlElement, @XmlAttribute or @XmlTransient take precedence over the default settings. public class XmlAccessTypeExample { @XmlAccessorType(XmlAccessT...
var person = new Person { Address = null; }; var city = person.Address.City; //throws a NullReferenceException var nullableCity = person.Address?.City; //returns the value of null This effect can be chained together: var person = new Person { Address = new Address { ...
Interfaces can be extremely helpful in many cases. For example, say you had a list of animals and you wanted to loop through the list, each printing the sound they make. {cat, dog, bird} One way to do this would be to use interfaces. This would allow for the same method to be called on all of th...
Unlike in languages like Python, static properties of the constructor function are not inherited to instances. Instances only inherit from their prototype, which inherits from the parent type's prototype. Static properties are never inherited. function Foo() {}; Foo.style = 'bold'; var foo = ne...
Variables can be accessed via dynamic variable names. The name of a variable can be stored in another variable, allowing it to be accessed dynamically. Such variables are known as variable variables. To turn a variable into a variable variable, you put an extra $ put in front of your variable. $va...
Python lists are zero-indexed, and act like arrays in other languages. lst = [1, 2, 3, 4] lst[0] # 1 lst[1] # 2 Attempting to access an index outside the bounds of the list will raise an IndexError. lst[4] # IndexError: list index out of range Negative indices are interpreted as countin...
dictionary = {"Hello": 1234, "World": 5678} print(dictionary["Hello"]) The above code will print 1234. The string "Hello" in this example is called a key. It is used to lookup a value in the dict by placing the key in square brackets. The number 1234 is ...
An enum provides a set of related values: enum Direction { case up case down case left case right } enum Direction { case up, down, left, right } Enum values can be used by their fully-qualified name, but you can omit the type name when it can be inferred: let dir = Dire...
In addition to the built-in round function, the math module provides the floor, ceil, and trunc functions. x = 1.55 y = -1.55 # round to the nearest integer round(x) # 2 round(y) # -2 # the second argument gives how many decimal places to round to (defaults to 0) round(x, 1) ...
Sometimes, when you have local changes incompatible with remote changes (ie, when you cannot fast-forward the remote branch, or the remote branch is not a direct ancestor of your local branch), the only way to push your changes is a force push. git push -f or git push --force Important not...
import random shuffle() You can use random.shuffle() to mix up/randomize the items in a mutable and indexable sequence. For example a list: laughs = ["Hi", "Ho", "He"] random.shuffle(laughs) # Shuffles in-place! Don't do: laughs = random.shuffle(laughs) p...
jQuery is the starting point for writing any jQuery code. It can be used as a function jQuery(...) or a variable jQuery.foo. $ is an alias for jQuery and the two can usually be interchanged for each other (except where jQuery.noConflict(); has been used - see Avoiding namespace collisions). Assumi...

Page 4 of 134