Java8示例程序

import java.util.Collections;import java.util.List;import java.util.ArrayList;import java.util.Comparator;

编程学习网为您整理以下代码实例,主要实现:Java8示例程序,希望可以帮到各位朋友。

import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.Comparator;

public class Java8Tester {

   public static voID main(String args[]) {

      List<String> names1 = new ArrayList<String>();
      names1.add("Mahesh ");
      names1.add("Suresh ");
      names1.add("Ramesh ");
      names1.add("Naresh ");
      names1.add("Kalpesh ");

      List<String> names2 = new ArrayList<String>();
      names2.add("Mahesh ");
      names2.add("Suresh ");
      names2.add("Ramesh ");
      names2.add("Naresh ");
      names2.add("Kalpesh ");

      Java8Tester tester = new Java8Tester();
      System.out.println("Sort using Java 7 Syntax: ");

      tester.sortUsingJava7(names1);
      System.out.println(names1);
      System.out.println("Sort using Java 8 Syntax: ");

      tester.sortUsingJava8(names2);
      System.out.println(names2);
   }

   //sort using java 7
   private voID sortUsingJava7(List<String> names) {   
      Collections.sort(names, new Comparator<String>() {
         @OverrIDe
         public int compare(String s1, String s2) {
            return s1.compareto(s2);
         }
      });
   }

   //sort using java 8
   private voID sortUsingJava8(List<String> names) {
      Collections.sort(names, (s1, s2) -> s1.compareto(s2));
   }
}
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐