Skip to content

Instantly share code, notes, and snippets.

@goish135
Created January 12, 2022 14:15
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save goish135/47487430346d67fc8fdcb4e0d6028ea8 to your computer and use it in GitHub Desktop.
Java Streams Example
package mycat;
public class Cat {
private String name;
private String weight;
// Getter
public String getName() {
return name;
}
// Setter
public void setName(String newName) {
this.name = newName;
}
// Getter
public String getWeight() {
return weight;
}
// Setter
public void setWeight(String newWeight) {
this.weight = newWeight;
}
}
package hello;
import mycat.Cat;
import java.util.ArrayList;
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello World!");
Cat catObj_1 = new Cat();
catObj_1.setName("Tom");
catObj_1.setWeight("6");
Cat catObj_2 = new Cat();
catObj_2.setName("Goish");
catObj_2.setWeight("7");
ArrayList<Cat> catArrayList = new ArrayList<>();
catArrayList.add(catObj_1);
catArrayList.add(catObj_2);
boolean existN = containsName(catArrayList,"John");
if(existN)
{
System.out.println("John exist!");
}
else
{
System.out.println("John not exist!");
}
boolean existY = containsName(catArrayList,"Tom");
if(existY)
{
System.out.println("Tom exist!");
}
else
{
System.out.println("Tom not exist!");
}
}
public static boolean containsName(ArrayList<Cat> list, String name)
{
return list.stream().filter(o -> o.getName().equals(name)).findFirst().isPresent();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment