1.TreeSet 集合的介绍和排序
public class TreeSetDemo {
public static void main ( String[ ] args) {
TreeSet< Integer> treeSet = new TreeSet < > ( ) ;
treeSet. add ( 20 ) ;
treeSet. add ( 13 ) ;
treeSet. add ( 11 ) ;
treeSet. add ( 28 ) ;
treeSet. add ( 34 ) ;
System. out. println ( treeSet) ;
}
}
public class TreeSetDemo {
public static void main ( String[ ] args) {
TreeSet< Student> stu = new TreeSet < > ( ) ;
treeSet. add ( new Student ( "张" ,21 ) ) ;
treeSet. add ( new Student ( "张" ,25 ) ) ;
treeSet. add ( new Student ( "张" ,32 ) ) ;
treeSet. add ( new Student ( "张" ,54 ) ) ;
treeSet. add ( new Student ( "张" ,43 ) ) ;
treeSet. add ( new Student ( "张" ,22 ) ) ;
for (Student student :treeSet){
System. out. println ( student) ;
}
}
public class TreeSetDemo {
public static void main ( String[ ] args) {
TreeSet< Student> stu = new TreeSet < > ( ) ;
TreeSet< Seudent> treeSet = new TreeSet < > ( new Comparator < Student> ) {
public int compare ( Student s1, Studeng s2) {
int num = s1. getName ( ) . length ( ) - s2. getName ( ) . length ( ) ;
int num2 = num == 0 ? s1. getName ( ) . compareTo ( s2. getName) : num
int num3= num2== 0 ? s1. getAge ( ) - s2. getAge ( ) : num2
return num3;
}
}
treeSet. add ( new Student ( "张4一" ,12 ) ) ;
treeSet. add ( new Student ( "李撒三" ,13 ) ) ;
treeSet. add ( new Student ( "刘" ,18 ) ) ;
treeSet. add ( new Student ( "林茜茜" ,56 ) ) ;
treeSet. add ( new Student ( "张五六七" ,19 ) ) ;
treeSet. add ( new Student ( "张" ,77 ) ) ;
for (Student student :treeSet){
System. out. println ( student) ;
}
}
}
public class Demo10 {
public static void main ( String[ ] args) {
ArrayList< Integer> integers = new ArrayList < > ( ) ;
integers. add ( 100 ) ;
integers. add ( 10 ) ;
integers. add ( 198 ) ;
integers. add ( 200 ) ;
integers. sort ( new Comparator < Integer> ( ) {
@Override
public int compare ( Integer o1, Integer o2) {
return o1- o2;
}
} ) ;
System. out. println ( integers) ;
}
}
public class Demo10 {
public static void main ( String[ ] args) {
Integer[ ] a = { 1 , 4 , 2 , 9 , 6 , 5 } ;
Arrays. sort ( a) ;
System. out. println ( Arrays. sort ( a) )
public class Demo10 {
public static void main ( String[ ] args) {
Integer[ ] a = { 1 , 1 , 3 , 2 , 4 , 6 , 4 , 8 } ;
Arrays. sort ( a, new Comparator < Integer> ( ) {
@Override
public int compare ( Integer o1, Integer o2) {
return o1- o2;
}
} ) ;
System. out. println ( Arrays. toString ( a) ) ;
}
}
TreeSet< Integer> treeSet = new TreeSet < > ( ) ;
Random random = new Random ( ) ;
for ( int i = 0 ; i< 10 ; i++ ) {
int num = random. nextInt ( 20 ) + 1 ;
treeSet. add ( num) ;
}
System. out. println ( treeSet. add ( num) ; )
2.HashMap的学习与介绍
public class Demo {
public static void main ( String[ ] args) {
ArrayList< String> list = new ArrayList < > ( )
list. add ( "s001---------张三" ) ;
list. add ( "s002---------李四" ) ;
list. add ( "s003---------王五" ) ;
String s = list. get ( 0 ) ;
String[ ] strings = s. split ( "[-]" ) ;
System. out. println ( strings[ 0 ] ) ;
System. out. println ( strings[ 1 ] ) ;
public class Demo {
public static void main ( String[ ] args) {
HashMap< String, String> map = new HashMap < > ( ) ;
String str = map. put ( "q" , "11" ) ;
String str1 = map. put ( "q" , "12" ) ;
map. put ( "q" , "11" ) ;
map. put ( "q" , "12" ) ;
map. put ( "e" , "13" ) ;
map. put ( "t" , "14" ) ;
map. put ( "p" , "22" ) ;
System. out. println ( map) ;
System. out. println ( str) ;
System. out. println ( str1) ;
Set< String> KeySet = map. keySet ( ) ;
for ( String key : keySet) {
System. out. println ( key+ "===" + map. get ( key) ) ;
}
Set< Map. Entry< String, String> > entries = map. entrySet ( ) ;
for ( Map. Entry< String, String> entry : entries) {
String key = entry. getKey ( ) ;
String value = entry. getValue ( ) ;
System. out. println ( key+ "=" + value) ;
3.HashMap与HashTable的区别
HashMap< String, Integer> hashMap = new HashMap < > ( ) ;
hashmap. put ( null, "1" ) ;
hashmap. put ( "2" , null) ;
hashmap. put ( "7" , "4" ) ;
hashmap. put ( "null" , null) ;
System. out. println ( hasMap) ;
HashTable< String, Integer> HashTable = new HashTable < > ( ) ;
HashTable. put ( null, "1" ) ;
HashTable. put ( "2" , null) ;
HashTable. put ( "7" , "4" ) ;
System. out. println ( hasMap) ;
4.LinkedHashMap集合的学习
LinkedHashMap< Integer, String> map = new LinkedHashMap < > ( ) ;
map. put ( 1 , "qq" ) ;
map. put ( 2 , "ww" ) ;
map. put ( 3 , "ee" ) ;
map. put ( 4 , "tt" ) ;
map. put ( 5 , "uu" ) ;
System. out. println ( map) ;
5.TreeMap集合的学习
public class Demo10 {
public static void main ( String[ ] args) {
@Override
TreeMap< Student, Integer > treeMap = new TreeMap < > ( new Comparator < Object> ( ) {
public int compare ( Student a1, Student a2) {
int i = a1. getAge ( ) - a2. getAge ( ) ;
int j = i== 0 ? a1. getName ( ) . compareTo ( s2. getName ( ) ) : i
return j;
treeMap. put ( new Student ( name: "zhang" , age:1 ) ,1 ) ;
treeMap. put ( new Student ( name: "li" , age:12 ) ,3 ) ;
treeMap. put ( new Student ( name: "wang" , age:15 ) ,4 ) ;
treeMap. put ( new Student ( name: "lin" , age:16 ) ,5 ) ;
Set< Map. Entry< Object, Object> > entries = treeMap. entrySet ( ) ;
for ( Map. Entry< Object, Object> entry : entries) {
Object key = entry. getKey ( ) ;
Object value = entry. getValue ( ) ;
System. out. println ( key+ "==" + value) ;
6.集合的练习
public class Demo10 {
public static void main ( String[ ] args) {
HashMap< Character, Integer> hashMap = new HashMap < > ( ) ;
Scanner scanner = new Scanner ( System. in) ;
System. out. println ( "请输入字符串" ) ;
String s = scanner. nextLine ( ) ;
for ( int i = 0 ; i < s. length ( ) ; i++ ) {
char c = s. charAt ( i) ;
if ( ! hashMap. containsKey ( c) ) {
hashMap. put ( c, 1 ) ;
} else {
Integer integer = hashMap. get ( c) ;
integer++ ;
Integer put = hashMap. put ( c, integer) ;
}
}
Set< Map. Entry< Character, Integer> > entries = hashMap. entrySet ( ) ;
for ( Map. Entry< Character, Integer> entry : entries) {
Character key = entry. getKey ( ) ;
Integer value = entry. getValue ( ) ;
System. out. print ( key+ "(" + value+ ")" ) ;
}
}
}
public class Demo10 {
public static void main ( String[ ] args) {
public class Demo10 {
public static void main ( String[ ] args) {
HashMap< String, Integer> hashMap = new HashMap < > ( ) ;
hashMap. put ( "张三" , 20 ) ;
hashMap. put ( "李四" , 21 ) ;
HashMap< String, Integer> hashMap1 = new HashMap < > ( ) ;
hashMap1. put ( "王五" , 23 ) ;
hashMap1. put ( "赵六" , 25 ) ;
HashMap< String, HashMap< String, Integer> > hashMap2 = new HashMap < > ( ) ;
Set< Map. Entry< String, HashMap< String, Integer> >> entries = hashMap2. entrySet ( ) ;
for ( Map. Entry< String, HashMap< String, Integer> > entry : entries) {
String key = entry. getKey ( ) ;
System. out. println ( key) ;
HashMap< String, Integer> value = entry. getValue ( ) ;
Set< Map. Entry< String, Integer> > entries1 = value. entrySet ( ) ;
for ( Map. Entry< String, Integer> stringIntegerEntry : entries1) {
Integer value1 = stringIntegerEntry. getValue ( ) ;
String key1 = stringIntegerEntry. getKey ( ) ;
System. out. println ( key1+ value1) ;
}
System. out. println ( "" ) ;
}
}
}