Java Tutorial/Apache Common/CharSetUtils
Содержание
Build Equals With EqualBuilder
import org.apache.rumons.lang.builder.EqualsBuilder;
import org.apache.rumons.lang.builder.HashCodeBuilder;
import org.apache.rumons.lang.builder.ToStringBuilder;
import org.apache.rumons.lang.builder.ToStringStyle;
public class MainClass {
public static void main(String[] args) {
//Create new BuilderTrial instances
BuilderTrial one = new BuilderTrial("Becker", 35);
BuilderTrial two = new BuilderTrial("Becker", 35);
BuilderTrial three = new BuilderTrial("Agassi", 33);
//one and two hold the same data in different objects
//three holds different data
System.out.println("One>>>" + one);
System.out.println("Two>>>" + two);
System.out.println("Three>>>" + three);
System.out.println("one equals two? " + one.equals(two));
System.out.println("one equals three? " + one.equals(three));
//As one and two hold the same data, the same hashcode is returned.
System.out.println("One HashCode>>> " + one.hashCode());
System.out.println("Two HashCode>>> " + two.hashCode());
System.out.println("Three HashCode>>> " + three.hashCode());
}
}
class BuilderTrial {
private String name;
private int age;
public BuilderTrial(String name, int age) {
this.name = name;
this.age = age;
}
public static void main(String[] args) {
}
public boolean equals(Object objCompared) {
if (!(objCompared instanceof BuilderTrial)) {
return false;
}
BuilderTrial rhs = (BuilderTrial) objCompared;
return new EqualsBuilder().append(name, rhs.name).append(age, rhs.age)
.isEquals();
}
public String toString() {
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).append("Name",
name).append("Age", age).toString();
}
public int hashCode() {
return new HashCodeBuilder(15, 19).append(name).append(age).toHashCode();
}
}
One>>>BuilderTrial@a981ca[ Name=Becker Age=35 ] Two>>>BuilderTrial@e7b241[ Name=Becker Age=35 ] Three>>>BuilderTrial@167d940[ Name=Agassi Age=33 ] one equals two? true one equals three? false One HashCode>>> -923455822 Two HashCode>>> -923455822 Three HashCode>>> -1433293806
CharSetUtils.count
import org.apache.rumons.lang.CharSetUtils;
public class MainClass {
public static void main(String[] args) {
//Count all occurrences of all the characters specified.
System.out.println("B and o count = " +
CharSetUtils.count("BorisBecker", "Bo"));
System.out.println("B,o,k,e and r count = " +
CharSetUtils.count("BorisBecker", new String[] { "Bo", "ker" }));
}
}
B and o count = 3 B,o,k,e and r count = 8
CharSetUtils.delete
import org.apache.rumons.lang.CharSetUtils;
public class MainClass {
public static void main(String[] args) {
//Specified characters deleted.
System.out.println("Delete B and o = " +
CharSetUtils.delete("BorisBecker", "Bo"));
System.out.println("Delete B,o,k,e and r = " +
CharSetUtils.delete("BorisBecker", new String[] { "Bo", "ker" }));
}
}
Delete B and o = risecker Delete B,o,k,e and r = isc
CharSetUtils.keep
import org.apache.rumons.lang.CharSetUtils;
public class MainClass {
public static void main(String[] args) {
//Keeps only the characters specified
System.out.println("Keep B and o = " +
CharSetUtils.keep("BorisBecker", "Bo"));
}
}
Keep B and o = BoB
CharSetUtils.squeeze
import org.apache.rumons.lang.CharSetUtils;
public class MainClass {
public static void main(String[] args) {
//Removes specified character repetitions
System.out.println("Squeeze B and o = " +
CharSetUtils.squeeze("BBoooorisbbbecker", "Bo"));
}
}
Squeeze B and o = Borisbbbecker