java基础编程

  java的类和常用编程模式还是要多练习,多手写java代码

  return new String(filecontent, encoding); 看懂这个意思了吗?第一次见这个构造函数吧,而String可以说是最常用的类了,但是这个构造方法却是第一次见。

  The caret ("^") is used in two different ways, one to signal the start of the text, one to negate a character match inside square brackets.

    菜鸟教程的正则表达式不错,有很多例子:http://www.runoob.com/java/java-regular-expressions.html

public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
        Map<K, V> result = new LinkedHashMap<>();
        Stream<Entry<K, V>> st = map.entrySet().stream(); // java8新引入的
        st.sorted(Comparator.comparing(e -> e.getValue())).forEach(e -> result.put(e.getKey(), e.getValue()));
        return result;
    } 按照map的value值排序,思路:指定value的排序规则,使用已有排序函数实现排序。

 map.entrySet():返回键值对集合,元素为Map.Entry<K,Value>,可以使用object.getKey()和getValue()进行遍历。

泛型方法泛型方法,是在调用方法的时候指明泛型的具体类型 。泛型类的定义非常简单,但是泛型方法就比较复杂了。

编译会报错!因为<T extends Comparable<T>>相当于<GregorianCalendar extends Comparable<GregorianCalendar>>
但是GregorianCalendar并没有实现Comparable<GregorianCalendar>而是实现的Comparable<Calendar>,这里不在限制范围之内所以会报错!
这样设计接口继承是为了避免代码冗余,所以这个格式就看着怪怪的。
  1. /**
  2. * 泛型方法的基本介绍
  3. * @param tClass 传入的泛型实参
  4. * @return T 返回值为T类型
  5. * 说明:
  6. * 1)public 与 返回值中间<T>非常重要,可以理解为声明此方法为泛型方法。
  7. * 2)只有声明了<T>的方法才是泛型方法,泛型类中的使用了泛型的成员方法并不是泛型方法。
  8. * 3)<T>表明该方法将使用泛型类型T,此时才可以在方法中使用泛型类型T。
  9. * 4)与泛型类的定义一样,此处T可以随便写为任意标识,常见的如T、E、K、V等形式的参数常用于表示泛型。
  10. */
  11. public <T> T genericMethod(Class<T> tClass)throws InstantiationException ,
  12. IllegalAccessException{
  13. T instance = tClass.newInstance();
  14. return instance;
  15. }
    Comparator接口的理解和使用:

为什么可以不实现 equals(Object obj) 函数呢? 因为任何类,默认都是已经实现了equals(Object obj)的。 Java中的一切类都是继承于java.lang.Object,在Object.java中实现了equals(Object obj)函数;所以,其它所有的类也相当于都实现了该函数。//这句话真的解答了我的疑惑,还有default method

// 使用 lambda 表达式以及函数操作(functional operation)  
players.forEach((player) -> System.out.print(player + "; "));  
   
// 在 Java 8 中使用双冒号操作符(double colon operator)  
players.forEach(System.out::println); 

//在阅读spring源码中,遇到这句话,理解一下是什么意思,重要的是读懂这些方法,见方法名就知道干什么的了。

this.setActiveProfiles(StringUtils.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(profiles)));

  

参考数据:<<effectivec java>>
   

原文地址:https://www.cnblogs.com/Robin008/p/9551481.html