java mix 知识点

时间:2022-07-24
本文章向大家介绍java mix 知识点,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一、java后台接受web前台传递的数组参数

前台发送:

param=1,2

后台接收

@RequestParam(value = "param") String[] param

@RequestParam(value = "param") List<String> param

二、toString():

public static String reflectionToString(Object o){
    if(o == null) return StringUtils.EMPTY;
    StringBuilder toStr = new StringBuilder();
    if(o instanceof Collection){
        Iterator it = ((Collection) o).iterator();
        while (it.hasNext()){
            toStr.append(reflectionToString(it.next()));
            toStr.append("n");
        }
    }else if(o instanceof Map){
        Iterator<Map.Entry> mit = ((Map) o).entrySet().iterator();
        while (mit.hasNext()){
            Map.Entry entry = mit.next();
            String kv = entry.getKey() + ": " + reflectionToString(entry.getValue());
            toStr.append(kv);
            toStr.append(" ");
        }
    } else if(o instanceof Object[]){
        for (Object temp: (Object[])o){
            toStr.append(reflectionToString(temp));
        }
    }else if(o instanceof String){
        toStr.append(o);
    } else {
        toStr.append(ToStringBuilder.reflectionToString(o, ToStringStyle.SHORT_PREFIX_STYLE));
    }
    return toStr.toString();
}

三、TreeMap

  • 基于红黑树。
  • 非线程安全。
  • 同步使用:SortedMap m = Collections.synchronizedSortedMap(new TreeMap(...))

四、properties属性文件工具类

package xxx.business.utils;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by windwant on 2016/7/19.
 */
public class ConfigUtils {

    private static final Logger logger = LoggerFactory.getLogger(ConfigUtils.class);

    private static PropertiesConfiguration config = null;

    static {
        try {
            if (config == null) {
                config = new PropertiesConfiguration("config.properties");
                //自动重新加载
                config.setReloadingStrategy(new FileChangedReloadingStrategy());
                //自动保存
                config.setAutoSave(true);
            }
        } catch (ConfigurationException e) {
            logger.error("load config.properties error", e);
            throw new RuntimeException(e);
        }
    }

    private ConfigUtils(){}

    public static String get(String key) {
        if (config != null) {
            String value = config.getString(key);
            return value == null ? "" : value;
        }
        return "";
    }

    public static Integer getInteger(String key) {
        int result = -1;
        if (config != null) {
            try {
                result = Integer.parseInt(config.getString(key));
            } catch (NumberFormatException e) {
                result = -1;
            }
        }
        return result;
    }
}

五、SortedMap基本特性

  • 继承于Map。
  • 提供对key(自然排序顺序或者SortedMap创建时提供的Comparator)的全排序。
  • key必须实现Comparable接口,以便于进行相互比较。
  • 应用于对map的遍历(EntrySet、KeySet、Values)。
  • 对比SortedSet。
  • subMap(fromkey,tokey)、headMap(tokey)、tailMap(fromKey)