#Java - Code Snippets for '#Java.util.hashset' - 5 code snippet(s) found |
|
Sample 1. Get all elements greater than 2, sort them and then push them to a new set, using Lambda Expression | |
|
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Set the predicate or the condition for filtering the elements.
Predicate<Integer> moreThan2Pred = (p) -> (p > 1);
// Use Filter to refine the element set, sort to Sort and Collectors.toSet to get a set out of Stream.
intSet = intSet.stream().filter(moreThan2Pred).sorted().collect(Collectors.toSet());
System.out.println(intSet); // Prints [2, 3, 4]
|
|
Like Feedback lambda expression collections set hashset generics stream predicate filter sort java.util.hashset java.util.stream.Collectors |
|
|
Sample 2. Combine two Summaries and Generate a new Summary using Lambda Expression | |
|
// Populate a List using Set elements.
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
Set<Integer> intSet2 = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
intSet2.add(1);
intSet2.add(2);
intSet2.add(3);
intSet2.add(4);
// Use the stream and collectors to Summarize all Integer elements
IntSummaryStatistics summary = intSet.stream().collect(Collectors.summarizingInt(p->((Integer)p)));
summary.combine(intSet2.stream().collect(Collectors.summarizingInt(p->((Integer)p))));
System.out.println(summary); // Prints IntSummaryStatistics{count=8, sum=20, min=1, average=2.500000, max=4}
|
|
Like Feedback lambda expression collections set hashset generics stream collectors Collectors.summarizingInt summary.combine java.util.hashset java.util.stream.Collectors java.util.IntSummaryStatistics |
|
|
Sample 3. Write a Program for Graph Breadth First Traversal using Apache Commons MultiMap | |
|
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
public class Graph {
private static Multimap<Integer,Integer> adjacentDirectedNodesMap = ArrayListMultimap.create();
private static Set<Integer> alreadyVisited = new HashSet();
static{
adjacentDirectedNodesMap.put(1, 2);
adjacentDirectedNodesMap.put(1, 3);
adjacentDirectedNodesMap.put(1, 5);
adjacentDirectedNodesMap.put(2, 4);
adjacentDirectedNodesMap.put(4, 5);
}
public static void main(String[] args){
ArrayList visited = new ArrayList();
Integer startNode = 1;
displayAdjacentNodes(startNode);
}
private static void displayAdjacentNodes(Integer integer){
System.out.println(integer);
for(Map.Entry<Integer, Collection<Integer>> adjacentNodes: adjacentDirectedNodesMap.asMap().entrySet()){
for(Integer integer1:adjacentNodes.getValue()){
if(alreadyVisited.contains(integer1)){
continue;
}
alreadyVisited.add(integer1);
System.out.println(integer1);
}
}
}
}
|
|
Like Feedback graph traversal breadth first traversal |
|
|
Sample 4. Write a Program for Graph Depth First Traversal using Apache Commons MultiMap | |
|
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
public class Graph {
private static Multimap<Integer,Integer> adjacentDirectedNodesMap = ArrayListMultimap.create();
private static Set<Integer> alreadyVisited = new HashSet();
static{
adjacentDirectedNodesMap.put(1, 2);
adjacentDirectedNodesMap.put(1, 3);
adjacentDirectedNodesMap.put(1, 5);
adjacentDirectedNodesMap.put(2, 4);
adjacentDirectedNodesMap.put(4, 5);
}
public static void main(String[] args){
ArrayList visited = new ArrayList();
Integer startNode = 1;
displayAdjacentNodes(startNode);
}
private static void displayAdjacentNodes(Integer integer){
if(alreadyVisited.contains(integer)){
return;
}
alreadyVisited.add(integer);
System.out.println(integer);
for(Integer adjacentNodes: adjacentDirectedNodesMap.get(integer)){
displayAdjacentNodes(adjacentNodes);
}
}
}
|
|
Like Feedback graph traversal depth first algorithm |
|
|
|
Sample 5. Apache Hadoop Map Reduce Example | |
|
https://apache.googlesource.com/hadoop-common/+/trunk/hadoop-mapreduce-project/hadoop-mapreduce-examples/src/main/java/org/apache/hadoop/examples/Grep.java
|
|
Like Feedback |
|
|