Skip to content

Instantly share code, notes, and snippets.

@whizyrel
Created November 18, 2023 10:15
Show Gist options
  • Select an option

  • Save whizyrel/43dcf2174be0bb8aee84f3daff7a4cb6 to your computer and use it in GitHub Desktop.

Select an option

Save whizyrel/43dcf2174be0bb8aee84f3daff7a4cb6 to your computer and use it in GitHub Desktop.
package org.evryword.datastructures;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.IntStream;
public class MyList {
public static void main(String[] args) {
// INFO THIS IS ABOUT LISTS
/*
List
Array
LinkedList
Set
TreeSet
Map
TreeMap
ArrayList
Set
HashMap
Iteration and filtering using stream
*/
System.out.println("==== Start here ====");
/*
1. Convert this array = {Mary, Maryam, Mitchell, Mimi, Margaret, Emily, Monster} into a Java List.
• Filter above list to print only words with 5 characters and below
• Convert all the words to UpperCase and print out the content
• Filter and Collect only the words that start with ‘Ma’.
2. Given this array {1,2,3,4,5....20}
• Using streams, print out only the even numbers
• Sum up all the odd numbers in the filtered stream of odd numbers
• Collect the even numbers in a list named evenNumbers.
• Map the even numbers to be a square of the number itself
*/
String[] myNameArray = {"Mary", "Maryam", "Mitchell", "Mimi", "Margaret", "Emily", "Monster"};
// INFO Using the Stream API
filterUsingStream(myNameArray, 5);
System.out.println();
System.out.println("===============================");
System.out.println();
// INFO Using a for loop
convertArrayToArrayList(myNameArray, 5);
System.out.println();
System.out.println("===============================");
System.out.println();
int[] myIntegers = IntStream.rangeClosed(1, 20).toArray();
Arrays.stream(myIntegers)
.filter((integer) -> (integer % 2) == 0)
.forEach((nos) -> System.out.println("the even number: " + nos));
int sumOfOddNumbers = Arrays.stream(myIntegers)
.filter((integer) -> (integer % 2) != 0).sum();
System.out.println("The sum of odd numbers " + sumOfOddNumbers);
List<Integer> evenNumbers = Arrays.stream(Arrays.stream(myIntegers)
.filter((integer) -> (integer % 2) == 0).toArray()).boxed().toList();
HashMap<Integer, Integer> mapOfIntegerToSquare = new HashMap<>();
evenNumbers.forEach((item) -> {
mapOfIntegerToSquare.put(item, item * item);
});
System.out.println("My list of even Numbers => " + evenNumbers);
System.out.println("My map of integer to square of integers => " + mapOfIntegerToSquare);
}
public static void filterUsingStream(String[] myNameArray, int stringLength) {
List<String> myListOfNamesWith5Characters = Arrays.stream(myNameArray)
.filter((string) -> string.length() <= stringLength)
.toList();
myListOfNamesWith5Characters
.stream()
.map(String::toUpperCase)
.forEach((string) -> {
System.out.println("word in uppercase: " + string);
});
System.out.println("My List of Names with 5 characters and below " + myListOfNamesWith5Characters);
System.out.println("Words starting with \"Ma\"" + Arrays.stream(myNameArray)
.filter((string) -> string.startsWith("Ma"))
.toList());
}
public static void convertArrayToArrayList(String[] array, int stringLength) {
List<String> myNameList = new ArrayList<>();
for (int i = 0; i < array.length; i++) {
String currentItem = array[i];
if (currentItem.length() <= stringLength) {
myNameList.add(array[i]);
}
}
System.out.println("=== list of words with five characters and below => " + myNameList);
for (String list : myNameList) {
System.out.println("word in uppercase: " + list.toUpperCase());
}
List<String> myWordsStartingWithMa = new ArrayList<>();
for (String it : array) {
if (it.startsWith("Ma")) myWordsStartingWithMa.add(it);
}
System.out.println("Words starting with \"Ma\" " + myWordsStartingWithMa);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment