|
1 | 1 | # Java Programs
|
2 | 2 |
|
3 | 3 | ## Q. Write a function to find out duplicate words in a given string?
|
4 |
| -**Approach** |
| 4 | + |
| 5 | +**Approach:** |
5 | 6 |
|
6 | 7 | 1. Define a string.
|
7 | 8 | 1. Convert the string into lowercase to make the comparison insensitive.
|
@@ -42,14 +43,18 @@ public class DuplicateWord {
|
42 | 43 | }
|
43 | 44 | }
|
44 | 45 | ```
|
| 46 | + |
45 | 47 | Output
|
46 |
| -``` |
| 48 | + |
| 49 | +```java |
47 | 50 | Duplicate words in a given string :
|
48 | 51 | big
|
49 | 52 | black
|
50 | 53 | ```
|
| 54 | + |
51 | 55 | ## Q. Find the missing number in an array?
|
52 |
| -**Approach** |
| 56 | + |
| 57 | +**Approach:** |
53 | 58 |
|
54 | 59 | 1. Calculate `A = n (n+1)/2` where n is largest number in series 1…N.
|
55 | 60 | 1. Calculate B = Sum of all numbers in given series
|
@@ -1720,6 +1725,181 @@ Path from vertex 0 to vertex 4 has minimum cost of 3 and the route is [ 0 4 ]
|
1720 | 1725 | <b><a href="#">↥ back to top</a></b>
|
1721 | 1726 | </div>
|
1722 | 1727 |
|
| 1728 | +## Q. How to display 10 random numbers using forEach()? |
| 1729 | + |
| 1730 | +```java |
| 1731 | +( new Random ()) |
| 1732 | + .ints () |
| 1733 | + .limit ( 10 ) |
| 1734 | + .forEach ( System . out :: println); |
| 1735 | +``` |
| 1736 | + |
| 1737 | +<div align="right"> |
| 1738 | + <b><a href="#">↥ back to top</a></b> |
| 1739 | +</div> |
| 1740 | + |
| 1741 | +## Q. How can I display unique squares of numbers using the method map()? |
| 1742 | + |
| 1743 | +```java |
| 1744 | +Stream |
| 1745 | + .of ( 1 , 2 , 3 , 2 , 1 ) |
| 1746 | + .map (s -> s * s) |
| 1747 | + .distinct () |
| 1748 | + .collect ( Collectors . toList ()) |
| 1749 | + .forEach ( System . out :: println); |
| 1750 | +``` |
| 1751 | + |
| 1752 | +<div align="right"> |
| 1753 | + <b><a href="#">↥ back to top</a></b> |
| 1754 | +</div> |
| 1755 | + |
| 1756 | +## Q. How to display the number of empty lines using the method filter()? |
| 1757 | + |
| 1758 | +```java |
| 1759 | +System.out.println ( |
| 1760 | + Stream |
| 1761 | + .of ( " Hello " , " " , " , " , " world " , " ! " ) |
| 1762 | + .filter ( String :: isEmpty) |
| 1763 | + .count ()); |
| 1764 | +``` |
| 1765 | + |
| 1766 | +<div align="right"> |
| 1767 | + <b><a href="#">↥ back to top</a></b> |
| 1768 | +</div> |
| 1769 | + |
| 1770 | +## Q. How to display 10 random numbers in ascending order? |
| 1771 | + |
| 1772 | +```java |
| 1773 | +( new Random ()) |
| 1774 | + .ints () |
| 1775 | + .limit ( 10 ) |
| 1776 | + .sorted () |
| 1777 | + .forEach ( System . out :: println); |
| 1778 | +``` |
| 1779 | + |
| 1780 | +<div align="right"> |
| 1781 | + <b><a href="#">↥ back to top</a></b> |
| 1782 | +</div> |
| 1783 | + |
| 1784 | +## Q. How to find the maximum number in a set? |
| 1785 | + |
| 1786 | +```java |
| 1787 | +Stream |
| 1788 | + .of ( 5 , 3 , 4 , 55 , 2 ) |
| 1789 | + .mapToInt (a -> a) |
| 1790 | + .max () |
| 1791 | + .getAsInt (); // 55 |
| 1792 | +``` |
| 1793 | + |
| 1794 | +<div align="right"> |
| 1795 | + <b><a href="#">↥ back to top</a></b> |
| 1796 | +</div> |
| 1797 | + |
| 1798 | +## Q. How to find the minimum number in a set? |
| 1799 | + |
| 1800 | +```java |
| 1801 | +Stream |
| 1802 | + .of ( 5 , 3 , 4 , 55 , 2 ) |
| 1803 | + .mapToInt (a -> a) |
| 1804 | + .min () |
| 1805 | + .getAsInt (); // 2 |
| 1806 | +``` |
| 1807 | + |
| 1808 | +<div align="right"> |
| 1809 | + <b><a href="#">↥ back to top</a></b> |
| 1810 | +</div> |
| 1811 | + |
| 1812 | +## Q. How to get the sum of all numbers in a set? |
| 1813 | + |
| 1814 | +```java |
| 1815 | +Stream |
| 1816 | + .of( 5 , 3 , 4 , 55 , 2 ) |
| 1817 | + .mapToInt() |
| 1818 | + .sum(); // 69 |
| 1819 | +``` |
| 1820 | + |
| 1821 | +<div align="right"> |
| 1822 | + <b><a href="#">↥ back to top</a></b> |
| 1823 | +</div> |
| 1824 | + |
| 1825 | +## Q. How to get the average of all numbers? |
| 1826 | + |
| 1827 | +```java |
| 1828 | +Stream |
| 1829 | + .of ( 5 , 3 , 4 , 55 , 2 ) |
| 1830 | + .mapToInt (a -> a) |
| 1831 | + .average () |
| 1832 | + .getAsDouble (); // 13.8 |
| 1833 | +``` |
| 1834 | + |
| 1835 | +<div align="right"> |
| 1836 | + <b><a href="#">↥ back to top</a></b> |
| 1837 | +</div> |
| 1838 | +## Q. How to get current date using Date Time API from Java 8? |
| 1839 | + |
| 1840 | +```java |
| 1841 | +LocalDate as.now(); |
| 1842 | +``` |
| 1843 | + |
| 1844 | +<div align="right"> |
| 1845 | + <b><a href="#">↥ back to top</a></b> |
| 1846 | +</div> |
| 1847 | + |
| 1848 | +## Q. How to add 1 week, 1 month, 1 year, 10 years to the current date using the Date Time API? |
| 1849 | + |
| 1850 | +```java |
| 1851 | +LocalDate as.now ().plusWeeks ( 1 ); |
| 1852 | +LocalDate as.now ().plusMonths ( 1 ); |
| 1853 | +LocalDate as.now ().plusYears ( 1 ); |
| 1854 | +LocalDate as.now ().plus ( 1 , ChronoUnit.DECADES ); |
| 1855 | +``` |
| 1856 | + |
| 1857 | +<div align="right"> |
| 1858 | + <b><a href="#">↥ back to top</a></b> |
| 1859 | +</div> |
| 1860 | + |
| 1861 | +## Q. How to get the next Tuesday using the Date Time API? |
| 1862 | + |
| 1863 | +```java |
| 1864 | +LocalDate as.now().with( TemporalAdjusters.next ( DayOfWeek.TUESDAY )); |
| 1865 | +``` |
| 1866 | + |
| 1867 | +<div align="right"> |
| 1868 | + <b><a href="#">↥ back to top</a></b> |
| 1869 | +</div> |
| 1870 | + |
| 1871 | +## Q. How to get the current time accurate to milliseconds using the Date Time API? |
| 1872 | + |
| 1873 | +```java |
| 1874 | +new Date ().toInstant (); |
| 1875 | +``` |
| 1876 | + |
| 1877 | +<div align="right"> |
| 1878 | + <b><a href="#">↥ back to top</a></b> |
| 1879 | +</div> |
| 1880 | + |
| 1881 | +## Q. How to get the second Saturday of the current month using the Date Time API? |
| 1882 | + |
| 1883 | +```java |
| 1884 | +LocalDate |
| 1885 | + .of ( LocalDate.Now ().GetYear (), LocalDate.Now ().GetMonth (), 1 ) |
| 1886 | + .with ( TemporalAdjusters.nextOrSame ( DayOfWeek.SATURDAY )) |
| 1887 | + .with ( TemporalAdjusters.next ( DayOfWeek.SATURDAY )); |
| 1888 | +``` |
| 1889 | +<div align="right"> |
| 1890 | + <b><a href="#">↥ back to top</a></b> |
| 1891 | +</div> |
| 1892 | + |
| 1893 | +## Q. How to get the current time in local time accurate to milliseconds using the Date Time API? |
| 1894 | + |
| 1895 | +```java |
| 1896 | +LocalDateTime.ofInstant ( new Date().toInstant(), ZoneId.systemDefault()); |
| 1897 | +``` |
| 1898 | + |
| 1899 | +<div align="right"> |
| 1900 | + <b><a href="#">↥ back to top</a></b> |
| 1901 | +</div> |
| 1902 | + |
1723 | 1903 | #### Q. How to find if there is a sub array with sum equal to zero?
|
1724 | 1904 | #### Q. How to remove a given element from array in Java?
|
1725 | 1905 | #### Q. How to find trigonometric values of an angle in java?
|
|
0 commit comments