Created
January 12, 2022 14:15
Star
You must be signed in to star a gist
Java Streams Example
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 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; | |
} | |
} |
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 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