Found 2617 Articles for Java

Java code to print common characters of two Strings in alphabetical order

AmitDiwan
Updated on 08-Jul-2020 10:20:46

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

Find the second most repeated word in a sequence in Java

AmitDiwan
Updated on 08-Jul-2020 10:20:18

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

Find the first repeated word in a string in Java

AmitDiwan
Updated on 08-Jul-2020 10:17:39

796 Views

To find the first repeated word in a string in Java, the code is as follows −Example Live Demoimport java.util.*; public class Demo{    static char repeat_first(char my_str[]){       HashSet my_hash = new HashSet();       for (int i=0; i

Find the uncommon values concatenated from both the strings in Java

AmitDiwan
Updated on 08-Jul-2020 10:16:20

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

Java program to display Hostname and IP address

AmitDiwan
Updated on 08-Jul-2020 10:14:28

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

Java program to find Maximum and minimum element’s position in a list

AmitDiwan
Updated on 07-Jul-2020 09:54:25

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

Program to Iterate over a Stream with Indices in Java 8

AmitDiwan
Updated on 07-Jul-2020 09:52:03

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

Java program to print unique values from a list

AmitDiwan
Updated on 07-Jul-2020 09:48:34

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

Java program to find all close matches of input string from a list

AmitDiwan
Updated on 07-Jul-2020 09:46:46

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

Java program to remove all duplicates words from a given sentence

AmitDiwan
Updated on 07-Jul-2020 09:44:10

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

Advertisements