java用法总结

  1. 计时
long startTime = System.nanoTime();
solution.process(inputFile);
long endTime = System.nanoTime();
long totalTime = (endTime - startTime) / 1000; // 单位 us
  1. 按行读文件
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(inputPath)), "UTF-8"));
String lineTxt;
while ((lineTxt = br.readLine()) != null) {
// do something
}
  1. List使用
List<List<String>> res = new ArrayList<>();

// 添加元素
List<String> tmp = new ArrayList<>();
tmp.add("1");
tmp.add("2");
res.add(tmp); // 获取指定位置元素
res.get(1); // 获取长度
Integer len = res.size(); // 遍历
for (List<String> r : res) {
// do something
}
  1. Map使用
Map<String, Integer> m = new HashMap<>();

// 插入
m.put(id1, newIndex); // 取出
Integer index1 = m.get(id1); // 如果不存在key则会返回null // 判断是否存在key
if (id2index.containsKey()){
// do something
} // 遍历
for(Map.Entry<String, Integer> entry: m.entrySet()){
String key = entry.getKey();
Integer value = entry.getValue();
}
  1. Set使用
Set<String> s = new HashSet<>();

// 添加元素
s.add(id1); // 判断是否存在key
boolean b = s.contains(key);
  1. String与Integer转换
// String转Integer
Integer i = Integer.valueOf("123"); // Integer转String
String s = String.valueOf(123);
  1. 数组
int mapSize = 2000000;
int[] map = new int[mapSize];
map[0] = 100;
int i1 = map[0];

加速点

  1. 切分字符串“0001A,0001B”
// 速度较快, 百万量级下速度加快1s
String id1 = lineTxt.substring(0, 5);
String id2 = lineTxt.substring(6); // 速度较慢
String[] twoId = ids.split(",");
String id1 = twoId[0];
String id2 = twoId[1];
  1. 提前分配空间
// 较快
Map<String, Integer> id2index = new HashMap<>(1500000);
List<List<String>> res = new ArrayList<>(1500000); // 较慢
Map<String, Integer> id2index = new HashMap<>();
List<List<String>> res = new ArrayList<>();
  1. Map取值直接取,不必先判断是否存在key
// 较快
Integer index1 = id2index.get(id1);
Integer index2 = id2index.get(id2);
if (index1 != null && index2 != null){} // 较慢
if (id2index.containsKey(id1) && id2index.containsKey(id2)){
Integer index1 = id2index.get(id1);
Integer index2 = id2index.get(id2);
}
  1. 读文件用字节读取 百万量级可从1.5s加速到0.4s左右
FileInputStream is;
static int bufferSize = 65536;
static byte buffer[] = new byte[bufferSize];
int pos = bufferSize; is = new FileInputStream(inputPath);
while(true) {
bufferSize = is.read(buffer, 0, bufferSize);
if(bufferSize == -1) {
return -1; //读完了
}
char c = (char) buffer[0];
}

【java】java基本用法记录的更多相关文章

  1. Java Spring AOP用法

    Java Spring AOP用法 Spring AOP Java web 环境搭建 Java web 项目搭建 Java Spring IOC用法 spring提供了两个核心功能,一个是IoC(控制 ...

  2. Java自学基础用法

    在慕课上面简单学习了一下java语言的用法 简单的用法总结记录一下. 代码(学习输入,输出): package hello; import java.util.Scanner; public clas ...

  3. JAVA的continue用法

    JAVA的continue用法: public class test{ public static void main(String [] args){  for(int i=0;i<=10;i ...

  4. Java Spring IOC用法

    Java Spring IOC用法 Spring IoC 在前两篇文章中,我们讲了java web环境搭建 和 java web项目搭建,现在看下spring ioc在java中的运用,开发工具为In ...

  5. 四种Java线程池用法解析

    本文为大家分析四种Java线程池用法,供大家参考,具体内容如下 http://www.jb51.net/article/81843.htm 1.new Thread的弊端 执行一个异步任务你还只是如下 ...

  6. Java学习-007-Log4J 日志记录配置文件详解及实例源代码

    此文主要讲述在初学 Java 时,常用的 Log4J 日志记录配置文件详解及实例源代码整理.希望能对初学 Java 编程的亲们有所帮助.若有不足之处,敬请大神指正,不胜感激!源代码测试通过日期为:20 ...

  7. JAVA中ArrayList用法

    JAVA中ArrayList用法 2011-07-20 15:02:03|  分类: 计算机专业 |  标签:java  arraylist用法  |举报|字号 订阅     Java学习过程中做题时 ...

  8. JAVA 中LinkedHashMap要点记录

    JAVA 中LinkedHashMap要点记录 构造函数中可能出现的几个参数说明如下: 1.initialCapacity 初始容量大小,使用无参构造方法时,此值默认是16 2.loadFactor ...

  9. this在java中的用法

    this在java中的用法 1.使用this关键字引用成员变量 作用:解决成员变量与参数或局部变量命名冲突的问题 public class Dog { String name; public Dog( ...

  10. java assert的用法简介【转】

    assert的基本用法 assertion(断言)在软件开发中是一种常用的调试方式,很多开发语言中都支持这种机制,如C,C++和Eiffel等,但是支持的形式不尽相同,有的是通过语言本身.有的是通过库 ...

随机推荐

  1. java UTC时间格式化

    import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import ja ...

  2. NSParagraphStyle 的属性

    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 20)]; label.font = [UIFont sys ...

  3. 「洛谷3338」「ZJOI2014」力【FFT】

    题目链接 [BZOJ] [洛谷] 题解 首先我们需要对这个式子进行化简,否则对着这么大一坨东西只能暴力... \[F_i=\sum_{j<i} \frac{q_iq_j}{(i-j)^2}-\s ...

  4. Linux-#!/bin/sh理解

    #!是一个特殊符号,/bin/sh是用来解释该脚本的的shell路径 #!/bin/sh指该脚本使用/bin/sh来执行 sh只是其中一种解释方式,通过如下命令可以查到支持的shell解释方式: xx ...

  5. vue中的computed(计算属性)和watch(监听属性)的特点,以及深度监听

    //计算属性是根据data中已有的属性,计算得到一个新的属性, <div>全名:{{fullName}}</div> 创建计算属性通过computed关键字,它是一个对象 计算 ...

  6. django 3.post接口开发

    如果有了一个项目,还想创建一个项目,那么就是进入项目的路径下,运行命令,比如: cd /Users/newcomer/PycharmProjects/djangoProject python3 man ...

  7. Django 中使用ImgFiled 和FileFiled

    1.使用ImgFiled class ImageField(upload_to=None, height_field=None, width_field=None, max_length=100, * ...

  8. python-tqdm进度条

    通过tqdm库可以打造自己的进度条使用 1. pip安装 pip install tqdm --trusted-host pypi.douban.com 2.使用 from time import s ...

  9. Python基础和原反补码及表达式

    基础语法 1.注释 “#”标注的文件 2.数字 整数,不区分long和int 进制0xa.0o10.0b10 bool,2个值True.False 浮点数 1.2.3.1314.-0.12.1.46e ...

  10. MarkDown里面的Emoji表情

    我才发现MarkDown里面可以使用一些Emoji表情,好玩,以后写博客的趣味性大大增加 想看全部的就去这里找https://www.webfx.com/tools/emoji-cheat-sheet ...