- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 2617 Articles for Java
2K+ Views
To print common characters of two strings in alphabetical order, the code is as follows −Example Live Demoimport java.io.*; import java.util.*; public class Demo{ static void common_chars(String str_1, String str_2){ int[] array_1 = new int[26]; int[] array_2 = new int[26]; int str_len_1 = str_1.length(); int str_len_2 = str_2.length(); for (int i = 0 ; i < str_len_1 ; i++) array_1[str_1.charAt(i) - 'a'] += 1; for (int i = 0 ; i < str_len_2 ; i++) array_2[str_2.charAt(i) ... Read More
522 Views
To find the second most repeated word in a sequence in Java, the code is as follows −Example Live Demoimport java.util.*; public class Demo{ static String second_repeated(Vector my_seq){ HashMap my_map = new HashMap(my_seq.size()){ @Override public Integer get(Object key){ return containsKey(key) ? super.get(key) : 0; } }; for (int i = 0; i < my_seq.size(); i++) my_map.put(my_seq.get(i), my_map.get(my_seq.get(i))+1); int first_val = Integer.MIN_VALUE; int sec_val ... Read More
128 Views
To find the uncommon values concatenated from both the strings in Java, the code is as follows −Example Live Demoimport java.util.*; import java.lang.*; import java.io.*; public class Demo{ public static String concat_str(String str_1, String str_2){ String result = ""; int i; HashMap my_map = new HashMap(); for (i = 0; i < str_2.length(); i++) my_map.put(str_2.charAt(i), 1); for (i = 0; i < str_1.length(); i++) if (!my_map.containsKey(str_1.charAt(i))) result += str_1.charAt(i); else ... Read More
2K+ Views
To display Hostname and IP address in Java, the code is as follows −Example Live Demoimport java.net.*; public class Demo{ public static void main(String[] args){ try{ InetAddress my_address = InetAddress.getLocalHost(); System.out.println("The IP address is : " + my_address.getHostAddress()); System.out.println("The host name is : " + my_address.getHostName()); } catch (UnknownHostException e){ System.out.println( "Couldn't find the local address."); } } }OutputThe IP address is : 127.0.0.1 The host name is : jdoodleA class ... Read More
315 Views
To find the maximum and minimum element’s position in a list, the Java program is as follows −Example Live Demoimport java.util.*; import java.util.Arrays; import java.util.Collections; public class Demo{ public static int index_val(int my_arr[], int t){ if (my_arr == null){ return -1; } int len = my_arr.length; int i = 0; while (i < len){ if (my_arr[i] == t){ return i; } else { ... Read More
405 Views
To iterate over a Stream with Indices in Java 8, the code is as follows −Example Live Demoimport java.util.stream.IntStream; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; public class Demo{ public static void main(String[] args){ String[] my_array = { "T", "h", "i", "s", "s", "a", "m", "p", "l", "e" }; AtomicInteger my_index = new AtomicInteger(); System.out.println("The elements in the string array are :"); Arrays.stream(my_array).map(str -> my_index.getAndIncrement() + " -> " + str).forEach(System.out::println); } }OutputThe elements in the string array are : 0 -> T 1 -> h 2 -> i 3 ... Read More
887 Views
To print unique values from a List in Java, the code is as follows −Example Live Demoimport java.io.*; public class Demo{ static void distinct_vals(int my_arr[], int len){ for (int i = 0; i < len; i++){ int j; for (j = 0; j < i; j++) if (my_arr[i] == my_arr[j]) break; if (i == j) System.out.print( my_arr[i] + " "); } } public static void main ... Read More
497 Views
To find all close matches of input string from a list, the Java code is as follows −Example Live Demoimport java.io.*; import java.util.*; public class Demo{ static String string_encoding(String str){ HashMap my_map = new HashMap(); String result = ""; int i = 0; char ch; for (int j = 0; j < str.length(); j++) { ch = str.charAt(j); if (!my_map.containsKey(ch)) my_map.put(ch, i++); result += my_map.get(ch); } ... Read More
521 Views
To remove all duplicate words from a given sentence, the Java code is as follows −Example Live Demoimport java.util.Arrays; import java.util.stream.Collectors; public class Demo{ public static void main(String[] args){ String my_str = "This is a is sample a sample only."; my_str = Arrays.stream(my_str.split("\s+")).distinct().collect(Collectors.joining(" ")); System.out.println(my_str); } }OutputThis is a sample only.A class named Demo contains the main function. In this function, a String object is defined. It is split based on the spaces and only distinct words of the string are joined together using space as a separator and displayed ... Read More