HashMap merge (key, value, double function) method in Java, example
HashMap merge (key, value, bifunction) method in Java, example Original text: https://www . geesforgeks . org/hashmap-merge key-value-bifunction-method -in-Java-with-examples/ Hash map class‘s Merge (key, value, double function) method is used Used to combine multiple mapped values of a key using the given mapping function. Bucket is actually an index into an array, which is called table in the HashMap implementation. So table[0] is called bucket0, table[1] is called bucket1, and so on. If the key does not exist or is associated with null, it only needs to output the key together with the corresponding value in Hashmap as a new entry. However, if the key has saved some values, the remapping function will match the old and new values with the given key. Merge. If the key is empty, it will always be mapped to bucket 0, because the null pointer is abnormal and no hash is calculated for the null key Syntax: public V merge(K key span>, V value, BiFunction remappingFunction) Parameters:This method accepts three parameters: key: is the key that we have a specific value. If two keys have the same value, they will be merged. Value: is the index corresponding to the specific key stored in the bucket.…
DoubleBuffer rewind() method in Java, example
Double buffer-rewind-in-Java-method-with-examples/ The Rewind() method of the java.nio.DoubleBuffer class is used to rewind this buffer. This method sets the position to zero, the limit is not affected, and if there are any previously marked positions, they are discarded. This method should be called when a channel write or acquisition operation is required. This means that if the buffer data has been written, it needs to be copied to another array. For example: out.write(buf); // Writes remaining databuf.rewind(); // Rewind the buffer buf.get(array); // Copy the data into array Syntax: public final DoubleBuffer rewind() Parameters:This method does not take any parameters. Return value:This method returns this buffer. The following is an example illustrating the rewind() method: Example 1: // Java program to demonstrate// rewind() methodimport java.nio.*;import java.util.*;public class GFG { public static void main(String[] args) { // defining and allocating DoubleBuffer // using allocate() method DoubleBuffer doubleBuffer = DoubleBuffer.allocate(4); // put char value in doubleBuffer // using put() method doubleBuffer.put(10.5); doubleBuffer.put(20.5); // print the double buffer System.out.println(“Buffer before operation: ” “p”>.toString( >.array()) o”>+ “\nPosition: ” .position() =”o”>+ “\nLimit: ” >doubleBuffer.limit()); >doubleBuffer.rewind(); // print the doublebuffer System.out. println(“\nBuffer after operation: ” class=”o”>+ Arrays.toString ( array()) /span> + doubleBuffer.position() ” + doubleBuffer.limit()); }} Output: Buffer before operation: [10.5, 20.5 span>, 0.0, 0.0 ]Position: 2 Limit: 4Buffer…
Minguo chronology dateNow(clock) method in Java, example
Minguo chronology dateNow (clock) method in Java, example Original text: https://www . geeksforgeeks . org/Minguo chronology-date now clock-in-Java-method -with-example/ The dateNow() method of the Java . time . chrono . Minguo Chronology class is used to retrieve the specified Get the current Minguo date in the clock object according to the Minguo calendar system. Syntax: public MinguoDate dateNow(Clock clock) Parameters: This method starts with The object of Clock is a parameter, used to obtain the current Republic of China date according to the Republic of China calendar system from the specified clock object. Return value:This method returns the Republic of China date according to the Republic of China calendar system from the specified clock object. The following is an example illustrating the dateNow() method: Example 1: // Java program to demonstrate// MinguoNow() methodimport java.util.*;import java.io.*;import java.time.*;import java.time.chrono.*;public class GFG { public static void main(String[] argv) { try { =”n”>MinguoDate hidate = MinguoDate.now(); // getting MinguoChronology // used in MinguoDate MinguoChronology crono = hidate.getChronology(); // creating and initializing span class=”o”>= Clock.systemUTC(); given clock object // by using dateNow() method MinguoDate date = crono.dateNow(clock); // display the result System.out.println ( “MinguoDate is: ” ); } catch (DateTimeException e) { System.out.println( span class=”s”>”passed parameter can “ + “not form a date” ); System.out.println( “Exception thrown: ” + e); } }} Output: MinguoDate is:…
SimpleTimeZonegetDSTSavings() method in Java, example
SimpleTimeZone getDSTSavings() method in Java, examples Original text: https://www . geeksforgeeks . org/simple time zone-getdstssaves-method-in-Java-with-examples/ The SimpleTimeZone class‘s getdstsings() method returns the amount of time (in milliseconds) by which the clock has advanced during daylight saving time. Syntax: public int getDSTSavings() Parameters :The function does not accept any parameters. Return value:This method returns the advance of time relative to standard time (in milliseconds). Exceptions:The function does not throw any exceptions. The following program demonstrates the above functionality: Program 1: // program to demonstrate the// function java.util.date.getDSTSavings()import java.util.SimpleTimeZone;public class GFG { public static void main (String[] args) { // creating simple time zone object SimpleTimeZone obj = new SimpleTimeZone(580, “India”); // check DST saving and print System.out.println(“DST saving = ” .getDSTSavings()); } } Output: DST saving = 0 Program 2: // program to demonstrate the// function java.util.date.getDSTSavings()import java.util.SimpleTimeZone;public class GFG { public static void main (String[] args) { // creating simple time zone object SimpleTimeZone obj = new SimpleTimeZone(580, “US”); // check DST saving and print System.out.println(“DST saving = ” .getDSTSavings()); } } Output: DST saving = 0
Area offset of hour(int) method in Java, example
Zone offset of hours(int) method in Java, example Original text: https://www . geesforgeks . org/zone offset-of hoursint-method-in-Java- with-examples/ of hour(int) of ZoneOffset class in java.time package Method used to get an instance of ZoneOffset, using the offset in hours passed as parameter. This method takes the hour as an argument as an int and converts it to a ZoneOffset. The maximum supported range is from +18:00 to -18:00 (inclusive). Syntax: public static ZoneOffset ofHours(int hours) Parameters:This method accepts a parameter hour which is an integer to be converted to a zone offset instance. Return Value: This method returns a region offset instance parsed from the specified hour. Exception: If the hour is invalid, this method throws a DateTimeException. The following example illustrates the ZoneOffset.ofHours() method: Example 1: // Java code to illustrate ofHours() methodimport java. time.*;public class GFG { public static void main(String[] args) { // Get the hours int hours = 10; span> = ZoneOffset.ofHours(hours); System.out. span>println(zoneOffset) ; }} Output: +10:00 Example 2: Demonstrate date and time exception // Java code to illustrate ofHours() methodimport java.time.*;public class GFG { public static void main(String[] args) { // Get the invalid hours int hours = 20; try { // ZoneOffset using ofHours() method = ZoneOffset.ofHours(hours); } catch (Exception e) { span class=”n”>System.out.println(e);…
MatcherappendTail(StringBuffer) method in Java, example
Matcher appendTail(StringBuffer) method in Java, examples Original text: https://www . geesforgeks . org/matcher-appendtailstringbuffer-method-in-Java-with-examples/ The appendTail(StringBuffer) method of the matcher class behaves as an append and replace method. This method reads the input string and appends it to the given string at the trailing position. Syntax: public StringBuffer appendTail(StringBuffer buffer) Parameters:This method takes One parameter buffer, which is the StringBuffer that stores the target string. Return value:This method returns a string that replaces the target string. The following example illustrates the Matcher.appendTail() method: Example 1: // Java code to illustrate appendTail() methodimport java.util.regex.*;public class GFG { public static void main( String[] args) { // Get the regex to be checked String regex = “(Geeks)”; // Create a pattern from regex Pattern pattern = Pattern. compile(regex); // Get the String to be matched String stringToBeMatched = “GeeksForGeeks Geeks for For Geeks Geek” ; // Create a matcher for the input String Matcher matcher = pattern.matcher(stringToBeMatched); System.out .println(“Before Replacement: ” + stringToBeMatched); / / Get the String to be replaced String stringToBeReplaced = “GEEKS”; StringBuffer buffer = new StringBuffer(); // Replace every matched pattern // with the target String while (matcher.find()) { matcher.appendReplacement(buffer, stringToBeReplaced); } // Add the replaced string at the tail // using the appendTail() method matcher .appendTail(buffer); // Print the replaced matcher System.out.println(“After Replacement: ” class=”p”>.toString()); }} Output: Before Replacement: GeeksForGeeks Geeks for For Geeks…
Sequencer getInstance(Locale) method in Java, example
Sequencer getInstance(Locale) method in Java, example Original text: https://www . geesforgeks . org/collator-getinstance locale-method-in-Java-with- example/ java.text.Collator class’s getInstance(Locale) method is used to get a new Collator with the required locale object. Syntax: public static Collator getInstance(Locale desiredLocale) Parameters: This method takes the area where the sorter object needs to be created. Return Value: It provides a new sorter object with the required locale. The following is an example illustrating the getInstance() method: Example 1: // Java program to demonstrate// getInstance() method import java.text.*;import java.util.*; import java.io.*;public class GFG { public static void main(String[] argv) { try { // Creating and initializing new simple rule String simple = “; // Creating and initializing // new RuleBasedCollator Object RuleBasedCollator col_1 = new RuleBasedCollator (simple); =”c1″>// Creating and initializing Collator // Object with Locale.UK // using getInstance() method Collator col_2 Collator.getInstance(Locale.UK); // display result if (col_1.equals(col_2)) span>.out.println ( col_1 + ” is equal to ” p”>); else System. out.println( col_1 + ” is not equal to ” + col_2); } catch (ClassCastException e) { System.out.println(“Exception thrown : ” + e ); } catch (ParseException e) { System.out.println(“Exception thrown : ” + e); } }} Output: java .text.RuleBasedCollator@5eb2e1c2 is not equal to java.text.RuleBasedCollator@289747d6 Example 2: // Java program to demonstrate// getInstance() methodimport java.text.*;import java.util.*;import java.io .*;public class GFG { public static void main( String[] argv) { try { >// Creating and initializing Collator Object // with Locale.UK //…
DurationplusMinutes(long) method in Java, example
Duration plusMinutes(long) method in Java, examples Original text: https://www . geesforgeks . org/duration-plusminutslong-method-in-Java-with-examples/ java.time packageThe plusMinutes(long) method of the Duration class is used to obtain the duration An immutable copy of the time, with the specified number of minutes added, passed as argument. Syntax: public Duration plusMinutes(long numberOfMinutes) Parameters:This method accepts A parameter Minutes, which is the number of minutes to be added. It can be positive or negative, but cannot be empty. Return Value: This method returns a Duration which is an immutable copy of the existing duration with the argument minutes added to in. Exception: If a numerical overflow occurs, this method throws an algorithm exception. The following example illustrates the Duration.plusMinutes() method: Example 1: // Java code to illustrate plusMinutes() methodimport java. time.Duration;public class GFG { public static void main(String[] args) { span> duration1 = Duration.parse(“P2DT3H4M”); // Get the duration added // using plusMinutes() method System.out.println(duration1.plusMinutes(2)); }} Output: PT51H6M Example 2: // Java code to illustrate plusMinutes() methodimport java.time.Duration;public class GFG { public static void main(String[] args) { // Duration 1 using parse() method Duration duration1 = Duration.parse(“P0DT0H4M”); // Get the duration added // using plusMinutes() method System.out. println(duration1.. span>plusMinutes(5) ); }} Output: PT9M Reference:https://docs . Oracle . com/javase/9/docs/API/Java/time/duration . html # plusMinutes-long-
ThaiBuddhistChronologyeraOf() method in Java, example
ThaiBuddhistChronology eraOf() method in Java, example Original text: https://www . geeksforgeeks . org/thaibudhistderor-eraof-method-in-Java-with-example/ The eraOf() method of Java . time . chrono . thaibudhistory class is used to retrieve ThaiBuddhistEra using a numeric value. Syntax: public ThaiBuddhistEra eraOf(int eraValue) Parameters: This method will Integer value as parameter to which ThaiBuddhistEra will be generated. Return value:This method returns the Thai historical era using a numerical value. The following is an example illustrating the eraOf() method: Example 1: // Java program to demonstrate// eraOf() methodimport java.util.*;import java.io.*;import java.time.*;import java.time.chrono.*;public class GFG { public static void main(String[] argv) { try { =”n”>ThaiBuddhistDate hidate = ThaiBuddhistDate.now(); // getting ThaiBuddhistChronology // used in LocalDate ThaiBuddhistChronology crono = hidate.getChronology(); // getting ThaiBuddhistEra for the span> class=”n”>ThaiBuddhistEra era = crono .eraOf(0); // display the result System.out.println ( /span> era); } catch (DateTimeException e) { >.out.println( “ThaiBuddhistEra is invalid”); System.out.println( >+ e); } }} Output: ThaiBuddhistEra is: BEFORE_BE Example 2: // Java program to demonstrate/ / eraOf() methodimport java.util.* ;import java.io.*;import java.time.*; import java.time.chrono.*;public class GFG { public static void main(String[] argv) { try { Object ThaiBuddhistDate.now(); “n”>ThaiBuddhistChronology crono = hidate.getChronology(); c1″>// getting ThaiBuddhistEra for the // given integer value // by using eraOf( ) method ThaiBuddhistEra era = crono.eraOf( 1); // display the result System.out. span>println( ”ThaiBuddhistEra is: ” “>} catch (DateTimeException e) { System .out.println( ”ThaiBuddhistEra is invalid”); System.out.println( + e); } }} Output: ThaiBuddhistEra…
TimeZoneinDaylightTime() method in Java, example
TimeZone inDaylightTime() method in Java, examples Original text: https://www . geesforgeks . org/time zone-indaylighttime-method-in-Java-with-examples/ The inDaylightTime(Date) method of the TimeZone class in Java is used to check and verify whether the given date is summer time. Syntax: public abstract boolean inDaylightTime(Date *date*) Parameters:This method takes a parameter of date type DateThe date type is the given date to be verified. Return Value: If the passed date is not valid relative to daylight saving time, this method returns a Boolean value of “True”, otherwise the method returns a Boolean value of “False”. The following program illustrates the use of the inDaylightTime() method in Java:Example 1: // Java code to demonstrate// inDaylightTime() methodimport java.util.*;public class SimpleTimeZone_Demo { public static void main(String[] args ) { // Creating a TimeZone “>TimeZone.getTimeZone(“Europe/Rome”); // Creating date object Date dt = new span> Date(); // Verifying daylight “>offtime_zone.inDaylightTime(dt); // Checking the value of day light System.out.println(“Is it in day” class=”o”>+ ” light saving time? ” n”>bool_daylight); }} Output: Is it in day light saving time? true Example 2: // Java code to demonstrate// inDaylightTime() methodimport java.util.*;public class SimpleTimeZone_Demo { public static void main(String[] args ) { // Creating a TimeZone “>TimeZone.getTimeZone(“Pacific/Pago_Pago”); // Creating date object Date dt = new Date(); // Verifying daylight boolean bool_daylight = offtime_zone.inDaylightTime(dt); // Checking the value…