0% found this document useful (0 votes)
2 views

all programming questions

The document contains various Java code snippets demonstrating string manipulation techniques, such as reversing strings, counting character occurrences, and finding non-repeating characters. It also includes examples of converting between data types, flattening nested lists, and finding maximum values in a list. Each code snippet is aimed at performing specific tasks related to string and list operations using Java streams and collections.

Uploaded by

Raghav Khajuria
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

all programming questions

The document contains various Java code snippets demonstrating string manipulation techniques, such as reversing strings, counting character occurrences, and finding non-repeating characters. It also includes examples of converting between data types, flattening nested lists, and finding maximum values in a list. Each code snippet is aimed at performing specific tasks related to string and list operations using Java streams and collections.

Uploaded by

Raghav Khajuria
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

String Reverse

String str="manjunatha";
String y= str.chars().mapToObj(x->String.valueOf((char)x)).reduce("",(s1,s2)-
>s2+s1)

2. String str = "Big black bug bit a big black dog on his big nose"

Map<String,Integer> map = Arrays.stream(str.split(" ")).collect


(Collectors.groupingBy(String::toLowercase,Collectors.counting());
syso(map)
//Function.indentity()
}

3.
String str = "Big black bug bit a big black dog on his big nose"
output: giB kcalb gub tib a gib kcalb god no sih gib eson

String z = Arrays.stream(str.split(" ")).map(y->new


StringBuilder(y).reverse ().toString())
.collect(Collectors.joining(" "));

4.convert Integer ArrayList to String ArrayList


.public static void main(String[] args) {
List<Integer> integerList = new ArrayList<>(Arrays.asList(1, 2, 3,4,5));

List<String> stringList = integerList.stream()


.map(String::valueOf)
.collect(Collectors.toList());

System.out.println("String List: " + stringList);


}

5.find Maximum character Occur in given String


String str = "Hello World";
char a= str.chars().mapToObj(c->(char)c).collect(Collectors.groupingBy
(Function.identity(),Collectors.counting()).entrySet().stream().max
(Map.Entry.comparingByValue()).map(Map.Entry::getKey).orElse('\0').get();

min(Map.Entry.comaparingByValue())

6.find first non repeat character in string

String str = "Hello Word";


str.chars().mapToObj(c-
>(char)c).collect(Collectors.groupingBy(Function.identity022222222222222222222(),Co
llectors.counting()).entrySet().stream().filter(entry->entry.getvalue()=1).map(
entry->entry.getKey()).findFirst().get().orElse('\0')

7.find first repeating character in string


filter(entry->entry.getValue()>1)

8.List<List<Integer>> nestedList = Arrays.asList(


Arrays.asList(1, 2, 3),
Arrays.asList(4, 5, 6),
Arrays.asList(7, 8, 9)
);
List<Integer> flattenedList = nestedList.stream()
.flatMap(List::stream)
.collect(Collectors.toList());

System.out.println(flattenedList); // Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

9.ArrayList<String> list = Arrays.asList(1,2,3,4,5);

ArrayList<String> reversed = new ArrayList<>(list.size());


IntStream.range(0, list.size())
.mapToObj(i -> list.get(list.size() - i - 1))
.forEach(reversed::add);

10.find second maximum


List<Integer> list = Arrays.asList(11,9,23,9,5);
int x= list.stream()
.distinct()
.sorted(Collections.reverseOrder())
.skip(1).findFirst().get();
System.out.println(x);
//limit(1)

You might also like