1、字符串优化处理

  1.1 常量池的优化:当String对象拥有相同的值时,他们只引用常量池的同一个拷贝.

String a="123";
String b="123";
System.out.println(a.intern()==b.intern()); //true

  1.2 字符串截取导致内存泄露问题优化(包装类都有此问题)

    在使用substring方法时,在其外面使用new String()进行处理即可解决此问题(触发垃圾回收)

new String("要被截取的字符串".substring(0,3));

  1.3 对于字符串的拆分,尽量使用indexOf效率更高

//将字符串arrayStr通过split拆分后放入strings集合中
public
static List<String> toList(String arrayStr,String split,List<String> strings) { int index=arrayStr.indexOf(split); while (index!=-1) { strings.add(new String(arrayStr.substring(0,index))); arrayStr=new String(arrayStr.substring(index+1)); index=arrayStr.indexOf(split); } //判断最后一个是否是空 if (!arrayStr.trim().equals("")) { strings.add(arrayStr); } return strings; }

 2、集合优化

  2.1 主要用于在尾部添加的处理时,使用ArrayList的效率较高;

    在头部和中间插入数据时,使用LinkedList的效率较高;

    删除操作较多时,使用LinkedListde效率较高

  2.2 对于集合的迭代和遍历使用原始的for(int i=0;i<10;i++)的效率会更高

  2.3 使用集合是给定初始化大小,效率更高

  2.4 如果需要对集合中数据尽量使用treeSet,treeMap来实现

3、常用技巧

  3.1 对于工具类使用静态方法

  3.2 使用clone方法代替new新的对象

  3.3 对于io操作要使用带缓存的buffer包装类

  3.4 对用数组的拷贝使用Arrays.copyOf()方法

  3.5 使用布尔运算&&代替位运算&

  3.6 对用大量的循环使用展开循环更好

  3.7 对于多次使用的表达式进行提取为一个临时变量

  3.8 尽量少用switch语句,使用ifelse替代

  3.9 能使用位运算的尽量使用位运算(如*2,/4等操作)

  3.10 能使用局部变量的就不用使用全局变量,效率更高

  3.11 能不使用异常的尽量不用进行异常处理

  3.12 不要使用二维数组,效率很低,使用一维数组代替即可

4、bio,aio ,nio的比较

  java bio:同步并阻塞,一个连接一个线程,即客户端有连接请求时就启动一个线程进行处理,使用与连接数目小且固定的框架对服务器的资源要求不较高,并发环境中jdk1.4之前的唯一选择

  java nio:同步非阻塞,一个请求一个线程,即客户端发送的连接请求都会注册到多路复用器上,多路复用器轮询到连接有I/O请求时才启动一个线程进行处理,适用于连接数目多但连接时间短的架构,jdk1.4开始支持

  java nio:异步非阻塞,一个有效请求一个线程,即客户端的I/O请求都是由OS先完成了再通知服务器应用去启动线程进行处理,适用于连接数目多且连接时间长的架构,jdk1.7开始支持

//通过nio进行文件复制
public void copyFileByNio(String sourceFile,String targetFile) throws Exception {
        FileInputStream inputStream=new FileInputStream(new File(sourceFile));
        FileOutputStream outputStream=new FileOutputStream(new File(targetFile));
        //获取nio通道
        FileChannel readChannel=inputStream.getChannel();
        FileChannel writeChannel=outputStream.getChannel();
        ByteBuffer readBuffer=ByteBuffer.allocate(1024*8);
        //通过nio通道进行读写操作
        while (true) {
            readBuffer.clear();
            int read=readChannel.read(readBuffer);
            if (read==-1) {
                break;
            }
            readBuffer.flip();
            writeChannel.write(readBuffer);
        }
        //关闭通道
        readChannel.close();
        writeChannel.close();
}

//通过aio复制文件
public void copyFileByAio(String sourceFile,String targetFile) throws Exception {
        //获取aio异步通道
        AsynchronousFileChannel readChannel = AsynchronousFileChannel.open(Paths.get(sourceFile));
        OpenOption openOption=StandardOpenOption.WRITE;
        //获取aio异步通道
        AsynchronousFileChannel writeChannel=AsynchronousFileChannel.open(Paths.get(targetFile),openOption);
        
        ByteBuffer readBuffer=ByteBuffer.allocate(1024*8);
        //通过aio通道进行读写操作
        Future<Integer> result=readChannel.read(readBuffer, 0);
        while (!result.isDone()) {
            Thread.sleep(1);
        }
        readBuffer.flip();
        writeChannel.write(readBuffer, 0);
        //关闭通道
        readChannel.close();
        writeChannel.close();
}

 5、ByteBuffer的常用操作

  rewind()://它使限制保持不变,将位置设置为 0。

  reset()://将此缓冲区的位置重置为最近标记的位置。

  flip();//它将限制设置为当前位置,然后将位置设置为 0。

  clear();//它将限制设置为容量大小,将位置设置为 0。

  slice();//创建一个从当前位置到上限的子缓存区

     ByteBuffer buffer=ByteBuffer.allocate(15);
        System.out.println("上限:"+buffer.limit()+",容量:"+buffer.capacity()+",位置:"+buffer.position());
        for(int i=0;i<10;i++){
            buffer.put((byte)i);
        }
        System.out.println("上限:"+buffer.limit()+",容量:"+buffer.capacity()+",位置:"+buffer.position());
        //使缓冲区为重新读取已包含的数据做好准备:它使限制保持不变,将位置设置为 0。
        buffer.rewind();
        System.out.println("上限:"+buffer.limit()+",容量:"+buffer.capacity()+",位置:"+buffer.position());
        
        for(int i=0;i<10;i++){
            if (i==4) {
                //在此缓冲区的位置设置标记。 
                buffer.mark();
            }else if (i==6) {
                buffer.mark();
            }
            
            buffer.put((byte)i);
        }
        
        //将此缓冲区的位置重置为最近标记的位置。 
        buffer.reset();
        System.out.println("上限:"+buffer.limit()+",容量:"+buffer.capacity()+",位置:"+buffer.position());
        
        //使缓冲区为一系列新的通道写入或相对获取 操作做好准备:它将限制设置为当前位置,然后将位置设置为 0。 
        buffer.flip();
        System.out.println("上限:"+buffer.limit()+",容量:"+buffer.capacity()+",位置:"+buffer.position());
        
        //使缓冲区为一系列新的通道读取或相对放置 操作做好准备:它将限制设置为容量大小,将位置设置为 0。 
        buffer.clear();
        System.out.println("上限:"+buffer.limit()+",容量:"+buffer.capacity()+",位置:"+buffer.position());

     //创建一个从当前位置到上限的子缓存区
        ByteBuffer subBuffer=buffer.slice();
        System.out.println("上限:"+subBuffer.limit()+",容量:"+subBuffer.capacity()+",位置:"+subBuffer.position());

 6、将文件内容映射到ByteBuffer缓存中

public void loadFile(String fileName) throws Exception {
        RandomAccessFile file=new RandomAccessFile(fileName, "rw");
        long length=file.length();
        FileChannel channel=file.getChannel();
        //将文件内容映射到ByteBuffer缓存中
        MappedByteBuffer buffer=channel.map(MapMode.READ_WRITE, 0, file.length());
        //从缓存中读取文件的内容
        byte[] content=new byte[(int)length]; 
        buffer.get(content, 0, (int)length);
        System.out.println(new String(buffer.array(),"gb2312"));
        channel.close();
        file.close();
}
//聚集写
public void gatherWrite(String filename) throws Exception {
        ByteBuffer courseBuffer=ByteBuffer.wrap("buffer 聚集写操作".getBytes("gb2312"));
        ByteBuffer authBuffer=ByteBuffer.wrap("special 讲师".getBytes("gb2312"));
        ByteBuffer[] buffers=new ByteBuffer[]{courseBuffer,authBuffer};
        FileOutputStream outputStream=new FileOutputStream(new File(filename));
        FileChannel channel=outputStream.getChannel();
        channel.write(buffers);
        channel.close();
        outputStream.close();
}

//映射读
public void mapRead(String filename) throws Exception {
        FileInputStream inputStream=new FileInputStream(new File(filename));
        FileChannel channel=inputStream.getChannel();
        ByteBuffer b1=ByteBuffer.allocate(13);
        ByteBuffer b2=ByteBuffer.allocate(100);
        channel.read(new ByteBuffer[]{b1,b2});
        System.out.println(new String(b1.array(),"gb2312"));
        System.out.println(new String(b2.array(),"gb2312"));
        channel.close();
        inputStream.close();
}

 

posted on 2017-01-12 17:37  YL10000  阅读(155)  评论(0编辑  收藏  举报