- Convert a List of Strings to uppercase:
javaList<String> strings = Arrays.asList("apple", "banana", "cherry"); List<String> uppercaseStrings = strings.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
System.out.println(uppercaseStrings); // [APPLE, BANANA, CHERRY]
- Filter a List of integers to only include even numbers:
javaList<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> evenNumbers = numbers.stream()
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());
System.out.println(evenNumbers); // [2, 4, 6, 8, 10]
- Calculate the sum of a List of integers:
javaList<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
int sum = numbers.stream()
.reduce(0, Integer::sum);
System.out.println(sum); // 55
- Group a List of objects by a property:
javaList<Person> people = Arrays.asList(
new Person("Alice", 25),
new Person("Bob", 30),
new Person("Charlie", 35),
new Person("Alice", 40)
);
Map<String, List<Person>> peopleByFirstName = people.stream()
.collect(Collectors.groupingBy(Person::getFirstName));
System.out.println(peopleByFirstName);
// {Alice=[Person(firstName=Alice, age=25), Person(firstName=Alice, age=40)],
// Bob=[Person(firstName=Bob, age=30)],
// Charlie=[Person(firstName=Charlie, age=35)]}
- Sort a List of strings alphabetically:
javaList<String> strings = Arrays.asList("banana", "apple", "cherry");
List<String> sortedStrings = strings.stream()
.sorted()
.collect(Collectors.toList());
System.out.println(sortedStrings); // [apple, banana, cherry]
- Find the maximum value in a List of integers:
javaList<Integer> numbers = Arrays.asList(3, 1, 4, 1, 5, 9, 2, 6, 5, 3);
Optional<Integer> max = numbers.stream()
.max(Integer::compare);
System.out.println(max); // Optional[9]
- Find the first element of a List of strings that starts with a given letter:
javaList<String> strings = Arrays.asList("apple", "banana", "cherry", "apricot", "blueberry");
Optional<String> first = strings.stream()
.filter(s -> s.startsWith("a"))
.findFirst();
System.out.println(first); // Optional[apple]
- Map a List of objects to a List of their properties:
javaList<Person> people = Arrays.asList(
new Person("Alice", 25),
new Person("Bob", 30),
new Person("Charlie", 35)
);
List<String> firstNames = people.stream()
.map(Person::getFirstName)
.collect(Collectors.toList());
System.out.println(firstNames); // [Alice, Bob, Charlie]
Create optional get Item list
1if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { Optional<SectionOffer> optionalSectionOffer = sectionOffers.stream()
.filter(s -> s.getDiscipline().contains(binding.spinnerDiscipline.getSelectedItem().toString()))
.findFirst();
if (optionalSectionOffer.isPresent()) {
SectionOffer sec = optionalSectionOffer.get();
int secID= sec.getId();
Toast.makeText(AdminSettingAssignCourseActivity.this, ""+ secID, Toast.LENGTH_SHORT).show();
// do something with sec
} else {
// handle the case where no matching section offer was found
}
}
1if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { Optional<SectionOffer> optionalSectionOffer = sectionOffers.stream() |
This code uses findFirst() to find the first SectionOffer that matches the selected discipline, and then uses map() to get its id. If no matching SectionOffer is found, the code sets a default value of -1 (you can change this to whatever makes sense for your use case). Finally, the id is displayed in a toast message.
String store array list
String array list
Here, the map() method is used to extract the discipline property from each SectionOffer object in the list and return a stream of String objects. The collect() method is then used to collect this stream into a new ArrayList of String objects..
ArrayList<Map<String, String>> maplist: Declares an ArrayList to hold a collection of maps. Each map represents a JSON object with key-value pairs.String datajson: Represents the JSON string containing the data to be parsed and displayed in the Spinner.maplist = new Gson().fromJson(datajson, new TypeToken<ArrayList<Map<String, String>>>(){}.getType()): Utilizes Gson to parse the JSON string into an ArrayList of maps. TheTypeTokenclass is used to specify the desired type of the resulting object.if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N): Checks if the device's Android version is compatible with Java 8 features.ArrayList<String> dataList = maplist.stream().map(map -> map.get("discipline")).collect(Collectors.toCollection(ArrayList::new)): Applies Java 8 streams to themaplistto extract the value associated with the key"discipline"from each map. The resulting values are collected into a new ArrayList calleddataList.ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dataList): Creates an ArrayAdapter with thedataListas the data source. It uses the system-provided layoutandroid.R.layout.simple_list_item_1for each item in the Spinner.binding.spinnerTeacherDiscipline.setAdapter(adapter): Sets the created adapter to the Spinner, populating it with the extracted values.
By parsing the JSON string, extracting the desired values using Java 8 streams, and setting them as the data source for the ArrayAdapter, you can populate a Spinner with the extracted values from the JSON data.
- Combine two Lists of integers by adding their corresponding elements:
javaList<Integer> list1 = Arrays.asList(1, 2, 3