-
This file contains 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
package com.tvidushi.DONE; | |
import java.util.Arrays; | |
import java.util.Comparator; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.function.Function; | |
import java.util.stream.Collectors; | |
public class Conversions { | |
public static void convertArrayToString() { | |
String[] cities = {"Delhi", "Bombay","Chennai","London","New York"}; | |
Arrays.stream(cities) | |
.reduce((x,y)->x+"|"+y) | |
.ifPresent(s->System.out.println(s)); | |
} | |
public static void createUserListFromMap(Map<Integer, String> map) { | |
List<Student> student = map | |
.entrySet() | |
.stream() | |
.map(e -> new Student(e.getKey(), e.getValue())) | |
.collect(Collectors.toList()); | |
student.forEach(l -> System.out.println("Id: "+ l.getId()+", Name: "+ l.getName())); | |
} | |
public static void createSortedUserNameListFromMap(Map<Integer, String> map) { | |
List<Student> student = map.entrySet() | |
.stream() | |
.sorted(Comparator.comparing(e -> e.getValue())) | |
.map(e -> new Student(e.getKey(), e.getValue())) | |
.collect(Collectors.toList()); | |
student.forEach(l -> System.out.println("Id: "+ l.getId()+", Name: "+ l.getName())); | |
} | |
public static Map<Integer, Student> populateHashMapFromUserList(List<Student> items) { | |
Map<String, Student> map = items.stream().collect(Collectors.toMap(Student::getId, Function.identity())); | |
} | |
public static void changeCaseOfELements(){ | |
List<String> alpha = Arrays.asList("surbhi", "ruchi", "anu", "sagarika"); | |
List upperALpha = alpha.stream().map(String::toUpperCase).collect(Collectors.toList()); | |
System.out.println(String.join(",",upperALpha)); | |
} | |
public static void main(String[] args) { | |
System.out.println("Converting Array to String"); | |
convertArrayToString(); | |
System.out.println("Creating list of Students by names from map"); | |
createUserListFromMap(new Student().createStudentDtls()); | |
System.out.println("Creating Sorted List of Students by names from map"); | |
createSortedUserNameListFromMap(new Student().createStudentDtls()) ; | |
System.out.println("Changing case of Array Elements :"); | |
System.out.println(); | |
changeCaseOfELements(); | |
} | |
} | |
class Student { | |
private int id; | |
private String name; | |
public Student(){ | |
} | |
public Student(int id, String name) { | |
this.id = id; | |
this.name = name; | |
} | |
public int getId() { | |
return id; | |
} | |
public String getName() { | |
return name; | |
} | |
public Map createStudentDtls(){ | |
Map<Integer, String> map = new HashMap<>(); | |
map.put(10, "Avinash"); | |
map.put(12, "Brijesh"); | |
map.put(9, "Sujoy"); | |
map.put(11, "Sagar"); | |
return map; | |
} | |
public List createStudentDtlslist(){ | |
return Arrays.asList(new Student(10,"Avinash"), | |
new Student(12,"Brijesh"), | |
new Student(9,"Sujoy"), | |
new Student(11,"Sagar")); | |
} | |
} | |
-
No comments:
Post a Comment