Java Small algorithm-remove string repetition character __ algorithm

Source: Internet
Author: User

Method 1:

public static void Removemethod (String s) {
StringBuffer sb=new StringBuffer ();
int Len=s.length ();
int i=0;
Boolean flag=false;
for (i=0; i<len;i++) {
Char C=s.charat (i);
if (S.indexof (c)!=s.lastindexof (c)) {
Flag=false;
}else{
Flag=true;
}
if (I==s.indexof (c))
Flag=true;
if (flag) {
Sb.append (c);
}
}
System.out.print (Sb.tostring ());
}


Results: 0123456789




Method 2:treeset


//1. Add a string loop to the TreeSet

String str= "01211113477440000447756677899123";
Treeset<string> ts=new treeset<> ();
int Len=str.length ();
for (int i=0;i<len;i++) {
Ts.add (Str.charat (i) + "");
}

2. Loop Traversal TreeSet
Iterator<string> I=ts.iterator ();
StringBuilder sb=new StringBuilder ();
while (I.hasnext ()) {
Sb.append (I.next ());
}


3. Output results
System.out.println (Sb.tostring ());//0123456789



Note: Set description

Collection set interface of a sub-interface
Set description
A set is a collection that contains no duplicate elements, that is, any two elements E1 and E2 have E1.equals (E2) =false,set have a maximum of one null element. The constructor of the set has a constraint, and the Passed-in collection parameter cannot contain duplicate elements.


HashSet of SET interface
This class implements the set interface, supported by a hash table (which is actually a HashMap instance). It does not guarantee the iteration order of the set, especially it does not guarantee that the order is immutable. This class allows null elements to be used.


HashSet Simple to use
public static void Main (string[] args) {
Hashset<string> hs=new hashset<string> ();
Hs.add ("AAAA");
Hs.add ("AAAA");
Hs.add ("bbbb");
Hs.add ("bbbb");
Hs.add ("CCCC");
Hs.add ("CCCC");
Hs.add ("dddd");
Hs.add (NULL);
Iterator<string> I=hs.iterator ();
while (I.hasnext ()) {
System.out.println (I.next ());
}
}
Output
Null
bbbb
dddd
Aaaa
Cccc


TreeSet of SET interface
This class implements the set interface, which is implemented by a two-fork tree, and the data in the TreeSet is automatically sorted and is not allowed to be placed in null values.


TreeSet Simple to use
Treeset<string> ts=new treeset<string> ();
Ts.add ("AAAA");
Ts.add ("AAAA");
Ts.add ("bbbb");
Ts.add ("bbbb");
Ts.add ("CCCC");
Ts.add ("dddd");
Ts.add ("dddd");
Iterator<string> I=ts.iterator ();
while (I.hasnext ()) {
System.out.println (I.next ());
}
Output
Aaaa
bbbb
Cccc
dddd

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.