Share

Search Java Code Snippets


  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.





#Java - Code Snippets for '#Filter' - 12 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. Get count of elements greater than 1 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);

// Use Stream, Predicate and Filter to get the count of elements more than 1

System.out.println(intSet.stream().filter(moreThan2Pred).count()); // Prints 3

   Like      Feedback     lambda expression   collections   set  hashset  generics  stream   predicate   filter


 Sample 3. Filtering objects using google.common.base.Predicate

static Collection<Employee> employeesGreaterThan30(Collection<Employee> employees) {
return filter(employees, new Predicate<Employee>() {
@Override
public boolean apply(Employee employee) {
return employee.getAge() > 30;
}
});
}

   Like      Feedback     predicate  google.common.base.Predicate  filter objects   alternate for predicate before java 8  collections  google guava


 Sample 4. Code Sample / Example / Snippet of org.mortbay.jetty.servlet.FilterHolder

  protected void defineFilter(WebApplicationContext ctx, String name,

String classname, Map<String, String> parameters, String[] urls) {



WebApplicationHandler handler = ctx.getWebApplicationHandler();

FilterHolder holder = handler.defineFilter(name, classname);

if (parameters != null) {

for(Map.Entry<String, String> e : parameters.entrySet()) {

holder.setInitParameter(e.getKey(), e.getValue());

}

}



for (String url : urls) {

handler.addFilterPathMapping(url, name, Dispatcher.__ALL);

}

}


   Like      Feedback      org.mortbay.jetty.servlet.FilterHolder


Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
 Sample 5. Code Sample / Example / Snippet of javax.swing.GrayFilter

    public static Image createDisabledImage (Image i) {

GrayFilter filter = new GrayFilter(true, 50);

ImageProducer prod = new FilteredImageSource(i.getSource(), filter);

Image grayImage = Toolkit.getDefaultToolkit().createImage(prod);

return grayImage;

}


   Like      Feedback      javax.swing.GrayFilter


 Sample 6. Code Sample / Example / Snippet of org.apache.calcite.rel.logical.LogicalFilter

    public RelNode convert(RelNode rel) {

final LogicalFilter filter = (LogicalFilter) rel;

final RelTraitSet traitSet = filter.getTraitSet().replace(out);

return new MongoFilter(

rel.getCluster(),

traitSet,

convert(filter.getInput(), out),

filter.getCondition());

}


   Like      Feedback      org.apache.calcite.rel.logical.LogicalFilter


 Sample 7. Code Sample / Example / Snippet of javax.servlet.Filter

    public List<ArtifactObject> lrp(String filter) throws Exception {

Filter f = m_context.createFilter(filter);

List<ArtifactObject> rps = m_artifactRepository.getResourceProcessors();

List<ArtifactObject> res = new LinkedList<>();

for (ArtifactObject rp : rps) {

if (f.matchCase(rp.getDictionary())) {

res.add(rp);

}

}

return res;

}




   Like      Feedback      javax.servlet.Filter


 Sample 8. Code Sample / Example / Snippet of org.osgi.framework.Filter

    public List<ArtifactObject> lrp(String filter) throws Exception {

Filter f = m_context.createFilter(filter);

List<ArtifactObject> rps = m_artifactRepository.getResourceProcessors();

List<ArtifactObject> res = new LinkedList<>();

for (ArtifactObject rp : rps) {

if (f.matchCase(rp.getDictionary())) {

res.add(rp);

}

}

return res;

}




   Like      Feedback      org.osgi.framework.Filter


 Sample 9. Code Sample / Example / Snippet of org.apache.felix.framework.capabilityset.SimpleFilter

    private static boolean matchMandatoryAttrbute(String attrName, SimpleFilter sf)

{

if ((sf.getName() != null) && sf.getName().equals(attrName))

{

return true;

}

else if (sf.getOperation() == SimpleFilter.AND)

{

List list = (List) sf.getValue();

for (int i = 0; i < list.size(); i++)

{

SimpleFilter sf2 = (SimpleFilter) list.get(i);

if ((sf2.getName() != null)

&& sf2.getName().equals(attrName))

{

return true;

}

}

}

return false;

}


   Like      Feedback      org.apache.felix.framework.capabilityset.SimpleFilter


Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
 Sample 10. Get only files from a directory using FileFilter

File dir = new File("C:/Folder");

File[] files = dir.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return file.isFile();
}
});

   Like      Feedback     FileFilter


 Sample 11. Get only sub directories in a directory using FileFilter

File dir = new File("C:/Folder");

File[] subdir = dir.listFiles(new FileFilter() {
   @Override
   public boolean accept(File file) {
      return file.isDirectory();
   }
});

   Like      Feedback     FileFilter


 Sample 12. Write a Program that gets a set of numbers , filters out the non prime numbers , calculate the factorial of each prime number and then finds the average of all factorials using Lambda expressions

public class BuggyBread1 {
   
   public static void main(String args[]) {
      
      // 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);
      intSet.add(5);

      double averageOfNonPrimeFactorials = intSet.stream().filter(p->checkIfPrime(p)).collect(Collectors.averagingInt(p->calculateFactorial(p)));
      
      System.out.println(averageOfNonPrimeFactorials );
   }
   
   static private boolean checkIfPrime(int num){
      for(int count=2;count < num;count++){
         if(num % count == 0){
            return false;
         }
      }
      return true;
   }
   
   static private int calculateFactorial(int num){
      int factorial = 1;
      for(int count=num;count > 0;count--){
         factorial = factorial * count;
      }
      return factorial;
   }
}

   Like      Feedback     java 8  lambda expressions  lambda  filter  Collectors  factorial  prime number



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus