sneppets-java8

Java 8 : Find first element from a collection (List or Set)

This tutorial explains you how to find first element from collection like List or Set using Java 8 streams findFirst(), iterator() and listIterator methods.

Find first element from List or Set

The below example shows how to get first element of List or Set using different methods of List or Set.

package com.Sneppets.app;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import com.plan.domain.Skill;

public class FirstElementCollectionExample {
	
	public static void main (String[] args) {
		
		//List
		List<Skill> skillList = new ArrayList<>();		
		skillList.add(new Skill(1L, "Java"));
		skillList.add(new Skill(2L, "Python"));
		skillList.add(new Skill(3L, "Golang"));
		skillList.add(new Skill(4L, "C++"));
		
		//Set
		Set<Skill> skillSet = new LinkedHashSet<>();
		skillSet.add(new Skill(1L, "Java"));
		skillSet.add(new Skill(2L, "Python"));
		skillSet.add(new Skill(3L, "Golang"));
		skillSet.add(new Skill(4L, "C++"));
		
		//using Java 8 streams findFirst() method
		String firstSkillNameinList = null;
		if (skillList.stream().findFirst().isPresent()) {
			firstSkillNameinList = skillList.stream().findFirst().get().getName();
		}			
		System.out.println("Java 8: First skill name of Skill List : " + firstSkillNameinList);
		
		String firstSkillNameinSet = null;
		if(skillSet.stream().findFirst().isPresent()) {
			firstSkillNameinSet = skillSet.stream().findFirst().get().getName();
		}
		System.out.println("Java 8:First skill name of Skill Set : " + firstSkillNameinSet);
		
		//using list get(index) method
		String firstSkillNamel = null;
		if(!skillList.isEmpty()) {
			firstSkillNamel = skillList.get(0).getName();
		}		
		System.out.println("List get(index) method : First skill name of Skill List : " + firstSkillNamel);
		
		//using set iterator() method
		String firstSkillNames = null;
		if(!skillSet.isEmpty()) {
			firstSkillNames = skillSet.iterator().next().getName();
		}		
		System.out.println("Set iterator() method : First skill name of Skill Set : " + firstSkillNames);
		
		//using listIterator() method
		String firstSkillNamel1 = null;
		if(!skillList.isEmpty()) {
			firstSkillNamel1 = skillList.listIterator().next().getName();
		}
		System.out.println("List listIterator() method : First skill name of Skill List : " + firstSkillNamel1);


	}
}

Output

Java 8: First skill name of Skill List : Java
Java 8:First skill name of Skill Set : Java
List get(index) method : First skill name of Skill List : Java
Set iterator() method : First skill name of Skill Set : Java
List listIterator() method : First skill name of Skill List : Java

java.util.Stream

A stream provides an interface to a sequenced set of values of a specific element type. However, stream consumes data from the source such as Collection List or Set as shown in the example above. Note, Collection.stream() creates as sequential stream and Collection.parallelStream() creates a parallel stream.

We chose Collection.stream() in our example and use findFirst() method to identify first element of a Collection. This method returns an Optional first element of the sequential stream or an empty Optional if the stream is empty.

You can also use java.util.Optional.isPresent() to check if there is a first element present in the collection using Collection.stream.findFirst().isPresent() as shown above.

Further Reading

References

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments