搜索词>>Java指纹图片 耗时0.1070
  • Java通过sourceafis比对指纹图片的相似度判断指纹

    Java通过sourceafis比对指纹图片的相似度判断指纹,sourceafis,Java指纹图片<h2>什么是SourceAFIS?</h2> <p>SourceAFIS是一种指纹识别引擎,可以获取一对人类的指纹图像并返回其相似性分数。 它可以进行1:1的比较以及高效的1:N搜索。 这是SourceAFIS算法的Java实现</p> <h2>1.maven环境依赖引入</h2> <pre> <code class="language-xml"> <!-- https://mvnrepository.com/artifact/com.machinezoo.sourceafis/sourceafis --> <dependency> <groupId>com.machinezoo.sourceafis</groupId> <artifactId>sourceafis</artifactId> <version>2.0.11</version> </dependency></code></pre> 不用maven的可以通过上方注释的地址去下载,注意依赖gson.jar <h2>2.Java通过sourceafis比对指纹图片</h2> <pre> <code class="language-java">package com.example.fingerprint; import java.nio.file.Files; import java.nio.file.Paths; import com.machinezoo.sourceafis.FingerprintMatcher; import com.machinezoo.sourceafis.FingerprintTemplate; /** * 通过三方库实现指纹识别对比 * * @author xq * */ public class Test { public static void main(String[] args) { try { //首先是读取两张对比的指纹图片,图片必须是白色背景,指纹为黑色 byte[] probeImage = Files.readAllBytes(Paths.get("d://fp//da1.png")); byte[] candidateImage = Files.readAllBytes(Paths.get("d://fp//da2.png")); FingerprintTemplate probe = new FingerprintTemplate(probeImage); // 由于直接从二进制中生成指纹模板非常消耗性能,推荐第一次使用后序列话成JSON,内部提供方法。再通过json生成模板 // String jsonTemplete=probe.json(); // probe=new FingerprintTemplate(jsonTemplete); FingerprintTemplate candidate = new FingerprintTemplate(candidateImage); FingerprintMatcher matcher = new FingerprintMatcher(probe); double score = matcher.match(candidate); System.out.println("匹配得分:" + score); boolean match = score >= 40; System.out.println("是否匹配:" + match); } catch (Exception e) { e.printStackTrace(); } } } </code></pre> <h2>3.1:N操作</h2> 假设我们要比较一下刚刚从指纹读取器读取的探针指纹与已经存储在数据库中的多个候选指纹。 数据库通常是磁盘,但是指纹数据库必须是内存,因为探针指纹必须与每个候选指纹进行比较。<br /> 以1:N匹配,返回匹配得分已经不够了。 我们需要识别指纹匹配的用户,也许是通过描述用户的类来识别。 <pre> <code class="language-java">public class UserDetails { int id; String name; FingerprintTemplate template; }</code></pre> 我们现在可以定义一种方法,它可以获取探针指纹和候选指纹列表,并返回最佳匹配,如果不匹配则返回null。 <pre> <code class="language-java">UserDetails find(FingerprintTemplate probe, Iterable<UserDetails> candidates) { FingerprintMatcher matcher = new FingerprintMatcher(probe); UserDetails bestCandidate = null; double bestScore = 0; for (UserDetails candidate : candidates) { double score = matcher.match(candidate.template); if (score > bestScore) { bestScore = score; bestCandidate = candidate; } } double threshold = 40; return bestScore >= threshold ? bestCandidate : null; }</code></pre> 构造FingerprintMatcher只需要一次,因为这是一个昂贵的操作。 FingerprintMatcher构建内存数据结构,加快匹配。 个人电话匹配方法相对较快。<br /> <br /> 您可能会想知道为什么SourceAFIS不会在API中提供这样的搜索方法。 除了更容易将模板与应用程序定义的用户身份相关联,保持应用程序侧的搜索循环允许应用程序自定义循环。 此类定制的示例包括:<br /> <br /> 并行策略,<br /> 人口过滤,例如只搜索男性,<br /> 手指位置过滤,例如将右拇指仅匹配到右拇指,<br /> 多指搜索,例如要求左索引和右食指匹配,以及<br /> 多视图搜索,为每个用户保留同一个手指的多个模板,以提高识别率。 <h2>4.除了匹配器比较耗资源通过二进制流创建指纹模板其实也很消耗资源,如何缓存?</h2> 每次应用程序重新启动时,从原始指纹图像重新创建数据库中的所有指纹模板是不合理的。 因此,SourceAFIS提供了缓存指纹模板的方法。 <pre> <code class="language-java">byte[] image = Files.readAllBytes(Paths.get("fingerprint.jpeg")); FingerprintTemplate template = new FingerprintTemplate(image); String json = template.json();</code></pre> 模板的JSON表示可以存储在数据库中,以加快应用程序的重新启动。 当应用程序启动时,它反序列化JSON模板,而不是从指纹图像中重新创建它们。 <pre> <code class="language-java">FingerprintTemplate template = new FingerprintTemplate(json);</code></pre> JSON模板不能代替指纹图像。 它们被绑定到特定的SourceAFIS版本。 为了允许SourceAFIS升级,应用程序应始终存储原始指纹图像,并将JSON模板视为临时缓存。 <h2> </h2>
  • Java 多线程编程自旋锁详解

    本文将讲述什么是自旋锁?自旋锁的使用场景,什么情况适合使用自旋锁?Java 怎么使用自旋锁?一、什么是自旋锁?计算机系统资源总是有限的,有些资源需要互斥访问,因此就有了锁机制,只有获得锁的线程才能访问资源。锁保证了每次只有一个线程可以访问资源。当线程申请一个已经被其他线程占用的锁,就会出现两种情况。一种是没有获得锁的线程会阻塞自己,等到锁被释放后再被唤起,这就是互斥锁;一种是没有获得锁的线程一直循环在那里看是否该锁的保持者已经释放了锁,这就是自旋锁。二、自旋锁可能引起的问题1.过多占据CPU时间:如果锁的当前持有者长时间不释放该锁,那么等待者将长时间的占据cpu时间片,导致CPU资源的浪费,因此可以设定一个时间,当锁持有者超过这个时间不释放锁时,等待者会放弃CPU时间片阻塞;2.死锁问题:试想一下,有一个线程连续两次试图获得自旋锁(比如在递归程序中),第一次这个线程获得了该锁,当第二次试图加锁的时候,检测到锁已被占用(其实是被自己占用),那么这时,线程会一直等待自己释放该锁,而不能继续执行,这样就引起了死锁。因此递归程序使用自旋锁应该遵循以下原则:递归程序决不能在持有自旋锁时调用它自己,也决不能在递归调用时试图获得相同的自旋锁。三、JAVA 多线程编程中一种自旋锁使用案例import java.util.concurrent.atomic.AtomicReference;public class SpinLock {    //java中原子(CAS)操作    AtomicReference<Thread> owner = new AtomicReference<Thread>();//持有自旋锁的线程对象    private int count;    public void lock() {        Thread cur = Thread.currentThread();        //lock函数将owner设置为当前线程,并且预测原来的值为空。unlock函数将owner设置为null,并且预测值为当前线程。当有第二个线程调用lock操作时由于owner值不为空,导致循环        //一直被执行,直至第一个线程调用unlock函数将owner设置为null,第二个线程才能进入临界区。        while (!owner.compareAndSet(null, cur)) {        }    }    public void unLock() {        Thread cur = Thread.currentThread();        owner.compareAndSet(cur, null);    }}上述代码说明:调用lock方法时,如果owner当前值为null,说明自旋锁还没有被占用,将owner设置为currentThread,并进行锁定。调用lock方法时,如果owner当前值不为null,说明自旋锁已经被其他线程占用,当前线程就会在while中继续循环检测。调用unlock方法时,会将owner置为空,相当于释放自旋锁。public class TestThread extends Thread {    static int sum;    private SpinLock lock;    public TestThread(SpinLock lock) {        this.lock = lock;    }    @Override    public void run() {        this.lock.lock();        sum++;        System.out.println("当前值:=====>"+sum);        this.lock.unLock();    }    public static void main(String[] args) throws InterruptedException {        SpinLock lock = new SpinLock();        for (int i = 0; i < 100; i++) {            TestThread test = new TestThread(lock);            Thread t = new Thread(test);            t.start();        }        Thread.currentThread().sleep(1000);        System.out.println(sum);    }}上述代码执行的输出结果一定是顺序的1-100输出。四、名词解释java CAS原子操作:Compare And Set 在java.util.concurrent.atomic包下很多类都具有compareAndSet方法。五、自旋锁的使用场景,什么时候适合使用自旋锁由于自旋锁只是在当前线程不停地执行循环体,不进行线程状态的切换,因此响应速度更快。但当线程数不停增加时,性能下降明显,因为每个线程都需要占用CPU时间。如果线程竞争不激烈,并且保持锁的时间很短,则适合使用自旋锁。
  • Java 类加载机制

    Java 类加载机制<h2>1.Java类加载流程图</h2> <img alt="类加载流程图" class="img-thumbnail" src="/assist/images/blog/a5fad6dfbb004e239876373a08e30a0c.gif" /> <h2>2.类的初始化</h2> 类什么时候才被初始化: <p><span style="color:#16a085">1)创建类的实例,也就是new一个对象<br /> 2)访问某个类或接口的静态变量,或者对该静态变量赋值<br /> 3)调用类的静态方法<br /> 4)反射(Class.forName("com.lyj.load"))<br /> 5)初始化一个类的子类(会首先初始化子类的父类)<br /> 6)JVM启动时标明的启动类,即文件名和类名相同的那个类</span></p> 只有这6中情况才会导致类的类的初始化。 <p>假如这个类存在直接父类,并且这个类还没有被初始化(注意:在一个类加载器中,类只能初始化一次),那就初始化直接的父类(不适用于接口)<br /> 加入类中存在初始化语句(如static变量和static块),那就依次执行这些初始化语句。</p> <h2>3.类的加载</h2>   类的加载指的是将类的.class文件中的二进制数据读入到内存中,将其放在运行时数据区的方法区内,然后在堆区创建一个这个类的Java.lang.Class对象,用来封装类在方法区类的对象。<br /> <img alt="" class="img-thumbnail" src="/assist/images/blog/77a455e73f0548808e25afb18c0d3828.gif" /><br /> 类加载器:<br /> <img alt="类加载器:" class="img-thumbnail" src="/assist/images/blog/35a17133b1c743dd80041c967d51a7ad.gif" /><br />   <pre> <code class="language-java">import java.net.URL; public class Main { public static void main(String[] args) { URL[] urls=sun.misc.Launcher.getBootstrapClassPath().getURLs(); for (int i = 0; i < urls.length; i++) { System.out.println(urls[i].toExternalForm()); } } }</code></pre> <pre> <code>file:/D:/Program%20Files/Java/jdk1.8.0_102/jre/lib/resources.jar file:/D:/Program%20Files/Java/jdk1.8.0_102/jre/lib/rt.jar file:/D:/Program%20Files/Java/jdk1.8.0_102/jre/lib/sunrsasign.jar file:/D:/Program%20Files/Java/jdk1.8.0_102/jre/lib/jsse.jar file:/D:/Program%20Files/Java/jdk1.8.0_102/jre/lib/jce.jar file:/D:/Program%20Files/Java/jdk1.8.0_102/jre/lib/charsets.jar file:/D:/Program%20Files/Java/jdk1.8.0_102/jre/lib/jfr.jar file:/D:/Program%20Files/Java/jdk1.8.0_102/jre/classes</code></pre> <h2>4.Java类加载机制</h2> <p>类装载器就是寻找类的字节码文件,并构造出类在JVM内部表示的对象组件。在Java中,类装载器把一个类装入JVM中,要经过以下步骤:<br /> (1) 装载:查找和导入Class文件;<br /> (2) 链接:把类的二进制数据合并到JRE中;<br />     (a)校验:检查载入Class文件数据的正确性;<br />     (b)准备:给类的静态变量分配存储空间;<br />     (c)解析:将符号引用转成直接引用;<br /> (3) 初始化:对类的静态变量,静态代码块执行初始化操作</p> <p>类加载器和双亲委派模型</p> <p>(1) Bootstrap ClassLoader : 将存放于<JAVA_HOME>\lib目录中的,或者被-Xbootclasspath参数所指定的路径中的,并且是虚拟机识别的(仅按照文件名识别,如 rt.jar 名字不符合的类库即使放在lib目录中也不会被加载)类库加载到虚拟机内存中。启动类加载器无法被Java程序直接引用<br /> (2) Extension ClassLoader : 将<JAVA_HOME>\lib\ext目录下的,或者被java.ext.dirs系统变量所指定的路径中的所有类库加载。开发者可以直接使用扩展类加载器。<br /> (3) Application ClassLoader : 负责加载用户类路径(ClassPath)上所指定的类库,开发者可直接使用。</p> <p>双亲委派模型的工作过程:如果一个类加载器收到了类加载的请求,它首先不会自己去尝试加载这个类,而是把这个请求委派给父类加载器去完成,每个层次的类加载器都是如此。因此所有的加载请求最终都应该传达到顶层的启动类加载器中,只有当父加载器反馈无法完成这个加载请求(它的搜索范围中没有找到所需的类)时,子加载器才会尝试自己去加载。</p> <p>双亲委派模型要求除了顶层的启动类加载器外,其余的类加载器都应有自己的父类加载器。这些类加载器的父子关系不是以继承的关系实现,而都是使用组合关系来复用父加载器的代码。<br />  </p> <pre> <code class="language-java">public class ClassLoaderTest { public static void main(String[] args) { //输出ClassLoaderText的类加载器名称 System.out.println("ClassLoaderText类的加载器的名称:" + ClassLoaderTest.class.getClassLoader().getClass().getName()); System.out.println("System类的加载器的名称:" + System.class.getClassLoader()); System.out.println("ArrayList类的加载器的名称:" + ArrayList.class.getClassLoader()); ClassLoader cl = ClassLoaderTest.class.getClassLoader(); while (cl != null) { System.out.print(cl.getClass().getName() + "->"); cl = cl.getParent(); } System.out.println(cl); } }</code></pre> <pre> <code class="language-html">ClassLoaderText类的加载器的名称:sun.misc.Launcher$AppClassLoader System类的加载器的名称:null ArrayList类的加载器的名称:null sun.misc.Launcher$AppClassLoader->sun.misc.Launcher$ExtClassLoader->null</code></pre> 好处:java类随着它的类加载器一起具备了一种带有优先级的层次关系。例如类java.lang.Object,它存放在rt.jar中,无论哪个类加载器要加载这个类,最终都会委派给启动类加载器进行加载,因此Object类在程序的各种类加载器环境中都是同一个类。相反,如果用户自己写了一个名为java.lang.Object的类,并放在程序的Classpath中,那系统中将会出现多个不同的Object类,java类型体系中最基础的行为也无法保证,应用程序也会变得一片混乱。
  • spring boot FastDFS Java client使用-Java编程

    Java编程之spring boot FastDFS Java client使用,Java编程,FastDFS Java客户端<h2>一、获取FastDFS Java的客户端链接工具</h2> 由于maven库中并没有编译好的jar工具包,目前只能通过GitHub下载源码自己打包成jar<br /> 下载地址:<a href="https://github.com/happyfish100/fastdfs-client-java" rel="external nofollow" target="_blank">点击去下去</a><br /> <br /> 如果通过浏览器直接下载,则下载后是一个zip压缩包。<br /> 1.解压zip包<br /> 2.eclipse通过已存在的maven项目方式导入<br /> 3.执行maven install命令打包<br /> <br /> 打包完成后再target目录下有打包好的jar文件:<br /> <strong>fastdfs-client-java-1.27-SNAPSHOT.jar</strong><br />   <h2>二、导入spring boot项目</h2> 创建一个spring boot项目,在创建好的项目中创建一个lib的文件夹,将上面打包的jar文件复制进去。然后打开spring boot项目的pom.xml文件,添加本地依赖如下: <pre> <code class="language-xml"> <!-- fastfds 客户端 |https://github.com/happyfish100/fastdfs-client-java --> <dependency> <groupId>org.csource</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.27-SNAPSHOT</version> <scope>system</scope> <systemPath>${project.basedir}/lib/fastdfs-client-java-1.27-SNAPSHOT.jar</systemPath> </dependency></code></pre> <h2>三、配置文件</h2> 在spring boot项目的resource目录下创建一个fdfs_client.conf文件,内容如下: <pre> <code>#注1:tracker_server指向您自己IP地址和端口,1-n个 #注2:除了tracker_server,其它配置项都是可选的 #注3:.conf 配置文件文件所在位置可以是项目classpath(或OS文件系统目录比如/opt/): #注4:.conf 配置文件优先按OS文件系统路径读取,没有找到才查找项目classpath,尤其针对linux环境下的相对路径 #注5:其他相关参考:https://github.com/happyfish100/fastdfs-client-java connect_timeout = 120 network_timeout = 130 charset = UTF-8 http.tracker_http_port = 80 http.anti_steal_token = no http.secret_key = FastDFS1234567890 tracker_server = 192.168.8.202:22122</code></pre> 其中里面的tracker_server 以及端口均需要根据自身使用的实际请来修改 <h2><br /> 四、编写一个公用的Java 的fastdfs客户端</h2> 首先需要创建一个FastDFS的文件辅助类:<br /> <strong>FastDSFile.java</strong> <pre> <code class="language-java">public class FastDSFile { private String name; private byte[] content; private String ext; private String md5; public String getName() { return name; } public void setName(String name) { this.name = name; } public byte[] getContent() { return content; } public void setContent(byte[] content) { this.content = content; } public String getExt() { return ext; } public void setExt(String ext) { this.ext = ext; } public String getMd5() { return md5; } public void setMd5(String md5) { this.md5 = md5; } } </code></pre> <br /> <strong>FastDFSClient.java</strong> <pre> <code class="language-java">import java.io.File; import java.io.IOException; import org.csource.common.MyException; import org.csource.fastdfs.ClientGlobal; import org.csource.fastdfs.StorageClient1; import org.csource.fastdfs.StorageServer; import org.csource.fastdfs.TrackerClient; import org.csource.fastdfs.TrackerServer; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import com.alibaba.fastjson.JSONArray; /** * fastDFS文件服务Java客户端实现,所有执行方法均为静态方法。 * * @author xq * */ public class FastDFSClient { /** * 客户端 */ private static StorageClient1 storageClient1 = null; // 初始化客户端,加载类时候执行片段 static { try { Resource resource = new ClassPathResource("fdfs_client.conf"); File file = resource.getFile(); String configFile = file.getAbsolutePath(); ClientGlobal.init(configFile); // TrackerClient trackerClient = new TrackerClient(ClientGlobal.g_tracker_group); // TrackerServer trackerServer = trackerClient.getConnection(); // StorageServer storageServer = trackerClient.getStoreStorage(trackerServer); // storageClient1 = new StorageClient1(trackerServer, storageServer); System.out.println("FastDFS Client Init Success!"); } catch (Exception e) { e.printStackTrace(); System.out.println("FastDFS Client Init Fail!"); } } /*** * 文件上传 * * @param fastDSFile * @return * @throws IOException * @throws MyException */ public static JSONArray upload(FastDSFile fastDSFile) throws IOException, MyException { String[] uploadResult = storageClient1.upload_file(fastDSFile.getContent(), fastDSFile.getExt(), null); // String arr = JSONArray.toJSONString(uploadResult); JSONArray arr = (JSONArray) JSONArray.toJSON(uploadResult); return arr; } /** * 文件下载 * * @param groupName * @param remoteFileName * @return * @throws IOException * @throws MyException */ public static byte[] download(String groupName, String remoteFileName) throws IOException, MyException { return storageClient1.download_file(groupName, remoteFileName); } /** * 文件删除 * * @param groupName * @param remoteFileName * @throws Exception * @return 返回0成功;非0失败. */ public static int delete(String groupName, String remoteFileName) throws Exception { return storageClient1.delete_file(groupName, remoteFileName); } } </code></pre> <h2>五、编写测试</h2> <strong>DemoSpringbootFastdfsApplicationTests.java</strong> <pre> <code class="language-java">import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import com.alibaba.fastjson.JSONArray; import net.xqlee.project.demo.fastdfs.clients.FastDFSClient; import net.xqlee.project.demo.fastdfs.clients.FastDSFile; @RunWith(SpringRunner.class) @SpringBootTest public class DemoSpringbootFastdfsApplicationTests { @Test public void contextLoads() { try { FileInputStream fis = new FileInputStream(new File("C:/Users/xq/Pictures/tx.jpg")); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] cache = new byte[4096]; while (fis.read(cache) != -1) { bos.write(cache); } fis.close(); FastDSFile fastDSFile = new FastDSFile(); fastDSFile.setContent(bos.toByteArray()); fastDSFile.setExt("jpg"); // -------上传---- JSONArray rs = FastDFSClient.upload(fastDSFile); System.out.println("上传结束:" + rs); // -------下载---- byte[] dfile = FastDFSClient.download(rs.getString(0), rs.getString(1)); FileOutputStream fos = new FileOutputStream(new File("C:/Users/xq/Pictures/tx-fdfs.jpg")); fos.write(dfile); fos.flush(); fos.close(); // -------删除----- int ds=FastDFSClient.delete(rs.getString(0), rs.getString(1)); // System.out.println("Delete:"+ds); System.out.println("---End----"); } catch (Exception e) { e.printStackTrace(); } } } </code></pre> <br /> 运行测试,可以看到在上传图片的目录下多了一个名为tx-fdfs.jpg的文件
  • Java编程之java static关键字

    Java编程之java static关键字,Java编程,static关键字<h2>一、java中static关键字</h2> java中的static关键字可以应用于变量、方法、块、导入和内部类。在本教程中,我们将了解在这些地方使用static关键字的效果。<br /> <br /> 本文将要讲解的内容目录: <ul> <li>Static 变量的使用讲解</li> <li>Static 方法的使用讲解</li> <li>Static 导入声明使用讲解</li> <li>Static 块的使用讲解</li> <li>Static 类的使用讲解</li> <li>static关键字使用总结</li> </ul> <h2>二、static变量</h2> 要声明一个static变量,请在变量声明中使用static关键字。static变量的语法是: <pre> <code>ACCESS_MODIFER static DATA_TYPE VARNAME;</code></pre> 例如,用这种方式声明整数类型的公共静态变量。 <pre> <code class="language-java">public static Integer staticVar;</code></pre> 静态变量最重要的一点是它们属于类级别。这意味着在运行时只能有一个变量的副本。当在类定义中定义静态变量时,类的每个实例都可以访问该单一副本。单独的类实例不会有它们自己的本地副本,就像它们对非静态变量一样。<br /> <br /> 让我们来了解一个例子: <pre> <code class="language-java">public class JavaStaticExample { public static void main(String[] args) { DataObject objOne = new DataObject(); objOne.staticVar = 10; objOne.nonStaticVar = 20; DataObject objTwo = new DataObject(); System.out.println(objTwo.staticVar); //10 System.out.println(objTwo.nonStaticVar); //null DataObject.staticVar = 30; //Direct Access System.out.println(objOne.staticVar); //30 System.out.println(objTwo.staticVar); //30 } } class DataObject { public static Integer staticVar; public Integer nonStaticVar; }</code></pre> <br /> 输出内容: <pre> <code>10 null 30 30</code></pre> 注意我们如何将值更改为30,并且这两个对象现在看到的更新值为30。<br /> <br /> 另一件您应该注意到的是,我们如何能够使用它的类名来访问静态变量,即dataobject . staticvar。我们不需要创建任何实例来访问静态变量。它清楚地表明静态变量属于类范围。 <h2>三、static方法</h2> 要声明静态方法,请在方法声明中使用静态关键字。静态方法的语法是: <pre> <code>ACCESS_MODIFER static RETURN_TYPE METHOD_NAME;</code></pre> <br /> 例如,用这种方式声明整数类型的公共静态变量。 <pre> <code class="language-java">public static Integer getStaticVar(){ return staticVar; }</code></pre> <p>要记住的一些事项。</p> <ol> <li>您只能在静态方法中访问静态变量。如果您尝试访问任何非静态变量,将生成的编译器错误信息“无法对非静态字段非静态变量进行静态引用”。</li> <li>静态方法可以通过它的类引用来访问,并且不需要创建类的实例。虽然您也可以使用实例引用,但是与通过类引用的访问相比,它不会有任何不同。</li> <li>静态方法也属于类级范围。</li> </ol>   <pre> <code class="language-java">public class JavaStaticExample { public static void main(String[] args) { DataObject.staticVar = 30; //Direct Access Integer value1 = DataObject.getStaticVar(); //access with class reference DataObject objOne = new DataObject(); Integer value2 = objOne.getStaticVar(); //access with instance reference System.out.println(value1); System.out.println(value2); } } class DataObject { public Integer nonStaticVar; public static Integer staticVar; //static variable public static Integer getStaticVar(){ return staticVar; } }</code></pre> 输出内容: <pre> <code>30 30</code></pre> <h2>四、static Import 声明</h2> <p>    正常的导入声明从包中导入类,因此它们可以在没有包引用的情况下使用。类似地,静态导入声明从类中导入静态成员,并允许它们在没有类引用的情况下使用。</p> <p>    静态导入声明也有两种类型:单静态导入和静态导入。单静态导入声明从类型中导入一个静态成员。静态输入-点播声明导入了类型的所有静态成员。<br />  </p> <pre> <code class="language-java">//Single-static-import declaration: import static <<package name>>.<<type name>>.<<static member name>>; //Static-import-on-demand declaration: import static <<package name>>.<<type name>>.*;</code></pre> 例如, <code>System.out</code> is <pre> <code class="language-java">//Static import statement import static java.lang.System.out; public class JavaStaticExample { public static void main(String[] args) { DataObject.staticVar = 30; out.println(DataObject.staticVar); //Static import statement example } } class DataObject { public static Integer staticVar; //static variable }</code></pre> 输出: <pre> <code>30</code></pre> <h2>五、Static 块</h2> 静态块是类初始化代码的一部分,它用静态关键字包装。一般的语法是: <pre> <code class="language-java">static { //initialize static members of class }</code></pre> 当类装入内存时,将执行静态块。一个类可以有多个静态块,并且它们将以相同的顺序执行,它们在类定义中出现。 <pre> <code class="language-java">import static java.lang.System.out; class DataObject { public Integer nonStaticVar; public static Integer staticVar; //static variable //It will be executed first static { staticVar = 40; //nonStaticVar = 20; //Not possible to access non-static members } //It will be executed second static { out.println(staticVar); } }</code></pre> 输出: <pre> <code>40</code></pre> <h2>六、static 类</h2> 在java中,可以将静态类作为内部类。与其他静态成员一样,嵌套的类属于类范围,所以可以访问内部的静态类,而不具有外部类的对象。 <pre> <code class="language-java">public class JavaStaticExample { public static void main(String[] args) { //Static inner class example System.out.println( DataObject.StaticInnerClas.innerStaticVar ); } } class DataObject { public Integer nonStaticVar; public static Integer staticVar; //static variable static class StaticInnerClas { Integer innerNonStaticVar = 60; static Integer innerStaticVar = 70; //static variable inside inner class } }</code></pre> 请注意,静态内部类无法访问外部类的非静态成员。它只能访问来自外部类的静态成员。 <pre> <code class="language-java">public class JavaStaticExample { public static void main(String[] args) { //Static inner class example DataObject.StaticInnerClas.accessOuterClass(); } } class DataObject { public Integer nonStaticVar; public static Integer staticVar; //static variable static { staticVar = 40; //nonStaticVar = 20; //Not possible to access non-static members } public static Integer getStaticVar(){ return staticVar; } static class StaticInnerClas { public static void accessOuterClass() { System.out.println(DataObject.staticVar); //static variable of outer class System.out.println(DataObject.getStaticVar()); //static method of outer class } } }</code></pre> 输出: <pre> <code>40</code></pre> <h2>七、总结</h2> 让我们总结一下java中静态使用的所有内容。 <ol> <li>静态成员属于类。不需要创建类实例来访问静态成员。</li> <li>静态成员(变量和方法)只能在静态方法和静态块中访问。</li> <li>不能在静态方法、块和内部类中访问非静态成员。</li> <li>一个类可以有多个静态块,它们将按照类定义的顺序执行。</li> <li>只有当类声明为内部类内部类时,类才能是静态的。</li> <li>静态导入可用于从类导入所有的静态成员。这些成员可以被引用,没有任何类引用。</li> </ol>
  • java编程之java jwt token使用autho0-jwt框架使用(二)

    java编程之java jwt token使用,autho0的Java-jwt框架使用,java编程,java-jwt<h2>一、前言</h2> Java编程中使用jwt,首先你必须了解jwt是什么,长什么样子。如果你不了解可以先去本站另外一篇博客<a href="http://www.leftso.com/blog/220.html" rel="" target="_blank" >什么是JWT?</a><br />   <h2>二、Java编程中jwt框架选择</h2> 在Java编程中,实现jwt标准的有很多框架,本博客采用的框架是auth0的java-jwt版本为3.2.0 <h2>三、什么是Java-JWT</h2> auth0的java-jwt是一个JSON WEB TOKEN(JWT)的一个实现。 <h2>四、安装下载相关依赖</h2> 如果你是采用<strong>maven</strong>的方式,在你的项目pom.xml文件中添加以下java-jwt的依赖片段: <pre> <code class="language-xml"><dependency> <groupId>com.auth0</groupId> <artifactId>java-jwt</artifactId> <version>3.2.0</version> </dependency></code></pre> 如果你是采用<strong>Gradle</strong>的方式,则添加以下内容: <pre> <code>compile 'com.auth0:java-jwt:3.2.0'</code></pre> <h2>五、java-jwt已经实现的算法</h2> 该库使用以下算法实现JWT验证和签名: <table class="table table-bordered table-hover" > <thead> <tr> <th>JWS</th> <th>算法</th> <th>介绍</th> </tr> </thead> <tbody> <tr> <td>HS256</td> <td>HMAC256</td> <td>HMAC with SHA-256</td> </tr> <tr> <td>HS384</td> <td>HMAC384</td> <td>HMAC with SHA-384</td> </tr> <tr> <td>HS512</td> <td>HMAC512</td> <td>HMAC with SHA-512</td> </tr> <tr> <td>RS256</td> <td>RSA256</td> <td>RSASSA-PKCS1-v1_5 with SHA-256</td> </tr> <tr> <td>RS384</td> <td>RSA384</td> <td>RSASSA-PKCS1-v1_5 with SHA-384</td> </tr> <tr> <td>RS512</td> <td>RSA512</td> <td>RSASSA-PKCS1-v1_5 with SHA-512</td> </tr> <tr> <td>ES256</td> <td>ECDSA256</td> <td>ECDSA with curve P-256 and SHA-256</td> </tr> <tr> <td>ES384</td> <td>ECDSA384</td> <td>ECDSA with curve P-384 and SHA-384</td> </tr> <tr> <td>ES512</td> <td>ECDSA512</td> <td>ECDSA with curve P-521 and SHA-512</td> </tr> </tbody> </table> <h2>六、如何使用java-jwt</h2> <strong>6.1.选择一种算法</strong> <p>  算法定义了一个令牌是如何被签名和验证的。它可以用HMAC算法的原始值来实例化,也可以在RSA和ECDSA算法的情况下对密钥对或密钥提供程序进行实例化。创建后,该实例可用于令牌签名和验证操作。</p> <p>在使用RSA或ECDSA算法时,只需要签署JWTs,就可以通过传递null值来避免指定公钥。当您需要验证JWTs时,也可以使用私钥进行操作<br /> <br /> 使用静态的字符密文或者key来获取算法器:<br />  </p> <pre> <code class="language-java">//HMAC Algorithm algorithmHS = Algorithm.HMAC256("secret"); //RSA RSAPublicKey publicKey = //Get the key instance RSAPrivateKey privateKey = //Get the key instance Algorithm algorithmRS = Algorithm.RSA256(publicKey, privateKey);</code></pre> 使用一个key提供者来获取算法:<br />   通过使用KeyProvider,您可以在运行时更改密钥,用于验证令牌签名或为RSA或ECDSA算法签署一个新的令牌。这是通过实现RSAKeyProvider或ECDSAKeyProvider方法实现的: <ul> <li><code>getPublicKeyById(String kid)</code>: 它在令牌签名验证中调用,它应该返回用于验证令牌的密钥。如果使用了关键的轮换,例如JWK,它可以使用id来获取正确的轮换键(或者只是一直返回相同的键)。</li> <li><code>getPrivateKey()</code>: 在令牌签名期间调用它,它应该返回用于签署JWT的密钥。</li> <li><code>getPrivateKeyId()</code>:在令牌签名期间调用它,它应该返回标识由getPrivateKey()返回的键的id的id。这个值比JWTCreator.Builder和keyid(String)方法中的值更受欢迎。如果您不需要设置孩子的值,就避免使用KeyProvider实例化算法。</li> </ul> 下面的代码片段将展示如何使用: <pre> <code class="language-java">final JwkStore jwkStore = new JwkStore("{JWKS_FILE_HOST}"); final RSAPrivateKey privateKey = //Get the key instance final String privateKeyId = //Create an Id for the above key RSAKeyProvider keyProvider = new RSAKeyProvider() { @Override public RSAPublicKey getPublicKeyById(String kid) { //Received 'kid' value might be null if it wasn't defined in the Token's header RSAPublicKey publicKey = jwkStore.get(kid); return (RSAPublicKey) publicKey; } @Override public RSAPrivateKey getPrivateKey() { return privateKey; } @Override public String getPrivateKeyId() { return privateKeyId; } }; Algorithm algorithm = Algorithm.RSA256(keyProvider); //Use the Algorithm to create and verify JWTs.</code></pre> <br /> <em>提示:对于使用JWKs的简单的键轮换,可以尝试JWKs-rsa-java库。</em><br /> <br /> <strong>6.2.创建一个签名的JWT token</strong><br /> 首先需要通过调用<code>jwt.create()</code>创建一个<code>JWTCreator</code>实例 <ul> <li>例如使用 <code>HS256算法:</code></li> </ul> <pre> <code class="language-java">try { Algorithm algorithm = Algorithm.HMAC256("secret"); String token = JWT.create() .withIssuer("auth0") .sign(algorithm); } catch (UnsupportedEncodingException exception){ //UTF-8 encoding not supported } catch (JWTCreationException exception){ //Invalid Signing configuration / Couldn't convert Claims. }</code></pre> <ul> <li>例如使用<code>RS256算法:</code></li> </ul> <pre> <code class="language-java">RSAPublicKey publicKey = //Get the key instance RSAPrivateKey privateKey = //Get the key instance try { Algorithm algorithm = Algorithm.RSA256(publicKey, privateKey); String token = JWT.create() .withIssuer("auth0") .sign(algorithm); } catch (JWTCreationException exception){ //Invalid Signing configuration / Couldn't convert Claims. }</code></pre> 如果Claim不能转换为JSON,或者在签名过程中使用的密钥无效,那么将会抛出<code>JWTCreationException</code>异常。<br /> <br /> <strong>6.3.验证令牌</strong><br />   首先需要通过调用jwt.require()和传递算法实例来创建一个JWTVerifier实例。如果您要求令牌具有特定的Claim值,请使用构建器来定义它们。方法build()返回的实例是可重用的,因此您可以定义一次,并使用它来验证不同的标记。最后调用verifier.verify()来验证token <ul> <li>例如使用 <code>HS256算法的时候:</code></li> </ul> <pre> <code class="language-java">String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.AbIJTDMFc7yUa5MhvcP03nJPyCPzZtQcGEp-zWfOkEE"; try { Algorithm algorithm = Algorithm.HMAC256("secret"); JWTVerifier verifier = JWT.require(algorithm) .withIssuer("auth0") .build(); //Reusable verifier instance DecodedJWT jwt = verifier.verify(token); } catch (UnsupportedEncodingException exception){ //UTF-8 encoding not supported } catch (JWTVerificationException exception){ //Invalid signature/claims }</code></pre> <ul> <li>例如使用 <code>RS256算法的时候:</code></li> </ul> <pre> <code class="language-java">String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.AbIJTDMFc7yUa5MhvcP03nJPyCPzZtQcGEp-zWfOkEE"; RSAPublicKey publicKey = //Get the key instance RSAPrivateKey privateKey = //Get the key instance try { Algorithm algorithm = Algorithm.RSA256(publicKey, privateKey); JWTVerifier verifier = JWT.require(algorithm) .withIssuer("auth0") .build(); //Reusable verifier instance DecodedJWT jwt = verifier.verify(token); } catch (JWTVerificationException exception){ //Invalid signature/claims }</code></pre> 如果令牌有一个无效的签名,或者没有满足Claim要求,那么将会抛出JWTVerificationException异常<br /> <br /> <strong>6.4.jwt时间的验证</strong><br /> JWT令牌可能包括可用于验证的DateNumber字段: <ul> <li>这个令牌发布了一个过期的时间 <code>"iat" < TODAY</code></li> <li>这个令牌还没过期 <code>"exp" > TODAY</code> and</li> <li>这个令牌已经被使用了. <code>"nbf" > TODAY</code></li> </ul> 当验证一个令牌时,时间验证会自动发生,导致在值无效时抛出一个JWTVerificationException。如果前面的任何一个字段都丢失了,那么在这个验证中就不会考虑这些字段。<br /> 要指定令牌仍然被认为有效的余地窗口,在JWTVerifier builder中使用accept回旋()方法,并传递一个正值的秒值。这适用于上面列出的每一项。 <pre> <code class="language-java">JWTVerifier verifier = JWT.require(algorithm) .acceptLeeway(1) // 1 sec for nbf, iat and exp .build();</code></pre> 您还可以为给定的日期声明指定一个自定义值,并为该声明覆盖缺省值。 <pre> <code class="language-java">JWTVerifier verifier = JWT.require(algorithm) .acceptLeeway(1) //1 sec for nbf and iat .acceptExpiresAt(5) //5 secs for exp .build();</code></pre> 如果您需要在您的lib/app中测试此行为,将验证实例转换为basever可视化,以获得verific.build()方法的可见性,该方法可以接受定制的时钟。例如: <pre> <code class="language-java">BaseVerification verification = (BaseVerification) JWT.require(algorithm) .acceptLeeway(1) .acceptExpiresAt(5); Clock clock = new CustomClock(); //Must implement Clock interface JWTVerifier verifier = verification.build(clock);</code></pre> <br /> <strong>6.5解码一个jwt令牌</strong> <pre> <code class="language-java">String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.AbIJTDMFc7yUa5MhvcP03nJPyCPzZtQcGEp-zWfOkEE"; try { DecodedJWT jwt = JWT.decode(token); } catch (JWTDecodeException exception){ //Invalid token }</code></pre> 如果令牌有无效的语法,或者消息头或有效负载不是JSONs,那么将会抛出JWTDecodeException异常。<br /> <br /> <strong>6.6JWT头部解析</strong> <h4><br /> Algorithm ("alg")</h4> 返回jwt的算法值或,如果没有定义则返回null <pre> <code class="language-java">String algorithm = jwt.getAlgorithm();</code></pre> <p> </p> <p>Type ("typ")</p> <p>返回jwt的类型值,如果没有定义则返回null(多数情况类型值为jwt)</p> <pre> <code class="language-java">String type = jwt.getType();</code></pre> <p> </p> <p>Content Type ("cty")</p> <p>返回内容的类型,如果没有定义则返回null</p> <pre> <code class="language-java">String contentType = jwt.getContentType();</code></pre> <p><br /> Key Id ("kid")<br /> 返回key的id值,如果没有定义则返回null<br />  </p> <pre> <code class="language-java">String keyId = jwt.getKeyId();</code></pre> <h4>私有的Claims,即自定义字段</h4> 在令牌的头部中定义的附加声明可以通过调用getHeaderClaim() 获取,即使无法找到,也会返回。您可以通过调用claim.isNull()来检查声明的值是否为null。 <pre> <code class="language-java">Claim claim = jwt.getHeaderClaim("owner");</code></pre> 当使用jwt.create()创建一个令牌时,您可以通过调用withHeader()来指定头声明,并同时传递声明的映射。 <pre> <code class="language-java">Map<String, Object> headerClaims = new HashMap(); headerClaims.put("owner", "auth0"); String token = JWT.create() .withHeader(headerClaims) .sign(algorithm); </code></pre> <em>提示:在签名过程之后,alg和typ值将始终包含在Header中。</em><br /> <br /> <strong>6.7JWT的负载(Payload)声明</strong> <h4>Issuer ("iss")</h4> 返回签发者的名称值,如果没有在负载中定义则返回null <pre> <code class="language-java">String issuer = jwt.getIssuer();</code></pre> <h4>Subject ("sub")</h4> 返回jwt所面向的用户的值,如果没有在负载中定义则返回null <pre> <code class="language-java">String subject = jwt.getSubject();</code></pre> <h4>Audience ("aud")</h4> 返回该jwt由谁接收,如果没有在负载中定义则返回null <pre> <code class="language-java">List<String> audience = jwt.getAudience();</code></pre> <h4>Expiration Time ("exp")</h4> 返回该jwt的过期时间,如果在负载中没有定义则返回null <pre> <code class="language-java">Date expiresAt = jwt.getExpiresAt();</code></pre> <h4>Not Before ("nbf")</h4> Returns the Not Before value or null if it's not defined in the Payload. <pre> <code class="language-java">Date notBefore = jwt.getNotBefore();</code></pre> <h4>Issued At ("iat")</h4> 返回在什么时候签发的,如果在负载中没有定义则返回null <pre> <code class="language-java">Date issuedAt = jwt.getIssuedAt();</code></pre> <h4>JWT ID ("jti")</h4> 返回该jwt的唯一标志,如果在负载中没有定义则返回null <pre> <code class="language-java">String id = jwt.getId();</code></pre> <strong>自定义声明</strong><br /> 在令牌有效负载中定义的附加声明可以通过调用getClaims()或 getClaim()和传递声明名来获得。即使无法找到声明,也将会有返回值。您可以通过调用claim.isNull()来检查声明的值是否为null。 <pre> <code class="language-java">Map<String, Claim> claims = jwt.getClaims(); //Key is the Claim name Claim claim = claims.get("isAdmin");</code></pre> 或者: <pre> <code class="language-java">Claim claim = jwt.getClaim("isAdmin");</code></pre> 当使用jwt.create()创建一个令牌时,您可以通过调用withClaim()来指定自定义声明,并同时传递名称和值。 <pre> <code class="language-java">String token = JWT.create() .withClaim("name", 123) .withArrayClaim("array", new Integer[]{1, 2, 3}) .sign(algorithm);</code></pre> 您还可以通过调用withClaim()来验证jwt.require()的自定义声明,并传递该名称和所需的值。 <pre> <code class="language-java">JWTVerifier verifier = JWT.require(algorithm) .withClaim("name", 123) .withArrayClaim("array", 1, 2, 3) .build(); DecodedJWT jwt = verifier.verify("my.jwt.token");</code></pre> <em>提示:当前支持的自定义JWT声明创建和验证的类型是:Boolean, Integer, Double, String, Date 和Arrays。</em><br /> <br /> <strong>6.8Claim Class</strong><br /> 索赔类是索赔值的包装器。它允许您将索赔作为不同的类类型。可用的工具方法:<br /> 原始的: <ul> <li><strong>asBoolean()</strong>: 返回布尔值,如果不能转换返回null。</li> <li><strong>asInt()</strong>: 返回整数值,如果不能转换返回null。</li> <li><strong>asDouble()</strong>: 返回 Double 值,如果不能转换则返回null。</li> <li><strong>asLong()</strong>: 返回Long 值,如果不能转换则返回null。</li> <li><strong>asString()</strong>: 返回String值,如果不能转换则返回null。</li> <li><strong>asDate()</strong>: 返回 Date值,如果不能转换则返回null。 必须是一个数字日期 (Unix 系统时间戳). 注意,JWT标准指定所有的数字日期值必须以秒为单位。</li> </ul> <h4>自定义类型和集合:</h4> 要获得作为集合的声明,您需要提供要转换的内容的类类型 <ul> <li>as(class): 返回 Class Type 的解析值. 对于集合,您应该使用asArray和asList方法。</li> <li>asMap(): 返回被转换为 Map<String, Object>的值</li> <li>asArray(class): 返回被转换成元素类型的 Class Type, or null if the value isn't a JSON Array.</li> <li>asList(class): 返回集合元素的 Class Type, or null if the value isn't a JSON Array.</li> </ul> <em>如果不能将值转换为给定的类类型,则会抛出JWTDecodeException异常</em><br /> <br /> <em>翻译的不标准的后续更近,欢迎提供宝贵意见或者翻译,联系leftso@qq.com</em>
  • 学习java垃圾回收

    垃圾回收(GC)一直是Java受欢迎背后的重要特性之一。垃圾回收是Java中用于释放未使用的内存的机制。本质上,它追踪所有仍在使用的对象,并将剩下的标记为垃圾。Java的垃圾回收被认为是一种自动内存管理模式,因为程序员不必指定对象准备被释放。垃圾回收在低优先级的线程上运行。    垃圾回收(GC)一直是Java受欢迎背后的重要特性之一。垃圾回收是Java中用于释放未使用的内存的机制。本质上,它追踪所有仍在使用的对象,并将剩下的标记为垃圾。Java的垃圾回收被认为是一种自动内存管理模式,因为程序员不必指定对象准备被释放。垃圾回收在低优先级的线程上运行。<br />     在本教程中,我们将学习java中内存分配/解除分配有关的各种概念,在场景后面运行的算法以及您必须自定义此行为的选项。 <h2 style="margin-left:0px; margin-right:0px; text-align:start">对象生命周期</h2> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">Java的对象生命周期可以分为三个阶段:</span></span></span></p> <ol style="margin-left:40px; margin-right:0px"> <li style="list-style-type:decimal"> <h4 style="margin-left:0px; margin-right:0px">对象创建</h4> <p style="margin-left:0px; margin-right:0px">要创建一个对象,通常我们使用<code>new</code>关键字。例如</p> <div> <div style="margin-left:0px; margin-right:0px"> <pre> <code class="language-java">Object obj = new Object();</code></pre> </div> </div> <p style="margin-left:0px; margin-right:0px">创建对象时,会分配特定数量的内存用于存储该对象。分配的内存量可以根据体系结构和JVM而有所不同。</p> </li> <li style="list-style-type:decimal"> <h4 style="margin-left:0px; margin-right:0px">对象在使用中</h4> <p style="margin-left:0px; margin-right:0px">到时候,对象被应用程序的其他对象使用(其他活动对象具有指向它的引用)。在使用过程中,对象驻留在内存中,并可能包含对其他对象的引用。</p> </li> <li style="list-style-type:decimal"> <h4 style="margin-left:0px; margin-right:0px">对象破坏</h4> <p style="margin-left:0px; margin-right:0px">垃圾回收系统监视对象,并在可行的情况下统计每个对象的引用数量。当没有对象的引用时,就没有办法用当前运行的代码来访问它,所以释放相关联的内存是非常合理的。</p> </li> </ol> <h2 style="margin-left:0px; margin-right:0px; text-align:start">垃圾回收算法</h2> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">对象创建是通过您编写的代码完成的; 以及您用来使用其提供的功能的框架。作为一名Java开发人员,我们不需要取消分配内存或取消引用对象。它由gargabe收集器在JVM级别自动完成。自从Java诞生以来,已经有很多关于在场景后面运行的算法的更新来释放内存。让我们看看他们是如何工作的?</span></span></span></p> <h4 style="margin-left:0px; margin-right:0px; text-align:start">马克和扫描</h4> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">它是初始和非常基本的算法,分两个阶段运行:</span></span></span></p> <ol style="margin-left:40px; margin-right:0px"> <li style="list-style-type:decimal"><strong>标记活动对象</strong> - 找出所有仍然活着的对象。</li> <li style="list-style-type:decimal"><strong>删除无法访问的对象</strong> - <strong>删除</strong>其他所有内容 - 所谓的死亡和未使用的对象。</li> </ol> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">首先,GC将一些特定对象定义为<strong>垃圾</strong></span></span></span>回收<span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff"><strong>根</strong>。例如当前正在执行的方法的本地变量和输入参数,活动线程,已加载类的静态字段和JNI引用。现在,GC遍历内存中的整个对象图,从这些根开始,并从根到下一个对象的引用。GC访问的每个对象都被标记为活动。</span></span></span></p> <blockquote> <p style="margin-left:0px; margin-right:0px">应用程序线程需要停止标记才能发生,因为如果它不断变化,它不能真正遍历图形。它被称为<strong>停止世界暂停</strong>。</p> </blockquote> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">第二阶段是摆脱未使用的对象来释放内存。这可以通过多种方式完成,例如</span></span></span></p> <ul> <li style="list-style-type:disc"><strong>正常删除</strong> - 正常删除将未引用的对象移除到空闲空间并保留引用的对象和指针。内存分配器(散列表的种类)持有对可以分配新对象的可用空间块的引用。</li> </ul> <p style="margin-left:0px; margin-right:0px">它经常被当作<code>mark-sweep</code>算法。<br /> <img alt="1" class="img-thumbnail" src="/assist/images/blog/622f18a72d874e05b1fda870bdf4918b.png" /></p> <ul> <li><strong>压缩删除</strong> - 仅删除未使用的对象效率不高,因为可用内存块分散在存储区域中,并且如果创建的对象足够大并且未找到足够大的内存块,将导致OutOfMemoryError。</li> </ul> <p style="margin-left:0px; margin-right:0px; text-align:left"><span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">为了解决这个问题,在删除未引用的对象之后,对其余的引用对象进行压缩。这里的压缩指的是将参考对象移动到一起的过程。这使得新的内存分配变得更加容易和快速。</span></span></span></p> <p style="margin-left:0px; margin-right:0px; text-align:left"><span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">它经常被当作<code>mark-sweep-compact</code>算法。</span></span></span></p> <img alt="2" class="img-thumbnail" src="/assist/images/blog/4724875646384aae8e4fd162f30dcc4a.png" /> <ul> <li><strong>删除与复制</strong> - 这与标记和补偿方法非常相似,因为它们也会重新定位所有活动对象。重要的区别是重定位的目标是不同的存储区域。</li> </ul> <p style="margin-left:0px; margin-right:0px; text-align:left"><span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">它经常被当作<code>mark-copy</code>算法。<br /> <img alt="3" class="img-thumbnail" src="/assist/images/blog/8f33830349204cf0bfd62e147511c737.png" /></span></span></span></p> <h4 style="margin-left:0px; margin-right:0px; text-align:start">并发标记扫描(CMS)垃圾回收</h4> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">CMS垃圾回收本质上是一种升级的标记和扫描方法。它<strong>使用多线程</strong>扫描堆内存。它进行了修改,以利用更快的系统并提高性能。</span></span></span></p> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">它试图通过与应用程序线程<em>同时</em>进行大部分垃圾</span></span></span>回收<span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">工作来尽量减少由于垃圾</span></span></span>回收<span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">造成的暂停。它使用年轻一代中的并行停止世界<strong>标记复制</strong>算法和老一代中主要是并发的<strong>标记扫描</strong>算法。</span></span></span></p> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">要使用CMS GC,请使用下面的JVM参数:</span></span></span></p> <pre> <code class="language-html">-XX:+UseConcMarkSweepGC</code></pre> <h6 style="margin-left:0px; margin-right:0px; text-align:start">CMS GC优化选项</h6> <table border="1" cellspacing="0" class="table table-bordered table-hover"> <tbody> <tr> <th style="background-color:#dadada">标识</th> <th style="background-color:#dadada">描述</th> </tr> <tr> <td>-XX:+ UseCMSInitiating \ OccupancyOnly</td> <td>表示您希望仅将占用情况用作启动CMS<span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff"><strong>回收</strong></span></span></span>操作的标准。</td> </tr> <tr> <td>-XX:CMSInitiating \ OccupancyFraction = 70</td> <td>设置CMS生成占有率以启动CMS<span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff"><strong>回收</strong></span></span></span>周期。</td> </tr> <tr> <td>-XX:CMSTriggerRatio = 70</td> <td>这是在<code>MinHeapFreeRatio</code>CMS周期开始之前分配的CMS生成的百分比。</td> </tr> <tr> <td>-XX:CMSTriggerPermRatio = 90</td> <td>设置<code>MinHeapFreeRatio</code>开始CMS<span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff"><strong>回收</strong></span></span></span>周期之前分配的CMS永久生成中的百分比。</td> </tr> <tr> <td>-XX:CMSWaitDuration = 2000</td> <td>使用该参数指定允许CMS等待年轻<span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff"><strong>回收</strong></span></span></span>的时间。</td> </tr> <tr> <td>-XX:+ UseParNewGC</td> <td>选择使用并行算法进行年轻空间<span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff"><strong>回收</strong></span></span></span>。</td> </tr> <tr> <td>-XX:+ CMSConcurrentMTEnabled</td> <td>为并发阶段启用多个线程。</td> </tr> <tr> <td>-XX:ConcGCThreads = 2</td> <td>设置用于并发阶段的并行线程的数量。</td> </tr> <tr> <td>-XX:ParallelGCThreads = 2</td> <td>设置您想要用于<em>停止世界</em>阶段的并行线程的数量。</td> </tr> <tr> <td>-XX:+ CMSIncrementalMode</td> <td>启用增量CMS(iCMS)模式。</td> </tr> <tr> <td>-XX:+ CMSClassUnloadingEnabled</td> <td>如果未启用,CMS将不会清除永久空间。</td> </tr> <tr> <td>-XX:+ ExplicitGCInvokes \并发</td> <td>这允许<code>System.gc()</code>触发并发回收而不是完整的垃圾回收周期。</td> </tr> </tbody> </table> <h4 style="margin-left:0px; margin-right:0px; text-align:start">串行垃圾回收</h4> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">该算法对老一代的年轻一代和<em>标记扫描压缩</em>使用<em>标记复制</em>。它适用于单个线程。执行时,它会冻结所有其他线程,直到垃圾</span></span></span>回收<span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">操作结束。</span></span></span></p> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">由于串行垃圾</span></span></span>回收<span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">的线程冻结性质,它只适用于非常小的程序。</span></span></span></p> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">要使用串行GC,请使用下面的JVM参数:</span></span></span></p> <pre> <code class="language-html">-XX:+UseSerialGC</code></pre> <h4 style="margin-left:0px; margin-right:0px; text-align:start">并行垃圾回收</h4> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">Simimar串行GC,它用于<code>mark-copy</code>年轻一代和<code>mark-sweep-compact</code>老一代。多个并发线程用于标记和复制/压缩阶段。您可以使用<code>-XX:ParallelGCThreads=N</code>选项配置线程数。</span></span></span></p> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">当您的主要目标是通过有效使用现有系统资源来提高吞吐量时,并行垃圾</span></span></span>回收<span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">器适用于多核机器。使用这种方法,GC循环时间可以大大减少。</span></span></span></p> <blockquote> <p>直到Java 8,我们已经看到并行GC作为默认垃圾<span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff"><strong>回收</strong></span></span></span>器。从Java 9开始,G1是32位和64位服务器配置上的默认垃圾回收器。- <a href="http://openjdk.java.net/jeps/248" rel="external nofollow" style="box-sizing:border-box; transition:all 0.1s ease-in-out; color:#3367d6; text-decoration:none; font-family:"varela round", "helvetica neue", Helvetica, Arial, sans-serif; font-size:16px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; orphans:2; text-align:start; text-transform:none; white-space:normal; widows:2; word-spacing:0px; -webkit-text-stroke-width:0px" target="_blank">JEP [248]</a></p> </blockquote> 要使用并行GC,请使用下面的JVM参数: <pre> <code class="language-html">-XX:+UseParallelGC</code></pre> <h4 style="margin-left:0px; margin-right:0px; text-align:start">G1垃圾<span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff"><strong>回收</strong></span></span></span></h4> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">G1(垃圾优先)垃圾<strong>回收</strong>器在Java 7中可用,旨在成为CMS<strong>回收</strong>器的长期替代品。G1<strong>回收</strong>器是一个并行的,并发的,逐步压缩的低暂停垃圾<strong>回收</strong>器。</span></span></span></p> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">这种方法涉及将内存堆分割成多个小区域(通常为2048)。每个地区都被标记为年轻一代(进一步分为伊甸园地区或幸存地区)或老一代。这样GC就可以避免一次<strong>回收</strong>整个堆,而是逐步处理问题。这意味着一次只考虑一小部分区域。<br /> <img alt="4" class="img-thumbnail" src="/assist/images/blog/7d93c5717c2e404fb4fd1cd62f8a1be0.png" /></span></span></span><br />  </p> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">G1会跟踪每个区域包含的实时数据量。该信息用于确定包含垃圾最多的区域; 所以他们首先被<strong>回收</strong>。这就是为什么它是名称<strong>垃圾优先</strong><strong>回收</strong>。</span></span></span></p> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">就像其他算法一样,不幸的是,压缩操作使用“ <em>停止世界”</em>方法进行。但根据设计目标,您可以为其设定特定的性能目标。您可以配置暂停持续时间,例如在任何给定秒内不超过10毫秒。Garbage-First GC将尽最大努力以高概率实现这一目标(但不能确定,由于OS级别的线程管理,这很难实时)。</span></span></span></p> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">如果您想在Java 7或Java 8机器中使用,请使用JVM参数如下:</span></span></span></p> <pre> <code class="language-html">-XX:+UseG1GC</code></pre> <h6 style="margin-left:0px; margin-right:0px; text-align:start">G1优化选项</h6> <table border="1" cellspacing="0" class="table table-bordered table-hover"> <tbody> <tr> <th style="background-color:#dadada">旗</th> <th style="background-color:#dadada">描述</th> </tr> <tr> <td>-XX:G1HeapRegionSize =16米</td> <td>堆区的大小。该值将是2的幂,可以从1MB到32MB。目标是基于最小的Java堆大小,大约有2048个区域。</td> </tr> <tr> <td>-XX:MaxGCPauseMillis = 200</td> <td>为所需的最大暂停时间设置一个目标值。默认值是200毫秒。指定的值不适应您的堆大小。</td> </tr> <tr> <td>-XX:G1ReservePercent = 5</td> <td>这决定了堆中的最小预留量。</td> </tr> <tr> <td>-XX:G1ConfidencePercent = 75</td> <td>这是置信度系数暂停预测启发式。</td> </tr> <tr> <td>-XX:GCPauseIntervalMillis = 200</td> <td>这是每个MMU的暂停间隔时间片,以毫秒为单位。</td> </tr> </tbody> </table> <h2 style="margin-left:0px; margin-right:0px; text-align:start">GC自定义选项</h2> <h4 style="margin-left:0px; margin-right:0px; text-align:start">GC配置标志</h4> <table border="1" cellspacing="0" class="table table-bordered table-hover"> <tbody> <tr> <th style="background-color:#dadada">标识</th> <th style="background-color:#dadada">描述</th> </tr> <tr> <td>-Xms2048m -Xmx3g</td> <td>设置初始和最大堆大小(年轻空间加上终身空间)。</td> </tr> <tr> <td>-XX:+ DisableExplicitGC</td> <td>这将导致JVM忽略应用程序的任何System.gc()方法调用。</td> </tr> <tr> <td>-XX:+ UseGCOverheadLimit</td> <td>这是用于限制在抛出OutOfMemory错误之前在垃圾回收中花费的时间的使用策略。</td> </tr> <tr> <td>-XX:GCTimeLimit = 95</td> <td>这限制了<code>OutOfMemory</code>抛出错误之前花在垃圾<span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff"><strong>回收</strong></span></span></span>上的时间比例。这用于<code>GCHeapFreeLimit</code>。</td> </tr> <tr> <td>-XX:GCHeapFreeLimit = 5</td> <td>这会在引发<code>OutOfMemory</code>错误之前设置完整垃圾回收之后可用空间的最小百分比。这用于<code>GCTimeLimit</code>。</td> </tr> <tr> <td>-XX:InitialHeapSize =3克</td> <td>设置初始堆大小(年轻空间加上终身空间)。</td> </tr> <tr> <td>-XX:MaxHeapSize =3克</td> <td>设置最大堆大小(年轻空间加上终身空间)。</td> </tr> <tr> <td>-XX:新尺寸=128米</td> <td>设置年轻空间的初始大小。</td> </tr> <tr> <td>-XX:MaxNewSize =128米</td> <td>设置年轻空间的最大尺寸。</td> </tr> <tr> <td>-XX:SurvivorRatio = 15</td> <td>将单个生存者空间的大小设置为Eden空间大小的一部分。</td> </tr> <tr> <td>-XX:PermSize =512米</td> <td>设置永久空间的初始大小。</td> </tr> <tr> <td>-XX:MaxPermSize参数=512米</td> <td>设置永久空间的最大尺寸。</td> </tr> <tr> <td>-Xss512k</td> <td>以字节为单位设置专用于每个线程的堆栈区域的大小。</td> </tr> </tbody> </table> <h4 style="margin-left:0px; margin-right:0px; text-align:start">GC日志记录标志</h4> <table border="1" cellspacing="0" class="table table-bordered table-hover"> <tbody> <tr> <th style="background-color:#dadada">标识</th> <th style="background-color:#dadada">描述</th> </tr> <tr> <td>-verbose:gc或-XX:+ PrintGC</td> <td>这将打印基本垃圾<span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff"><strong>回收</strong></span></span></span>信息。</td> </tr> <tr> <td>-XX:+ PrintGCDetails</td> <td>这将打印更详细的垃圾<span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff"><strong>回收</strong></span></span></span>信息。</td> </tr> <tr> <td>-XX:+ PrintGCTimeStamps</td> <td>您可以为每个垃圾<span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff"><strong>回收</strong></span></span></span>事件打印时间戳。秒是连续的并从JVM开始时间开始。</td> </tr> <tr> <td>-XX:+ PrintGCDateStamps</td> <td>您可以为每个垃圾<span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff"><strong>回收</strong></span></span></span>事件打印日期戳。</td> </tr> <tr> <td>-Xloggc:</td> <td>使用这个你可以重定向垃圾<span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff"><strong>回收</strong></span></span></span>输出到一个文件而不是控制台。</td> </tr> <tr> <td>-XX:+打印\ TenuringDistribution</td> <td>您可以在每个<span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff"><strong>回收</strong></span></span></span>周期后打印有关年轻空间的详细信息。</td> </tr> <tr> <td>-XX:+ PrintTLAB</td> <td>您可以使用此标志打印TLAB分配统计信息。</td> </tr> <tr> <td>-XX:+ PrintReferenceGC</td> <td>使用此标志,您可以在停止世界暂停期间打印参考处理的时间(即弱,软等等)。</td> </tr> <tr> <td>-XX:+ HEAPDUMP \ OnOutOfMemoryError</td> <td>这会在内存不足的情况下创建堆转储文件。</td> </tr> </tbody> </table> <h2 style="margin-left:0px; margin-right:0px; text-align:start">总结</h2> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">所以在这个<strong>java垃圾回收教程中</strong>,我们了解了以下内容 -</span></span></span></p> <ol> <li style="list-style-type:decimal">对象生命周期分为3个阶段,即对象创建,对象使用和对象破坏。</li> <li style="list-style-type:decimal">如何<code>mark-sweep</code>,<code>mark-sweep-compact</code>以及<code>mark-copy</code>机制炒菜锅。</li> <li style="list-style-type:decimal">不同的单线程和并发GC算法。</li> <li style="list-style-type:decimal">直到Java 8,并行GC是默认算法。</li> <li style="list-style-type:decimal">自java 9以来,G1已被设置为默认的GC算法。</li> <li style="list-style-type:decimal">此外,还有各种标志来控制垃圾<span style="color:#333333"><span style="font-family:"varela round","helvetica neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff"><strong>回收</strong></span></span></span>算法的行为并记录任何应用程序的有用信息。</li> </ol> <br /> <br /> <br /> <br /> <br />  
  • java编程基础数组ArrayList使用详解

    java编程基础数组ArrayList使用详解<p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">Java编程基础中的ArrayList用于存储动态大小的元素集合。与大小固定的数组相反,ArrayList会在添加新元素时自动增大其大小。</span></span></span></p> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">ArrayList是Java集合框架的一部分,它实现了Java的<code>List</code>接口。<img alt="list" class="img-thumbnail" src="/assist/images/blog/3a58de20c693483dba6ea4d6a42b7e2a.jpg" /></span></span></span><br /> <br />  </p> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">以下几点需要注意Java中的ArrayList -</span></span></span></p> <ul style="margin-left:30px; margin-right:0px"> <li> <p style="margin-left:0px; margin-right:0px">ArrayList是一个可调整大小的数组,也称为动态数组。它的尺寸越来越大,以适应新的元素,并在元素被移除时缩小尺寸。</p> </li> <li> <p style="margin-left:0px; margin-right:0px">ArrayList内部使用数组来存储元素。就像数组一样,它允许您通过索引来检索元素。</p> </li> <li> <p style="margin-left:0px; margin-right:0px">Java ArrayList允许重复值和空值。</p> </li> <li> <p style="margin-left:0px; margin-right:0px">Java ArrayList是一个有序的集合。它维护元素的插入顺序。</p> </li> <li> <p style="margin-left:0px; margin-right:0px">您不能创建原始类型,如一个ArrayList <code>int</code>,<code>chat</code>等你需要用盒装的类型,如<code>Integer</code>,<code>Character</code>,<code>Boolean</code>等。</p> </li> <li> <p style="margin-left:0px; margin-right:0px">Java ArrayList不同步。如果多个线程同时尝试修改ArrayList,那么最终结果将是非确定性的。如果多个线程要修改它,你必须显式同步对ArrayList的访问。</p> </li> </ul> <h2 style="margin-left:0px; margin-right:0px; text-align:start">创建一个ArrayList并添加新的元素</h2> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">这个例子显示:</span></span></span></p> <ul style="margin-left:30px; margin-right:0px"> <li>如何使用<a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#ArrayList--" rel="external nofollow" style="box-sizing:border-box; color:#419be8; text-decoration:none; word-wrap:break-word" target="_blank"><code>ArrayList()</code></a>构造函数创建ArrayList 。</li> <li>使用该<a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#add-E-" rel="external nofollow" style="box-sizing:border-box; color:#419be8; text-decoration:none; word-wrap:break-word" target="_blank"><code>add()</code></a>方法向ArrayList添加新元素。</li> </ul> <pre> <code class="language-java">import java.util.ArrayList; import java.util.List; public class CreateArrayListExample { public static void main(String[] args) { // Creating an ArrayList of String List<String> animals = new ArrayList<>(); // Adding new elements to the ArrayList animals.add("Lion"); animals.add("Tiger"); animals.add("Cat"); animals.add("Dog"); System.out.println(animals); // Adding an element at a particular index in an ArrayList animals.add(2, "Elephant"); System.out.println(animals); } }</code></pre> <p style="margin-left:0px; margin-right:0px; text-align:start"><br /> <span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">输出</span></span></span></p> <pre> <code class="language-html">[Lion, Tiger, Cat, Dog] [Lion, Tiger, Elephant, Cat, Dog]</code></pre> <h2 style="margin-left:0px; margin-right:0px; text-align:start">从另一个集合创建一个ArrayList</h2> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">这个例子显示:</span></span></span></p> <ul style="margin-left:30px; margin-right:0px"> <li> <p style="margin-left:0px; margin-right:0px">如何使用<a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#ArrayList-java.util.Collection-" rel="external nofollow" style="box-sizing:border-box; color:#419be8; text-decoration:none; word-wrap:break-word" target="_blank"><code>ArrayList(Collection c)</code></a>构造函数从另一个ArrayList创建一个ArrayList 。</p> </li> <li> <p style="margin-left:0px; margin-right:0px">如何使用该<a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#addAll-java.util.Collection-" rel="external nofollow" style="box-sizing:border-box; color:#419be8; text-decoration:none; word-wrap:break-word" target="_blank"><code>addAll()</code></a>方法将现有ArrayList中的所有元素添加到新的ArrayList中。</p> <pre> <code class="language-java">import java.util.ArrayList; import java.util.List; public class CreateArrayListFromCollectionExample { public static void main(String[] args) { List<Integer> firstFivePrimeNumbers = new ArrayList<>(); firstFivePrimeNumbers.add(2); firstFivePrimeNumbers.add(3); firstFivePrimeNumbers.add(5); firstFivePrimeNumbers.add(7); firstFivePrimeNumbers.add(11); // Creating an ArrayList from another collection List<Integer> firstTenPrimeNumbers = new ArrayList<>(firstFivePrimeNumbers); List<Integer> nextFivePrimeNumbers = new ArrayList<>(); nextFivePrimeNumbers.add(13); nextFivePrimeNumbers.add(17); nextFivePrimeNumbers.add(19); nextFivePrimeNumbers.add(23); nextFivePrimeNumbers.add(29); // Adding an entire collection to an ArrayList firstTenPrimeNumbers.addAll(nextFivePrimeNumbers); System.out.println(firstTenPrimeNumbers); } }</code></pre> </li> </ul> <p style="margin-left:0px; margin-right:0px; text-align:start"><br /> <span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">输出:</span></span></span></p> <pre> <code class="language-html">[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]</code></pre> <h2 style="margin-left:0px; margin-right:0px; text-align:start">访问ArrayList中的元素</h2> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">这个例子显示:</span></span></span></p> <ul style="margin-left:30px; margin-right:0px"> <li>如何使用该<a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#isEmpty--" rel="external nofollow" style="box-sizing:border-box; color:#419be8; text-decoration:none; word-wrap:break-word" target="_blank"><code>isEmpty()</code></a>方法检查ArrayList是否为空。</li> <li>如何使用该<a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#size--" rel="external nofollow" style="box-sizing:border-box; color:#419be8; text-decoration:none; word-wrap:break-word" target="_blank"><code>size()</code></a>方法找到ArrayList的大小。</li> <li>如何使用该<a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#get-int-" rel="external nofollow" style="box-sizing:border-box; color:#419be8; text-decoration:none; word-wrap:break-word" target="_blank"><code>get()</code></a>方法访问ArrayList中特定索引处的元素。</li> <li>如何使用该<a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#set-int-E-" rel="external nofollow" style="box-sizing:border-box; color:#419be8; text-decoration:none; word-wrap:break-word" target="_blank"><code>set()</code></a>方法修改ArrayList中特定索引处的元素。</li> </ul> <pre> <code class="language-java">import java.util.ArrayList; import java.util.List; public class AccessElementsFromArrayListExample { public static void main(String[] args) { List<String> topCompanies = new ArrayList<>(); // Check is an ArrayList is empty System.out.println("Is the topCompanies list empty? : " + topCompanies.isEmpty()); topCompanies.add("Google"); topCompanies.add("Apple"); topCompanies.add("Microsoft"); topCompanies.add("Amazon"); topCompanies.add("Facebook"); // Find the size of an ArrayList System.out.println("Here are the top " + topCompanies.size() + " companies in the world"); System.out.println(topCompanies); // Retrieve the element at a given index String bestCompany = topCompanies.get(0); String secondBestCompany = topCompanies.get(1); String lastCompany = topCompanies.get(topCompanies.size() - 1); System.out.println("Best Company: " + bestCompany); System.out.println("Second Best Company: " + secondBestCompany); System.out.println("Last Company in the list: " + lastCompany); // Modify the element at a given index topCompanies.set(4, "Walmart"); System.out.println("Modified top companies list: " + topCompanies); } }</code></pre> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">输出:</span></span></span></p> <pre> <code class="language-html">Is the topCompanies list empty? : true Here are the top 5 companies in the world [Google, Apple, Microsoft, Amazon, Facebook] Best Company: Google Second Best Company: Apple Last Company in the list: Facebook Modified top companies list: [Google, Apple, Microsoft, Amazon, Walmart]</code></pre> <h2 style="margin-left:0px; margin-right:0px; text-align:start">从ArrayList中移除元素</h2> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">这个例子显示:</span></span></span></p> <ol style="margin-left:30px; margin-right:0px"> <li> <p style="margin-left:0px; margin-right:0px">如何删除ArrayList |中给定索引处的元素 <a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#remove-java.lang.Object-" rel="external nofollow" style="box-sizing:border-box; color:#419be8; text-decoration:none; word-wrap:break-word" target="_blank">delete</a><a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#remove-int-" rel="external nofollow" style="box-sizing:border-box; color:#419be8; text-decoration:none; word-wrap:break-word" target="_blank">(int索引)</a></p> </li> <li> <p style="margin-left:0px; margin-right:0px">如何从ArrayList |中删除元素 <a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#remove-java.lang.Object-" rel="external nofollow" style="box-sizing:border-box; color:#419be8; text-decoration:none; word-wrap:break-word" target="_blank">delete(Object o)</a></p> </li> <li> <p style="margin-left:0px; margin-right:0px">如何从给定集合中存在的ArrayList中移除所有元素| <a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#remove-java.lang.Object-" rel="external nofollow" style="box-sizing:border-box; color:#419be8; text-decoration:none; word-wrap:break-word" target="_blank">delete</a>All<a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#removeAll-java.util.Collection-" rel="external nofollow" style="box-sizing:border-box; color:#419be8; text-decoration:none; word-wrap:break-word" target="_blank">()</a></p> </li> <li> <p style="margin-left:0px; margin-right:0px">如何删除所有匹配给定谓词|的元素 <a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#removeIf-java.util.function.Predicate-" rel="external nofollow" style="box-sizing:border-box; color:#419be8; text-decoration:none; word-wrap:break-word" target="_blank">removeIf()</a></p> </li> <li> <p style="margin-left:0px; margin-right:0px">如何清除ArrayList | <a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#clear--" rel="external nofollow" style="box-sizing:border-box; color:#419be8; text-decoration:none; word-wrap:break-word" target="_blank">clear()</a></p> </li> </ol> <pre> <code class="language-java">import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; public class RemoveElementsFromArrayListExample { public static void main(String[] args) { List<String> programmingLanguages = new ArrayList<>(); programmingLanguages.add("C"); programmingLanguages.add("C++"); programmingLanguages.add("Java"); programmingLanguages.add("Kotlin"); programmingLanguages.add("Python"); programmingLanguages.add("Perl"); programmingLanguages.add("Ruby"); System.out.println("Initial List: " + programmingLanguages); // Remove the element at index `5` programmingLanguages.remove(5); System.out.println("After remove(5): " + programmingLanguages); // Remove the element "Kotlin" (The remove() method returns false if the element does not exist in the ArrayList) boolean isRemoved = programmingLanguages.remove("Kotlin"); System.out.println("After remove(\"Kotlin\"): " + programmingLanguages); // Remove all the elements belonging to the collection scriptingLanguages List<String> scriptingLanguages = new ArrayList<>(); scriptingLanguages.add("Python"); scriptingLanguages.add("Ruby"); scriptingLanguages.add("Perl"); programmingLanguages.removeAll(scriptingLanguages); System.out.println("After removeAll(scriptingLanguages): " + programmingLanguages); // Remove all the elements that satisfy the given predicate programmingLanguages.removeIf(new Predicate<String>() { @Override public boolean test(String s) { return s.startsWith("C"); } }); /* The above removeIf() call can also be written using lambda expression like this - programmingLanguages.removeIf(s -> s.startsWith("C")) */ System.out.println("After Removing all elements that start with \"C\": " + programmingLanguages); // Remove all elements from the ArrayList programmingLanguages.clear(); System.out.println("After clear(): " + programmingLanguages); } }</code></pre> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">输出:</span></span></span></p> <pre> <code class="language-html">Initial List: [C, C++, Java, Kotlin, Python, Perl, Ruby] After remove(5): [C, C++, Java, Kotlin, Python, Ruby] After remove("Kotlin"): [C, C++, Java, Python, Ruby] After removeAll(scriptingLanguages): [C, C++, Java] After Removing all elements that start with "C": [Java] After clear(): []</code></pre> <h2 style="margin-left:0px; margin-right:0px; text-align:start">迭代ArrayList</h2> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">以下示例显示如何使用迭代遍历ArrayList</span></span></span></p> <ol style="margin-left:30px; margin-right:0px"> <li>Java 8 <a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#forEach-java.util.function.Consumer-" rel="external nofollow" style="box-sizing:border-box; color:#419be8; text-decoration:none; word-wrap:break-word" target="_blank"><code>forEach</code></a>循环。</li> <li><a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#iterator--" rel="external nofollow" style="box-sizing:border-box; color:#419be8; text-decoration:none; word-wrap:break-word" target="_blank"><code>iterator()</code></a>。</li> <li><a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#iterator--" rel="external nofollow" style="box-sizing:border-box; color:#419be8; text-decoration:none; word-wrap:break-word" target="_blank"><code>iterator()</code></a>和Java 8 <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html#forEachRemaining-java.util.function.Consumer-" rel="external nofollow" style="box-sizing:border-box; color:#419be8; text-decoration:none; word-wrap:break-word" target="_blank">forEachRemaining()</a>方法。</li> <li><a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#listIterator-int-" rel="external nofollow" style="box-sizing:border-box; color:#419be8; text-decoration:none; word-wrap:break-word" target="_blank"><code>listIterator()</code></a>。</li> <li>每个循环都很简单。</li> <li>用索引循环。</li> </ol> <pre> <code class="language-java">import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.ListIterator; public class IterateOverArrayListExample { public static void main(String[] args) { List<String> tvShows = new ArrayList<>(); tvShows.add("Breaking Bad"); tvShows.add("Game Of Thrones"); tvShows.add("Friends"); tvShows.add("Prison break"); System.out.println("=== Iterate using Java 8 forEach loop ==="); tvShows.forEach(tvShow -> { System.out.println(tvShow); }); System.out.println("\n=== Iterate using an iterator() ==="); Iterator<String> tvShowIterator = tvShows.iterator(); while (tvShowIterator.hasNext()) { String tvShow = tvShowIterator.next(); System.out.println(tvShow); } System.out.println("\n=== Iterate using an iterator() and Java 8 forEachRemaining() method ==="); tvShowIterator = tvShows.iterator(); tvShowIterator.forEachRemaining(tvShow -> { System.out.println(tvShow); }); System.out.println("\n=== Iterate using a listIterator() to traverse in both directions ==="); // Here, we start from the end of the list and traverse backwards. ListIterator<String> tvShowListIterator = tvShows.listIterator(tvShows.size()); while (tvShowListIterator.hasPrevious()) { String tvShow = tvShowListIterator.previous(); System.out.println(tvShow); } System.out.println("\n=== Iterate using simple for-each loop ==="); for(String tvShow: tvShows) { System.out.println(tvShow); } System.out.println("\n=== Iterate using for loop with index ==="); for(int i = 0; i < tvShows.size(); i++) { System.out.println(tvShows.get(i)); } } }</code></pre> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">输出:</span></span></span></p> <pre> <code class="language-html">=== Iterate using Java 8 forEach loop === Breaking Bad Game Of Thrones Friends Prison break === Iterate using an iterator() === Breaking Bad Game Of Thrones Friends Prison break === Iterate using an iterator() and Java 8 forEachRemaining() method === Breaking Bad Game Of Thrones Friends Prison break === Iterate using a listIterator() to traverse in both directions === Prison break Friends Game Of Thrones Breaking Bad === Iterate using simple for-each loop === Breaking Bad Game Of Thrones Friends Prison break === Iterate using for loop with index === Breaking Bad Game Of Thrones Friends Prison break</code></pre> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">在遍历期间需要修改ArrayList时,<code>iterator()</code>和<code>listIterator()</code>方法很有用。</span></span></span></p> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">考虑下面的例子,在<code>iterator.remove()</code>遍历它的时候我们使用方法从ArrayList中移除元素-</span></span></span></p> <pre> <code class="language-java">import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class ArrayListIteratorRemoveExample { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(); numbers.add(13); numbers.add(18); numbers.add(25); numbers.add(40); Iterator<Integer> numbersIterator = numbers.iterator(); while (numbersIterator.hasNext()) { Integer num = numbersIterator.next(); if(num % 2 != 0) { numbersIterator.remove(); } } System.out.println(numbers); } }</code></pre> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">输出:</span></span></span></p> <pre> <code class="language-html">[18, 40]</code></pre> <h2 style="margin-left:0px; margin-right:0px; text-align:start">在ArrayList中搜索元素</h2> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">下面的例子显示了如何:</span></span></span></p> <ul style="margin-left:30px; margin-right:0px"> <li> <p style="margin-left:0px; margin-right:0px">检查ArrayList是否包含给定的元素|  <a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#contains-java.lang.Object-" rel="external nofollow" target="_blank">contains</a><a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#contains-java.lang.Object-" rel="external nofollow" style="box-sizing:border-box; color:#419be8; text-decoration:none; word-wrap:break-word" target="_blank">()</a></p> </li> <li> <p style="margin-left:0px; margin-right:0px">查找ArrayList |中第一次出现元素的索引 <a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#indexOf-java.lang.Object-" rel="external nofollow" target="_blank">indexOf</a><a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#indexOf-java.lang.Object-" rel="external nofollow" style="box-sizing:border-box; color:#419be8; text-decoration:none; word-wrap:break-word" target="_blank">()</a></p> </li> <li> <p style="margin-left:0px; margin-right:0px">查找ArrayList |中最后一次出现元素的索引 <a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#lastIndexOf-java.lang.Object-" rel="external nofollow" style="box-sizing:border-box; color:#419be8; text-decoration:none; word-wrap:break-word" target="_blank">lastIndexOf()</a></p> </li> </ul> <pre> <code class="language-java">import java.util.ArrayList; import java.util.List; public class SearchElementsInArrayListExample { public static void main(String[] args) { List<String> names = new ArrayList<>(); names.add("John"); names.add("Alice"); names.add("Bob"); names.add("Steve"); names.add("John"); names.add("Steve"); names.add("Maria"); // Check if an ArrayList contains a given element System.out.println("Does names array contain \"Bob\"? : " + names.contains("Bob")); // Find the index of first occurrence of an element in an ArrayList System.out.println("indexOf \"Steve\": " + names.indexOf("Steve")); System.out.println("indexOf \"Mark\": " + names.indexOf("Mark")); // Find the index of the last occurrence of an element in an ArrayList System.out.println("lastIndexOf \"John\" : " + names.lastIndexOf("John")); System.out.println("lastIndexOf \"Bill\" : " + names.lastIndexOf("Bill")); } }</code></pre> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">输出:</span></span></span></p> <pre> <code class="language-html">Does names array contain "Bob"? : true indexOf "Steve": 3 indexOf "Mark": -1 lastIndexOf "John" : 4 lastIndexOf "Bill" : -1</code></pre> <h2 style="margin-left:0px; margin-right:0px; text-align:start">用户定义对象的ArrayList</h2> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">由于ArrayList支持泛型,因此可以创建<strong>任何</strong>类型的ArrayList 。它可以是简单的类型,如<code>Integer</code>,<code>String</code>,<code>Double</code>或复杂类型等的ArrayLists的ArrayList,或包含HashMap的ArrayList或任何用户定义的对象的ArrayList。</span></span></span></p> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">在以下示例中,您将学习如何创建用户定义对象的ArrayList。</span></span></span></p> <pre> <code class="language-java">import java.util.ArrayList; import java.util.List; class User { private String name; private int age; public User(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } public class ArrayListUserDefinedObjectExample { public static void main(String[] args) { List<User> users = new ArrayList<>(); users.add(new User("Rajeev", 25)); users.add(new User("John", 34)); users.add(new User("Steve", 29)); users.forEach(user -> { System.out.println("Name : " + user.getName() + ", Age : " + user.getAge()); }); } }</code></pre> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">输出:</span></span></span></p> <pre> <code class="language-html">Name : Rajeev, Age : 25 Name : John, Age : 34 Name : Steve, Age : 29</code></pre> <h2 style="margin-left:0px; margin-right:0px; text-align:start">排序ArrayList</h2> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">对ArrayList进行排序是您在程序中遇到的一个非常常见的任务。在本节中,我会告诉你如何 -</span></span></span></p> <ul style="margin-left:30px; margin-right:0px"> <li>使用<a href="https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#sort-java.util.List-" rel="external nofollow" style="box-sizing:border-box; color:#419be8; text-decoration:none; word-wrap:break-word" target="_blank"><code>Collections.sort()</code></a>方法对ArrayList进行排序。</li> <li>使用<a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#sort-java.util.Comparator-" rel="external nofollow" style="box-sizing:border-box; color:#419be8; text-decoration:none; word-wrap:break-word" target="_blank"><code>ArrayList.sort()</code></a>方法对ArrayList进行排序。</li> <li>用自定义比较器对用户定义对象的ArrayList进行排序。</li> </ul> <h4 style="margin-left:0px; margin-right:0px; text-align:start">1.使用Collections.sort()方法对ArrayList进行排序</h4> <pre> <code class="language-java">import java.util.ArrayList; import java.util.Collections; import java.util.List; public class ArrayListCollectionsSortExample { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(); numbers.add(13); numbers.add(7); numbers.add(18); numbers.add(5); numbers.add(2); System.out.println("Before : " + numbers); // Sorting an ArrayList using Collections.sort() method Collections.sort(numbers); System.out.println("After : " + numbers); } }</code></pre> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">输出:</span></span></span></p> <pre> <code class="language-html">Before : [13, 7, 18, 5, 2] After : [2, 5, 7, 13, 18]</code></pre> <p style="margin-left:0px; margin-right:0px; text-align:start">2.使用ArrayList.sort()方法对ArrayList排序<br />  </p> <pre> <code class="language-java">import java.util.ArrayList; import java.util.Comparator; import java.util.List; public class ArrayListSortExample { public static void main(String[] args) { List<String> names = new ArrayList<>(); names.add("Lisa"); names.add("Jennifer"); names.add("Mark"); names.add("David"); System.out.println("Names : " + names); // Sort an ArrayList using its sort() method. You must pass a Comparator to the ArrayList.sort() method. names.sort(new Comparator<String>() { @Override public int compare(String name1, String name2) { return name1.compareTo(name2); } }); // The above `sort()` method call can also be written simply using lambda expressions names.sort((name1, name2) -> name1.compareTo(name2)); // Following is an even more concise solution names.sort(Comparator.naturalOrder()); System.out.println("Sorted Names : " + names); } }</code></pre> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">输出:</span></span></span></p> <pre> <code class="language-html">Names : [Lisa, Jennifer, Mark, David] Sorted Names : [David, Jennifer, Lisa, Mark]</code></pre> <p style="margin-left:0px; margin-right:0px; text-align:start">3.使用自定义比较器对对象的ArrayList进行排序<br />  </p> <pre> <code class="language-java">import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; class Person { private String name; private Integer age; public Person(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "{" + "name='" + name + '\'' + ", age=" + age + '}'; } } public class ArrayListObjectSortExample { public static void main(String[] args) { List<Person> people = new ArrayList<>(); people.add(new Person("Sachin", 47)); people.add(new Person("Chris", 34)); people.add(new Person("Rajeev", 25)); people.add(new Person("David", 31)); System.out.println("Person List : " + people); // Sort People by their Age people.sort((person1, person2) -> { return person1.getAge() - person2.getAge(); }); // A more concise way of writing the above sorting function people.sort(Comparator.comparingInt(Person::getAge)); System.out.println("Sorted Person List by Age : " + people); // You can also sort using Collections.sort() method by passing the custom Comparator Collections.sort(people, Comparator.comparing(Person::getName)); System.out.println("Sorted Person List by Name : " + people); } }</code></pre> 输出: <pre> <code class="language-html">Person List : [{name='Sachin', age=47}, {name='Chris', age=34}, {name='Rajeev', age=25}, {name='David', age=31}] Sorted Person List by Age : [{name='Rajeev', age=25}, {name='David', age=31}, {name='Chris', age=34}, {name='Sachin', age=47}] Sorted Person List by Name : [{name='Chris', age=34}, {name='David', age=31}, {name='Rajeev', age=25}, {name='Sachin', age=47}]</code></pre>   <h2 style="margin-left:0px; margin-right:0px; text-align:start">同步对ArrayList的访问</h2> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">ArrayList类不同步。如果多个线程同时尝试修改一个ArrayList,那么最终的结果变得不确定,因为一个线程可能会覆盖另一个线程所做的更改。</span></span></span></p> <h4 style="margin-left:0px; margin-right:0px; text-align:start">示例演示ArrayList在多线程环境中的不可预知行为</h4> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">以下示例显示了当多个线程同时尝试修改ArrayList时发生的情况。</span></span></span></p> <pre> <code class="language-java">import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class UnsafeArrayListExample { public static void main(String[] args) throws InterruptedException { List<Integer> unsafeArrayList = new ArrayList<>(); unsafeArrayList.add(1); unsafeArrayList.add(2); unsafeArrayList.add(3); // Create a thread pool of size 10 ExecutorService executorService = Executors.newFixedThreadPool(10); // Create a Runnable task that increments the each element of the ArrayList by one Runnable task = () -> { incrementArrayList(unsafeArrayList); }; // Submit the task to the executor service 100 times. // All the tasks will modify the ArrayList concurrently for(int i = 0; i < 100; i++) { executorService.submit(task); } executorService.shutdown(); executorService.awaitTermination(60, TimeUnit.SECONDS); System.out.println(unsafeArrayList); } // Increment all the values in the ArrayList by one private static void incrementArrayList(List<Integer> unsafeArrayList) { for(int i = 0; i < unsafeArrayList.size(); i++) { Integer value = unsafeArrayList.get(i); unsafeArrayList.set(i, value + 1); } } }</code></pre> 上述程序的最终输出应该相等,<code>[101, 102, 103]</code>因为我们将ArrayList中的值增加了100次。但是如果你运行这个程序,它会在每次运行时产生不同的输出 -<br /> 输出: <pre> <code class="language-html">[96, 96, 98]</code></pre>   <h4 style="margin-left:0px; margin-right:0px; text-align:start">演示如何将并发修改同步到ArrayList的示例</h4> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">好吧!现在让我们看看我们如何<code>ArrayList</code>在多线程环境中同步访问。</span></span></span></p> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">以下示例显示了上一个示例的同步版本。与以前的程序不同,该程序的输出是确定性的,并且始终是相同的。</span></span></span></p> <pre> <code class="language-java">import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class SynchronizedArrayListExample { public static void main(String[] args) throws InterruptedException { List<Integer> safeArrayList = Collections.synchronizedList(new ArrayList<>()); safeArrayList.add(1); safeArrayList.add(2); safeArrayList.add(3); // Create a thread pool of size 10 ExecutorService executorService = Executors.newFixedThreadPool(10); // Create a Runnable task that increments the each element of the ArrayList by one Runnable task = () -> { incrementArrayList(safeArrayList); }; // Submit the task to the executor service 100 times. // All the tasks will modify the ArrayList concurrently for(int i = 0; i < 100; i++) { executorService.submit(task); } executorService.shutdown(); executorService.awaitTermination(60, TimeUnit.SECONDS); System.out.println(safeArrayList); } // Increment all the values in the ArrayList by one private static void incrementArrayList(List<Integer> safeArrayList) { synchronized (safeArrayList) { for (int i = 0; i < safeArrayList.size(); i++) { Integer value = safeArrayList.get(i); safeArrayList.set(i, value + 1); } } } }</code></pre> 输出: <pre> <code class="language-html">[101, 102, 103]</code></pre> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">以上示例使用<a href="https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#synchronizedList-java.util.List-" rel="external nofollow" style="box-sizing:border-box; color:#419be8; text-decoration:none; word-wrap:break-word" target="_blank"><code>Collections.synchronizedList()</code></a>方法来获取ArrayList的同步视图。</span></span></span></p> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff">而且,对<code>incrementArrayList()</code>方法内的ArrayList的修改被封装在一个<code>synchronized</code>块中。这确保了两个线程不能同时增加ArrayList元素。</span></span></span></p> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="color:rgba(0, 0, 0, 0.87)"><span style="font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif"><span style="background-color:#ffffff"><em><a href="https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CopyOnWriteArrayList.html" rel="external nofollow" style="box-sizing:border-box; color:#419be8; text-decoration:none; word-wrap:break-word" target="_blank"><code>CopyOnWriteArrayList</code></a>如果你需要线程安全,你也可以使用。它是ArrayList类的线程安全版本。它通过创建ArrayList的新副本来实现所有的变异操作。</em></span></span></span></p>
  • Java编程软件有哪些_常用Java编程软件下载和安装

    Java编程软件有哪些?常用Java编程软件下载、安装和使用说明Java编程软件下载和安装_Java编程常用软件
  • Java MongoDB驱动程序

    Java MongoDB驱动程序,下载/升级,Java驱动程序兼容性,第三方框架和库<h1>Java MongoDB驱动程序</h1> <p>在这个页面上</p> <ul> <li><a href="https://docs.mongodb.com/ecosystem/drivers/java/#driver-features" id="id3" rel="nofollow" target="_blank">驱动程序特点</a></li> <li><a href="https://docs.mongodb.com/ecosystem/drivers/java/#download-upgrade" id="id4" rel="nofollow" target="_blank">下载/升级</a></li> <li><a href="https://docs.mongodb.com/ecosystem/drivers/java/#java-driver-compatibility" id="id5" rel="nofollow" target="_blank">Java驱动程序兼容性</a></li> <li><a href="https://docs.mongodb.com/ecosystem/drivers/java/#third-party-frameworks-and-libraries" id="id6" rel="nofollow" target="_blank">第三方框架和图书馆</a></li> <li><a href="https://docs.mongodb.com/ecosystem/drivers/java/#additional-resources" id="id7" rel="nofollow" target="_blank">其他资源</a></li> </ul> <p>从3.0版本开始,官方MongoDB Java驱动程序提供与MongoDB的同步和异步交互。有关官方MongoDB Java Driver参考资料,请参阅:</p> <ul> <li><a href="http://mongodb.github.io/mongo-java-driver/" rel="nofollow" target="_blank">MongoDB Java驱动程序文档</a></li> <li><a href="http://api.mongodb.com/java/current" rel="nofollow" target="_blank">MongoDB Java驱动程序API文档</a></li> </ul> <h2>驱动程序特性</h2> MongoDB驱动<br /> 一个更新的Java驱动程序,其中包括传统的API以及符合新的跨驱动程序CRUD规范的新的通用MongoCollection界面。有关Java驱动程序的文档,包括入门指南,请参阅<a href="http://mongodb.github.io/mongo-java-driver/3.2/driver/" rel="nofollow" target="_blank">Java驱动程序文档</a>。<br /> MongoDB异步驱动<br /> 一种新的异步API,可以利用Netty或Java 7的AsynchronousSocketChannel快速和非阻塞IO。有关Async Java驱动程序(包括入门指南)的<a href="http://mongodb.github.io/mongo-java-driver/3.2/driver-async/" rel="nofollow" target="_blank">文档</a>,请参阅<a href="http://mongodb.github.io/mongo-java-driver/3.2/driver-async/" rel="nofollow" target="_blank">Async Java驱动程序文档</a>。<br /> BSON图书馆<br /> 具有新型编解码器基础架构的独立BSON库,可用于构建高性能编码器和解码器,无需中间地图实例。有关BSON Library的文档,请参阅<a href="http://mongodb.github.io/mongo-java-driver/3.2/bson/" rel="nofollow" target="_blank">BSON Library</a>。<br /> 核心驱动<br /> 一个新的核心库,MongoDB驱动程序和异步驱动程序都在其上构建。用户可以使用新的核心库构建替代或实验高级API。 <h2>下载/升级</h2> <p>将驱动程序合并到项目中的推荐方法是使用依赖关系管理系统。有关更多信息,请参阅 <a href="http://mongodb.github.io/mongo-java-driver/" rel="nofollow" target="_blank">MongoDB Java驱动程序</a>。</p> <p>如果从较早版本的Java驱动程序升级,请参阅 <a href="http://mongodb.github.io/mongo-java-driver/3.2/whats-new/upgrading/" rel="nofollow" target="_blank">最新消息</a>。</p> <h2>Java驱动程序兼容性</h2> <h3>MongoDB兼容性</h3> <p>以下兼容性表格指定了与特定版本的MongoDB一起使用的MongoDB Java驱动程序的推荐版本。</p> <p>第一列列出了驱动程序版本。</p> <table border="1"> <thead> <tr> <th>Java驱动程序版本</th> <th>MongoDB 2.4</th> <th>MongoDB 2.6</th> <th>MongoDB 3.0</th> <th>MongoDB 3.2</th> <th>MongoDB 3.4</th> </tr> </thead> <tbody> <tr> <th>版本3.4</th> <td>✓</td> <td>✓</td> <td>✓</td> <td>✓</td> <td>✓</td> </tr> <tr> <th>版本3.3</th> <td>✓</td> <td>✓</td> <td>✓</td> <td>✓</td> <td> </td> </tr> <tr> <th>版本3.2</th> <td>✓</td> <td>✓</td> <td>✓</td> <td>✓</td> <td> </td> </tr> <tr> <th>版本2.14</th> <td>✓</td> <td>✓</td> <td>✓</td> <td>✓<a href="https://docs.mongodb.com/ecosystem/drivers/java/#id2" id="id1" rel="nofollow">[*]</a></td> <td> </td> </tr> </tbody> </table> <p>有关其他驱动程序版本,请参阅<a href="https://docs.mongodb.com/ecosystem/drivers/driver-compatibility-reference/#reference-compatibility-mongodb-java" rel="nofollow" target="_blank">Java驱动程序MongoDB兼容性参考</a>。</p> <p>该驱动程序不支持旧版本的MongoDB。</p> <table id="id2"> <tbody> <tr> <td><a href="https://docs.mongodb.com/ecosystem/drivers/java/#id1" rel="nofollow" target="_blank">[*]</a></td> <td>2.14驱动程序不支持所有MongoDB 3.2功能(例如,阅读关注); 但是,如果您当前使用的是2.x版本的驱动程序,并且希望针对MongoDB 3.2运行,但无法升级到驱动程序版本3.2,请使用2.14驱动程序。</td> </tr> </tbody> </table> <h3>语言兼容性</h3> <p>以下兼容性表格指定了与特定版本的Java一起使用的MongoDB Java驱动程序的推荐版本。</p> <p>第一列列出了驱动程序版本。</p> <table border="1"> <thead> <tr> <th>Java驱动程序版本</th> <th>Java 5</th> <th>Java 6</th> <th>Java 7</th> <th>Java 8</th> </tr> </thead> <tbody> <tr> <th>版本3.x</th> <td> </td> <td>✓</td> <td>✓</td> <td>✓</td> </tr> <tr> <th>版本2.x</th> <td>✓</td> <td>✓</td> <td>✓</td> <td>✓</td> </tr> </tbody> </table> <p>有关其他驱动程序版本,请参阅<a href="https://docs.mongodb.com/ecosystem/drivers/driver-compatibility-reference/#reference-compatibility-language-java" rel="nofollow" target="_blank">Java驱动程序语言兼容性参考</a>。</p> <h2>第三方框架和库</h2> <h3>POJO贴片</h3> <ul> <li><a href="https://github.com/mongodb/morphia" rel="nofollow" target="_blank">Morphia</a>。带有DAO / Datastore抽象的Type-Safe Wrapper。</li> <li><a href="http://www.springsource.org/spring-data/mongodb" rel="nofollow" target="_blank">Spring MongoDB</a>。为Spring用户提供熟悉的数据访问功能,包括丰富的POJO映射。</li> <li><a href="http://sboesebeck.github.io/morphium/" rel="nofollow" target="_blank">形态</a>。功能丰富的POJO Mapper包括声明缓存,集群感知,验证,部分更新等功能,支持聚合框架。</li> <li><a href="http://github.com/jannehietamaki/mungbean" rel="nofollow" target="_blank">绿豆</a>(w / clojure支持)。</li> <li><a href="http://www.datanucleus.org/products/accessplatform_3_0/mongodb/support.html" rel="nofollow" target="_blank">DataNucleus JPA / JDO</a>。JPA / JDO包装器</li> <li><a href="https://github.com/dadastream/lib-mongomapper" rel="nofollow" target="_blank">LIB-mongomapper</a>。JavaBean Mapper(无注释)。</li> <li><a href="http://mongojack.org/" rel="nofollow" target="_blank">MongoJack</a>。使用杰克逊(注释)来映射到POJO或从POJO映射,并有一个简单的包装<code>DBCollection</code>。</li> <li><a href="http://kundera.googlecode.com/" rel="nofollow" target="_blank">昆德拉</a>。JPA兼容ORM。适用于多个数据存储。</li> <li><a href="http://dbuschman7.github.io/mongoFS/" rel="nofollow" target="_blank">MongoFS</a>。增强的文件存储库,支持文件压缩,加密和Zip文件扩展。可以在GridFS兼容的桶上使用。</li> <li><a href="http://www.jongo.org/" rel="nofollow" target="_blank">琼戈</a>。在Java中查询<code>mongo</code>(在shell中使用字符串),将结果解组成Java对象(使用Jackson)</li> <li><a href="http://mongolink.org/" rel="nofollow" target="_blank">MongoLink</a>。对象文档映射器(ODM。)使用简单的java DSL进行映射声明。</li> <li><a href="http://www.hibernate.org/ogm/" rel="nofollow" target="_blank">休眠OGM</a>。为MongoDB提供Java持久性支持。</li> <li><a href="http://github.com/ParaPenguin/morphix" rel="nofollow" target="_blank">Morphix</a>。轻量级,易于使用的POJO映射器,具有对象缓存和生命周期方法。</li> </ul> <h3>代码生成</h3> <ul> <li><a href="http://java.dzone.com/articles/using-mongodb-sculptor" rel="nofollow" target="_blank">雕塑家</a>。基于MongoDB的DSL - > Java(代码生成器)</li> <li><a href="http://github.com/mattinsler/com.lowereast.guiceymongo/" rel="nofollow" target="_blank">GuicyData</a>。DSL - > Java生成器与Guice集成。</li> </ul> <h3>杂项</h3> <ul> <li><a href="https://github.com/gaillard/mongo-queue-java" rel="nofollow" target="_blank">mongo-queue-java</a>。Java消息队列使用MongoDB作为后端。</li> <li><a href="https://github.com/deftlabs/mongo-java-logging" rel="nofollow" target="_blank">mongo-java-logging</a>。Java日志记录处理程序。</li> <li><a href="http://code.google.com/p/log4mongo/" rel="nofollow" target="_blank">log4mongo</a>。一个log4j appender</li> <li><a href="http://www.allanbank.com/mongodb-async-driver/" rel="nofollow" target="_blank">Allanbank异步Java驱动程序</a></li> <li><a href="http://www.unityjdbc.com/mongojdbc/" rel="nofollow" target="_blank">MongoDB的JDBC驱动</a></li> <li><a href="http://github.com/erh/mongo-jdbc" rel="nofollow" target="_blank">(实验,Type4)JDBC驱动程序</a></li> <li><a href="http://metamodel.eobjects.org/download.html" rel="nofollow" target="_blank">元模型数据挖掘和查询库</a></li> <li><a href="https://sites.google.com/site/mongodbjavarestserver/home" rel="nofollow" target="_blank">Mongodb Java REST服务器</a>基于<a href="http://www.eclipse.org/jetty/" rel="nofollow" target="_blank">Jetty</a></li> </ul> <h3>Clojure的</h3> <ul> <li><a href="https://github.com/michaelklishin/monger" rel="nofollow" target="_blank">贩子</a></li> <li><a href="https://github.com/aboekhoff/congomongo" rel="nofollow" target="_blank">刚果蒙古</a></li> </ul> <h3>Groovy的</h3> <ul> <li><a href="http://github.com/poiati/gmongo" rel="nofollow" target="_blank">GMongo,一个Groovy包装到MongoDB Java驱动程序</a></li> <li><a href="http://blog.paulopoiati.com/2010/06/20/gmongo-0-5-released" rel="nofollow" target="_blank">GMongo 0.5发行编写</a></li> </ul> <h3>JavaScript(Rhino)</h3> <ul> <li><a href="https://github.com/nlloyd/horn-of-mongo" rel="nofollow" target="_blank">蒙戈的号角</a>。一个基于Java的Rhino JavaScript Engine构建的MongoDB shell。</li> <li><a href="http://code.google.com/p/mongodb-rhino" rel="nofollow" target="_blank">MongoDB犀牛</a>。提供JVM和MongoDB的Rhino JavaScript引擎之间的完全集成的工具集。使用MongoDB Java驱动程序。</li> </ul> <h3>Hadoop的</h3> <p><a href="https://docs.mongodb.com/ecosystem/tools/hadoop/#hadoop-connector" rel="nofollow" target="_blank">MongoDB Connector for Hadoop</a></p> <h2>其他资源</h2> <ul> <li><a href="https://university.mongodb.com/courses/M101J/about?jmp=docs" rel="nofollow" target="_blank">M101J:MongoDB for Java开发者免费在线课程</a></li> <li><a href="http://www.mongodb.com/presentations/webinar-mongodb-java-everything-you-need-know?jmp=docs" rel="nofollow" target="_blank">演示:MongoDB + Java - 你需要知道的一切</a></li> <li><a href="http://docs.mongodb.org/getting-started/java" rel="nofollow" target="_blank">MongoDB入门(Java Edition)</a></li> </ul>