package bean ;public class Person {private String name ;private int age ;public Person ( String name , int age ) {super ();this . name = name ;this . age = age ;}@Overridepublic int hashCode () {final int prime = 31 ;int result = 1 ;result = prime * result + age ;result = prime * result + (( name == null ) ? 0 : name . hashCode ());return result ;}@Overridepublic boolean equals ( Object obj ) {if ( this == obj ) return true ;if ( obj == null ) return false ;if ( getClass () != obj . getClass ()) return false ;Person other = ( Person ) obj ;if ( age != other . age ) return false ;if ( name == null ) {if ( other . name != null ) return false ;} else if (! name . equals ( other . name )) return false ;return true ;}@Overridepublic String toString () {return "Person [name=" + name + ", age=" + age + "]" ;}}
package Collection ;import java . util . HashSet ;import java . util . Iterator ;import java . util . Set ;import bean . Person ;public class HashSetDemo {public static void main ( String [] args ) {// Set集合保证元素唯一性。Set < String > set = new HashSet < String >();set . add ( "Java1" );set . add ( "Java2" );set . add ( "Java1" );set . add ( "Java1" );set . add ( "Java2" );for ( Iterator < String > it = set . iterator (); it . hasNext ();) {System . out . println ( it . next ());}System . out . println ( "-------------------" );// Set集合存储自定义类时,建议该类重写hashCode和equals方法,这是保证元素唯一性的前提,一般还会重写toString方法。Set < Person > setObj = new HashSet < Person >();setObj . add ( new Person ( "Jacob" , 25 ));setObj . add ( new Person ( "Meteor" , 23 ));setObj . add ( new Person ( "Tom" , 26 ));setObj . add ( new Person ( "Llllin" , 27 ));setObj . add ( new Person ( "Jacob" , 24 ));for ( Iterator < Person > it = setObj . iterator (); it . hasNext ();) {System . out . println ( it . next ());}}}
package Collection ;import java . util . HashSet ;import java . util . Iterator ;import java . util . Set ;public class HashSetDemo {public static void main ( String [] args ) {// Set集合不保证存取顺序。Set < String > set = new HashSet < String >();set . add ( "Java1" );set . add ( "Java2" );set . add ( "Java3" );set . add ( "Java4" );set . add ( "Java5" );for ( Iterator < String > it = set . iterator (); it . hasNext ();) {System . out . println ( it . next ());}}}
package comparator ;import java . util . Comparator ;public class ComparatorByLength implements Comparator < String > {@Overridepublic int compare ( String s1 , String s2 ) {int result = s1 . length () - s2 . length ();return result == 0 ? s1 . compareTo ( s2 ) : result ;}}
package collection . set ;import java . util . Collections ;import java . util . HashSet ;import java . util . Iterator ;import java . util . Set ;import comparator . ComparatorByLength ;public class HashSetTest {public static void main ( String [] args ) {/** 案例:获取集合最大值*/Set < String > set = new HashSet < String >();set . add ( "Jacob" );set . add ( "Java" );set . add ( "Meteor" );set . add ( "abc" );set . add ( "it&java" );set . add ( "zz" );// 方法一:使用Collections.max()方法(使用的是自然排序方法)。System . out . println ( Collections . max ( set ));// 方法二:使用自然排序System . out . println ( getMax ( set ));// 方法三:使用比较器(ComparatorByLength)System . out . println ( getMax ( set , new ComparatorByLength ()));}/*** 使用比较器获取Set集合中的最大值(使用字符串长度比较器)** @param set* @param comparatorByLength* @return*/public static Object getMax ( Set < String > set , ComparatorByLength comparatorByLength ) {if ( comparatorByLength == null ) {throw new NullPointerException ();}Iterator < String > it = set . iterator ();String max = it . next ();while ( it . hasNext ()) {String obj = it . next ();if ( comparatorByLength . compare ( obj , max ) > 0 ) {max = obj ;}}return max ;}/*** 使用自然排序获取Set集合中的最大值。** @param set* @return*/public static Object getMax ( Set < String > set ) {Iterator < String > it = set . iterator ();String max = it . next ();while ( it . hasNext ()) {String com = it . next ();if ( com . compareTo ( max ) > 0 ) {max = com ;}}return max ;}}
package collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
public class Set_LinkedHashSet {
public static void main(String[] args) {
/*
* LinkedHashSet可以保持元素存取顺序
*/
Set<String> set1 = new HashSet<String>();
Set<String> set2 = new LinkedHashSet<String>();
init(set1 );
init(set2 );
System.out.println( "HashSet:");
for (Iterator<String> it = set1.iterator(); it.hasNext();) {
System.out.println( it.next());
}
System.out.println( "LinkedHashSet:");
for (Iterator<String> it = set2.iterator(); it.hasNext();) {
System.out.println( it.next());
}
}
public static void init(Set<String> set) {
set.add("Java1");
set.add("Java2");
set.add("Java3");
set.add("Java4");
set.add("Java5");
}
}
package bean;
public class Person implements Comparable<Person> {
private String name;
private int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Person() {
super();
}
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;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj ) {
if (this == obj) return true ;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false ;
Person other = (Person) obj;
if (age != other.age) return false;
if (name == null) {
if (other.name != null) return false;
} else if (!name.equals(other.name)) return false;
return true;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]" ;
}
@Override
public int compareTo(Person o) {
int margin = this.name.compareTo( o. name);
return margin == 0 ? this. age - o. age : margin;
}
}
ComparatorByAge.java
package comparator;
import java.util.Comparator;
import bean.Person;
public class ComparatorByAge implements Comparator<Person> {
@Override
public int compare(Person o1, Person o2) {
int margin = o1.getAge() - o2.getAge();
return margin == 0 ? o1 .compareTo(o2 ) : margin ;
}
}
package collection.set;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import bean.Person;
import comparator.ComparatorByAge;
import comparator.ComparatorByLength;
public class TreeSetDemo {
public static void main(String[] args) {
// 使用自然排序将字符串加入TreeSet集合
System.out.println( "字符串(自然排序):" );
Set<String> set = new TreeSet<String>();
set.add("java");
set.add("linkedhashset");
set.add("awesome");
set.add("zzz");
set.add("treeset");
// 元素在添加的时候就已经间接调用compareTo方法进行排序
for (Object object : set) {
System.out.println( object);
}
// 使用比较器将字符串按由长至短加入TreeSet集合
System.out.println( "字符串(比较器):" );
Set<String> setComparatorByLength =
new TreeSet<String>(Collections. reverseOrder(new ComparatorByLength()));
setComparatorByLength.add( "java");
setComparatorByLength.add( "linkedhashset");
setComparatorByLength.add( "awesome");
setComparatorByLength.add( "zzz");
setComparatorByLength.add( "treeset");
for (String string : setComparatorByLength) {
System.out.println( string);
}
// 使用自然排序将自定义类加入TreeSet集合
System.out.println( "自定义类(自然排序):" );
Set<Person> setObj = new TreeSet<Person>();
setObj.add(new Person( "jacob", 25));
setObj.add(new Person( "meteor", 23));
setObj.add(new Person( "kitty", 22));
setObj.add(new Person( "tom", 26));
for (Iterator<Person> it = setObj.iterator(); it.hasNext();) {
System.out.println( it.next());
}
// 使用比较器将自定义类加入TreeSet集合
System.out.println( "自定义类(比较器):" );
TreeSet<Person> setObjComparatorByAge = new TreeSet<Person>(new ComparatorByAge());
setObjComparatorByAge.add( new Person( "jacob", 25));
setObjComparatorByAge.add( new Person( "meteor", 23));
setObjComparatorByAge.add( new Person( "kitty", 22));
setObjComparatorByAge.add( new Person( "tom", 26));
for (Iterator<Person> it = setObjComparatorByAge.iterator(); it .hasNext();) {
System.out.println( it.next());
}
}
}