博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TreeSet&第三方比较器&Map
阅读量:4634 次
发布时间:2019-06-09

本文共 5143 字,大约阅读时间需要 17 分钟。

TreeSet集合

特点:无序,但是可排序,不重复

  • CompareTo方法:对于String类的CompareTo方法,由对象的unicode码-参数的unicode码,并且按位比较。
  • 如果值大于0,证明对象要大于参数。
  • 如果值小于0,证明对象要小于参数。
  • 如果值等于0,证明对象等于参数

代码演示:

1 public class TreeSet 集合 { 2 public static void main(String[] args) { 3 /*TreeSet
s=new TreeSet<>(); 4 s.add("1"); 5 s.add("5"); 6 s.add("2"); 7 s.add("9"); 8 s.add("3"); 9 System.out.println(s);*/10 TreeSet
set=new TreeSet<>();11 set.add(new Person("ls", 20));12 set.add(new Person("zs", 19));13 set.add(new Person("ml", 22));14 set.add(new Person("ww", 21));15 System.out.println(set);16 // System.out.println("ab".compareTo("abcd"));17 }18 }19 class Person implements Comparable
{20 String name;21 int age;22 @Override23 public String toString() {24 return "Person [name=" + name + ", age=" + age + "]";25 }26 public Person() {27 super();28 // TODO Auto-generated constructor stub29 }30 public Person(String name, int age) {31 super();32 this.name = name;33 this.age = age;34 }35 // 假设排序规则: 按照年龄进行升序排序 -- 》要降序?将原有顺序结果加负号或者将参数与对象对调位置。36 @Override37 public int compareTo(Person o) {38 return -this.name.compareTo(o.name);39 }40 }

 

 第三方比较器:

代码演示:

1 public class  第三方比较器 { 2 public static void main(String[] args) { 3 /*TreeSet
set=new TreeSet<>(new StudentComparator()); 4 set.add(new Student(19, "zs")); 5 set.add(new Student(20, "ls")); 6 set.add(new Student(21, "ww")); 7 System.out.println(set);*/ 8 // 按姓名排序 9 TreeSet
set=new TreeSet<>(new Comparator
() {10 @Override11 public int compare(Student o1, Student o2) {12 // TODO Auto-generated method stub13 return o1.name.compareTo(o2.name);14 }15 });16 set.add(new Student(19, "zs"));17 set.add(new Student(20, "ls"));18 set.add(new Student(21, "ww"));19 System.out.println(set);20 }21 }22 // 第三方比较器的子类23 class StudentComparator implements Comparator
{24 //o1 相当于 this o2 相当于 o25 // 按照年龄比 升序26 @Override27 public int compare(Student o1, Student o2) {28 // TODO Auto-generated method stub29 return o1.age-o2.age;30 }31 }32 class Student {33 int age;34 String name;35 public Student() {36 super();37 // TODO Auto-generated constructor stub38 }39 public Student(int age, String name) {40 super();41 this.age = age;42 this.name = name;43 }44 @Override45 public String toString() {46 return "Student [age=" + age + ", name=" + name + "]";47 }48 }

 

 

Map集合:

Map集合  称为双列集合,存储的是一对值,称为键值对,其中键用key,值用value表示

Map集合  键是不重复的,并且每个键最多映射一个值。

如果插入集合的键与原有的键重复,会覆盖原有键的值。

3. 常用方法:

①.containsKey(Object key) /containsValue(Object value) 包含键 / 值
***②.get(key) 通过 key 获取 value
***③.put(K key, V value) 添加,将 key 与 value 进行关联
④.remove(Object key) 通过 key ,删除。

 

Map集合的遍历

①增强for

②迭代器

注意:要使用迭代器,就要将Map集合转换为Set集合

① keySet() 将 Map 集合转换为 Set 集合,其中将 Map 集合中的 key 转换为 Set 集合。

② entrySet() 将 Map 集合转换为 Set 集合,其中将 Map 集合中的 key 和 value 同时看做一个 Entry ,
一个 Entry 中同时包括 key 和 value 。
1>getKey 获取键
2>getValue 获取值

 

代码演示:

1 public class Map 集合的遍历 { 2 public static void main(String[] args) { 3 HashMap
map=new HashMap<>(); 4 map.put("1", " 一 "); 5 map.put("3", " 三 "); 6 map.put("2", " 二 "); 7 map.put("5", " 五 "); 8 //① keySet 9 /*Set
keySet = map.keySet();10 //for/ 迭代器11 Iterator
it = keySet.iterator();12 while(it.hasNext()) {13 String key = it.next();14 // get(key) 通过 key 获取值15 System.out.println(key+"====>"+map.get(key));16 }*/17 //② entrySet18 Set
> set1=map.entrySet();19 Iterator
> it=set1.iterator();20 while(it.hasNext()) {21 Map.Entry
entry=it.next();22 System.out.println(entry.getKey()+"===>"+entry.getValue());23 }24 }25 }

 

 

 

集合应用之简版斗地主:

1 public class  简版斗地主 { 2 public static void main(String[] args) { 3 String[] colors= {
" 方块 "," 梅花 "," 黑桃 "," 红桃 "}; 4 String[] nums= {
"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; 5 // 洗牌: 将对应的花色与牌面拼接成字符串 6 ArrayList
pokers=new ArrayList<>(); 7 for (String string : colors) { 8 for (String st : nums) { 9 pokers.add(string+st);10 }11 }12 // 加入大小王13 pokers.add(" 小王 ");14 pokers.add(" 大王 ");15 System.out.println(pokers);16 System.out.println(pokers.size());17 // 洗牌18 Collections.shuffle(pokers);19 System.out.println(pokers);20 // 底牌21 ArrayList
diPai=new ArrayList<>();22 // 玩家 123 ArrayList
player1=new ArrayList<>();24 // 玩家 225 ArrayList
player2=new ArrayList<>();26 // 玩家 327 ArrayList
player3=new ArrayList<>();28 // 底牌 3 张29 diPai.add(pokers.remove(0));30 diPai.add(pokers.remove(0));31 diPai.add(pokers.remove(0));32 System.out.println(diPai);33 System.out.println(pokers);34 while(true) {35 if(pokers.isEmpty()) {36 break;37 }38 player1.add(pokers.remove(0));39 if(pokers.isEmpty()) {40 break;41 }42 player2.add(pokers.remove(0));43 if(pokers.isEmpty()) {44 break;45 }46 player3.add(pokers.remove(0));47 }48 System.out.println("========================");49 System.out.println(player1);50 System.out.println(player2);51 System.out.println(player3);52 System.out.println(player3);53 }54 }

 

转载于:https://www.cnblogs.com/ywzbky/p/10679118.html

你可能感兴趣的文章
jsp 环境配置记录
查看>>
本地视频播放黑屏,有声音
查看>>
Python3-Cookbook总结 - 第一章:数据结构和算法
查看>>
Python03
查看>>
LOJ 2537 「PKUWC2018」Minimax
查看>>
使用java中replaceAll方法替换字符串中的反斜杠
查看>>
Android初学第36天
查看>>
Some configure
查看>>
.net core 中的[FromBody]
查看>>
json_encode时中文编码转正常状态
查看>>
流量调整和限流技术 【转载】
查看>>
Axure 全局辅助线(转)
查看>>
正由另一进程使用,因此该进程无法访问此文件。
查看>>
1 线性空间
查看>>
VS不显示最近打开的项目
查看>>
MyEclipse安装Freemarker插件
查看>>
计算多项式的值
查看>>
DP(动态规划)
查看>>
chkconfig
查看>>
TMS320F28335项目开发记录2_CCS与JTAG仿真器连接问题汇总
查看>>