Java ArrayList de-weight

Source: Internet
Author: User
Tags addall set set

The duplicate values in the list collection are processed in most cases by two methods,

One is to assign to another list set after judging by traversing the list collection,

The other is to return to the list collection by assigning a set set.

Method 1:set set to go heavy without disrupting the order

list<string> list =NewArraylist<string>(); List.add ("AAA"); List.add ("BBB"); List.add ("AAA"); List.add ("ABA"); List.add ("AAA");//set set to go heavy without disrupting the orderSet<string> set =NewHashset<string>(); List<String> NewList =NewArraylist<string>(); for(String cd:list) {if(Set.add (CD)) {Newlist.add (CD); }}system.out.println ("Go to the weight of the collection:" + newlist);

Method 2: After traversal, judge to assign to another list collection

// after traversal, judge assigns to another list collection New Arraylist<string>();  for (String cd:list) {    if (!  Newlist.contains (CD)) {        newlist.add (CD);}    } System.out.println ("de-weight after collection:" + newlist);

Method 3:set to Weight

// set de-weight New Hashset<string>(); Listnew arraylist<string>(); Set.addall (list); Newlist.addall (set); System.out.println ("de-weight after collection:" + newlist);

Method 4:set (code is reduced to one line)

// set de-weight (reduced to one line) New Arraylist<string> (new hashset<string>(list)); System.out.println ("de-weight after collection:" + newlist);

Method 5: Remove weight and arrange in natural order

// go to weight and arrange in natural order New Arraylist<string> (new treeset<string>(list)); System.out.println ("de-weight after collection:" + newlist);

Article Source: http://blog.csdn.net/cs6704/article/details/50158373

Java ArrayList de-weight

Related Article

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.