Last active
April 11, 2021 06:31
-
-
Save ashikuzzaman-ar/ed05b2e3ec2d1ae1aedbf01e380c6d9b to your computer and use it in GitHub Desktop.
Question: you have an array of string and you have to sort them according to their size. Example: Input: ["ab", "a", "abc", "z", "abcd", "mp"]; Output: ["a", "z", "ab", "mp", "abc", "abcd"]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Question: you have an array of string and you have to sort them according to their size. | |
Example: | |
Input: ["ab", "a", "abc", "z", "abcd", "mp"]; | |
Output: ["a", "z", "ab", "mp", "abc", "abcd"] | |
*/ | |
import java.util.Comparator; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
public class Solution { | |
public static void main(String[] args) { | |
List<String> input = List.of("ab", "a", "abc", "z", "abcd", "mp"); | |
System.out.println(input.stream().sorted(Comparator.comparingInt(String::length)).collect(Collectors.toList())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment