英文:
Filter , Sort and Find element in a ArrayList using Java8
问题
import java.util.*;
import java.util.stream.Collectors;
public class StreamFilterSortMain {
public static void main(String[] args) throws InterruptedException {
List<Guid> guids = new ArrayList<>();
guids.add(new Guid("guid1","YES",new Date()));
Thread.sleep(1000);
guids.add(new Guid("guid2","NO",new Date()));
Thread.sleep(1000);
guids.add(new Guid("guid3","YES",new Date()));
Thread.sleep(1000);
guids.add(new Guid("guid4","YES",new Date()));
// Java 8 approach
Guid pGuid = filterAndThenSortUsingJava8(guids);
System.out.println("Guid=" + pGuid);
}
private static Guid filterAndThenSortUsingJava8(List<Guid> guids) {
List<Guid> filteredGuids = guids.stream()
.filter(guid -> guid.getActive().equals("YES"))
.sorted(Comparator.comparing(Guid::getCreated).reversed())
.collect(Collectors.toList());
return filteredGuids.get(0);
}
}
Note: The above code demonstrates how to rewrite the filterAndThenSortUsingTraditonalJava()
method using Java 8 features such as streams and lambda expressions. The filter()
function is used to filter out GUIDs with the active status "YES", and the sorted()
function is used to sort the filtered GUIDs based on their creation dates in descending order. The result is collected into a list, and the first element (which has the latest created date) is returned as the processed GUID.
英文:
I have an List of GUIDs, from which I need to select the latest Active GUID. I wrote the code using traditional Java. I am trying to write with Java 8. Any suggestions ?
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Guid {
private String guid ;
private String active;
private Date created ;
}
public class StreamFilterSortMain {
public static void main(String[] args) throws InterruptedException {
List<Guid> guids = new ArrayList<>();
guids.add(new Guid("guid1","YES",new Date()));
Thread.sleep(1000);
guids.add(new Guid("guid2","NO",new Date()));
Thread.sleep(1000);
guids.add(new Guid("guid3","YES",new Date()));
Thread.sleep(1000);
guids.add(new Guid("guid4","YES",new Date()));
//Traditional Java
Guid pGuid = filterAndThenSortUsingTraditonalJava(guids);
System.out.println("Guid="+pGuid);
}
private static Guid filterAndThenSortUsingTraditonalJava(List<Guid> guids) {
Guid processedGuid = null;
//Use Iterator or Create New ArrayList
List<Guid> filteredGuids = new ArrayList<>();
for (Guid guid: guids) {
if(guid.getActive().equals("YES")){
filteredGuids.add(guid);
}
}
Collections.sort(filteredGuids, new Comparator<Guid>() {
@Override
public int compare(Guid o1, Guid o2) {
return o2.getCreated().compareTo(o1.getCreated());
}
});
processedGuid = filteredGuids.get(0);
return processedGuid;
}
}
How to rewrite the method using filterAndThenSortUsingTraditonalJava() using Java8 . I am pretty new to Java8. Really appreciate your suggestion.
答案1
得分: 1
希望这能满足您的需求
private static Guid filterAndThenSort(List<Guid> guids) {
return guids
.stream() //我们可以使用流或并行流
.filter(guid -> guid.getActive().equals("YES")) //如果active等于YES,则过滤guid
.sorted((guid1, guid2) -> guid2.getCreated().compareTo(guid1.getCreated())) //使用时间戳降序排序
.findFirst() //找到第一个元素
.orElse(null); //如果未找到元素,则返回null
}
英文:
Hope this satisfies your requirement
private static Guid filterAndThenSort(List<Guid> guids) {
return guids
.stream() //we can use stream or parallelstream
.filter( guid -> guid.getActive().equals("YES") ) // filters guid if active euals YES
.sorted((guid1, guid2) -> guid2.getCreated().compareTo(guid1.getCreated())) // sorting using timestamp descending
.findFirst() // findfirst element
.orElse(null); //if element not found return null
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论