Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Feature/one liners stream2 #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions src/basicCodes/Java8Features.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.*;
import static java.util.stream.Collectors.*;

public class Java8Features {
public static void main(String args[]){
Expand Down Expand Up @@ -123,12 +123,12 @@ public static void main(String args[]){

// System.out.println( personList.stream().map(Person::getAge).reduce(0,Integer.sum()); not possible

// System.out.println( personList.stream().mapToInt(Person::getAge).sum());
//
// List<String> names2 = personList.stream().filter(person -> person.getAge() >30)
// .map(person -> person.getName().toUpperCase())
// .collect(toList());
//
System.out.println( personList.stream().mapToInt(Person::getAge).sum());

List<String> names2 = personList.stream().filter(person -> person.getAge() >30)
.map(person -> person.getName().toUpperCase())
.collect(toList());

Set<String> malesList = personList.stream()
.filter(person -> person.getAge() > 30)
.filter(person ->person.getGender() == Gender.MALE)
Expand All @@ -146,6 +146,24 @@ public static void main(String args[]){

System.out.println(personGrpMap);

System.out.println(personList.stream().max((Person1, Person2)-> Person1.getAge()>Person2.getAge() ? 1:-1));

System.out.println(personList.stream().min((Person1, Person2)-> Person1.getAge()>Person2.getAge() ? 1:-1));




String str = "abcdABCDabcd";
Map<Character, Integer> frequency =
str.chars()
.mapToObj(c -> (char)c)
.collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(c -> 1)));
System.out.println(frequency);

List<Integer> list1 = Arrays.asList(1,2,3,3,4,4,4,5);

System.out.println(list1.stream().collect(Collectors.groupingBy(Function.identity(), counting())));

}

}
Expand Down
4 changes: 2 additions & 2 deletions src/basicCodes/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ public class Person {
String name;
int age;

Gender gender;

@Override
public String toString() {
return "Person{" +
Expand All @@ -13,8 +15,6 @@ public String toString() {
'}';
}

Gender gender;

public Person(String name, Gender gender, int i) {
this.name=name;
this.gender = gender;
Expand Down
56 changes: 56 additions & 0 deletions src/basicCodes/Practice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package basicCodes;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Practice {
public static void main(String[] args)
{
// List<Integer> arrLst = Arrays.asList(10,15,8,49,25,98,98,32,15);
// List<String> strLst = Arrays.asList("Pune","Solapur","Nasik","Satara","Sangali","Kolhapur");
//
// System.out.println(strLst.stream().filter(s -> s.length() < 5).collect(Collectors.toList()));
//
// System.out.println(new HashSet<>(arrLst));
//
// List<Person> personlist = new ArrayList<>();
//
// personlist.add(new Person("Subhram", Gender.MALE, 26));
// personlist.add(new Person("Srabani", Gender.FEMALE, 56));
// personlist.add(new Person("Moumita", Gender.FEMALE, 36));
// personlist.add(new Person("Subhendu", Gender.MALE, 66));
// personlist.add(new Person("Rakesh", Gender.MALE, 26));
//
// System.out.println(personlist.stream().sorted(Comparator.comparing(Person::getName)).collect(Collectors.toList()));
//
// IntStream.rangeClosed(1,10).forEach(System.out::println);
//
// String str = "abcdABCDabcd";
// Map<Character, Integer> frequency =
// str.chars()
// .mapToObj(c -> (char)c)
// .collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(c -> 1)));
// System.out.println(frequency);

Map<Character, Integer> map = new HashMap<Character, Integer>();
String s = "aasjajikkk";
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
Integer val = map.get(c);
if (val != null) {
map.put(c, val + 1);
}
else {
map.put(c, 1);
}
}




}
}