Posts

Showing posts with the label List & Set

Java - How to count duplicated items in a List

In this section, we will show you h ow to count duplicated items in a List,using Collections.frequency and Map . Main.java import java.util.ArrayList ; import java.util.Collections ; import java.util.HashMap ; import java.util.HashSet ; import java.util.List ; import java.util.Map ; import java.util.Set ; import java.util.TreeMap ; public class Main { public static void main ( String [] args) { List < String > list = new ArrayList<>(); list .add( "Java" ); list .add( "Kotlin" ); list .add( "Python" ); list .add( "PHP" ); list .add( "Angular" ); list .add( "Java" ); list .add( "Kotlin" ); list .add( "React" ); list .add( "PHP" ); System . out .println( " \n Example 1 - Count 'Java' with frequency" ); System . out .println( "Java : " + Collections . frequency (...

Java - Convert colon-separated String to a List and List to a colon-separated String

In this section, we will show you h ow to c onvert colon-separated String to a List and  vice versa . 1. Colon-separated String to List Main.java import java.util.Arrays ; import java.util.List ; public class Main { public static void main ( String [] args) { String str = "India: USA: Japan: Russia" ; //Remove whitespace and split by colon List < String > result = Arrays . asList ( str .split( " \\ s*: \\ s* " )); System . out .println( result ); } } Console Output: [India, USA, Japan, Russia] 2. List to Colon-separated String Main.java import java.util.Arrays ; import java.util.List ; public class Main { public static void main ( String [] args) { List < String > list = Arrays . asList ( "India" , "USA" , "Japan" , "Russia" ); String result = String . join ( ":" , list ); System . out .println( result ); } } Console O...

Java - Convert hyphen-separated String to a List and List to a hyphen-separated String

In this section, we will show you h ow to c onvert hyphen-separated String to a List and  vice versa . 1. Hyphen-separated String to List Main.java import java.util.Arrays ; import java.util.List ; public class Main { public static void main ( String [] args) { String str = "India- USA- Japan- Russia" ; //Remove whitespace and split by hyphen List < String > result = Arrays . asList ( str .split( " \\ s*- \\ s* " )); System . out .println( result ); } } Console Output: [India, USA, Japan, Russia] 2. List to Hyphen-separated String Main.java import java.util.Arrays ; import java.util.List ; public class Main { public static void main ( String [] args) { List < String > list = Arrays . asList ( "India" , "USA" , "Japan" , "Russia" ); String result = String . join ( "-" , list ); System . out .println( result ); } } Conso...

Java - Convert comma-separated String to a List and List to a comma-separated String

In this section, we will show you h ow to c onvert comma-separated String to a List and  vice versa . 1. Comma-separated String to List Main.java import java.util.Arrays ; import java.util.List ; public class Main { public static void main ( String [] args) { String str = "India,USA,Japan,Russia" ; //Remove whitespace and split by comma List < String > result = Arrays . asList ( str .split( " \\ s*, \\ s* " )); System . out .println( result ); } } Console Output: [India, USA, Japan, Russia] 2. List to Comma-separated String Main.java import java.util.Arrays ; import java.util.List ; public class Main { public static void main ( String [] args) { List < String > list = Arrays . asList ( "India" , "USA" , "Japan" , "Russia" ); String result = String . join ( "," , list ); System . out .println( result ); } } Console Outp...

How to search a String in a List in Java?

In this section, we will show you h ow to search a string in a List in Java. We can use  .contains() , . startsWith() or .matches() to search for a string in ArrayList. Java 8 Example import java.util.ArrayList ; import java.util.List ; import java.util.stream.Collectors ; public class Main { public static void main ( String [] args) { List < String > list = new ArrayList<>(); list .add( "India" ); list .add( "USA" ); list .add( "Japan" ); list .add( "Brazil" ); list .add( "Israel" ); //.contains example List < String > result1 = list .stream() .filter(x -> x.contains( "India" )) .collect( Collectors . toList ()); System . out .println( result1 ); //.startsWith example List < String > result2 = list .stream() .filter(x -> x.st...

Java 8 Streams - Finding largest & smallest element in an Array, List & Set

Example 1: Finding the largest element in an array import java.util.Arrays; /* Java Program to find the largest number from an Array. */ public class DriverClass { public static void main( String [] args) { int [] numbers = { 2 , 6 , 7 , 9 , 5 , 155 , 66 , 99 }; int largestNumber = Arrays.stream(numbers). max().getAsInt(); System.out.println( "Largest Number= " + largestNumber); } } Output: Largest Number= 155 Example 2: Finding the largest element in a List import java.util.Arrays; import java.util.Comparator; import java.util.List; /* Java Program to find the largest number from a List. */ public class DriverClass { public static void main( String [] args) { List < Integer > numbers = Arrays. asList( 2 , 6 , 7 , 9 , 5 , 155 , 66 , 99 ); int largestNumber = numbers.stream(). max(Comparator.comparing ...