BAEL-6837 Add example code for getting the first item from a Set
This commit is contained in:
		
							parent
							
								
									d4fec3ad45
								
							
						
					
					
						commit
						b5fef8d2b5
					
				| @ -0,0 +1,35 @@ | ||||
| package com.baeldung.firstitemfromset; | ||||
| 
 | ||||
| import static org.junit.Assert.assertEquals; | ||||
| 
 | ||||
| import java.util.Arrays; | ||||
| import java.util.Iterator; | ||||
| import java.util.LinkedHashSet; | ||||
| import java.util.Optional; | ||||
| import java.util.Set; | ||||
| 
 | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| public class GetFirstItemFromSetUnitTest { | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenASet_whenUsingIterator_thenRetrieveAnItem(){ | ||||
|         Set<Integer> set = new LinkedHashSet<>(Arrays.asList(1, 2, 3, 4, 5)); | ||||
|         Iterator iterator = set.iterator(); | ||||
|         if(iterator.hasNext()){ | ||||
|             int retrieved = (int) iterator.next(); | ||||
|             assertEquals(retrieved, 1); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenASet_whenUsingStreams_thenRetrieveAnItem(){ | ||||
|         Set<Integer> set = new LinkedHashSet<>(Arrays.asList(1, 2, 3, 4, 5)); | ||||
|         Optional<Integer> optional = set.stream().findFirst(); | ||||
|         if(optional.isPresent()){ | ||||
|             int retrieved = optional.get(); | ||||
|             assertEquals(retrieved, 1); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user