Java's ArrayList cannot be duplicated, using HashSet

Source: Internet
Author: User

ArrayList can only use the Contains method to judge all objects that are read into memory at once, thus removing duplicates.

list<string> seqId = new arraylist<string> (); for (int i = 0; i < alllines.length; i++) {string[            ] Eachline = Alllines[i].split ("[\ t]");            System.out.println (Eachline[0]);            if (!seqid.contains (eachline[0))) {Seqid.add (eachline[0]); }}


HashSet can remove repetition;

And to specify the object type,

hashset<string> seqidset = new hashset<string> ();iterator<string> IdS = Seqidset.iterator (); while (            Ids.hasnext ()) {String tmpid = Ids.next ();            string tmp = new string (tmpid);            String Eachidinset = tmp.substring (0,tmp.indexof ("."));            System.out.println ("..." +eachidinset); System.out.println (Eachidinset);}

And iterator can only generate one iteration object and cannot be reused.

iterator<string> IdS = Seqidset.iterator (); This sentence if you do not specify a <String> object of this type, the following String tmpid = Ids.next (); will be changed to Object tmpid = Ids.next ();

The Java classes you need to import are as follows:

/** import java.io.file;import java.io.fileinputstream;import java.io.filereader;import Java.io.IOException;import Java.io.bufferedreader;import Java.util.Scanner; */import java.util.iterator;import java.util.hashmap;import Java.util.set;import java.util.HashSet;import Java.util.arraylist;import java.util.List;


This article is from the "R Language Learning history" blog, please be sure to keep this source http://matrix6ro.blog.51cto.com/1746429/1773312

Java's ArrayList cannot be duplicated, using HashSet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.