集合Set(下)

Set(下)

https://blog.csdn.net/Veer_c/article/details/103825477(Set上)

java.lang.ClaastException:类型转换异常
有俩种解决方式:
1.集合中的对象所属的类实现一个Comparable接口
2.为集合指定相应的比较器
TreeSet集合有两种排序方式:至于哪两种排序方式,取决于他的构造器
自然排序:无参构造public TreeSet()
比较器排序(Comparator):

import java.util.TreeSet;

public class TreeSetDemo {
 public static void main(String[] args) {
  //创建TreeSet集合,存储自定义对象
  TreeSet<Student> ts = new TreeSet<Student>();

  //给集合中添加Student对象
  Student s = new Student("guodegang", 50);
  Student s6 = new Student("liuyifei", 50);
  Student s2 = new Student("zhangxueyou", 55);
  Student s3 = new Student("amu", 45);
  Student s4 = new Student("tf-boys", 18);
  Student s5 = new Student("wangfeng", 49);
  ts.add(s);
  ts.add(s2);
  ts.add(s3);
  ts.add(s4);
  ts.add(s5);
  ts.add(s6);

  //遍历集合
  for (Student student : ts) {
   System.out.println(student);
  }
 }
}

3.创建set集合的时候,传入Comparator(比较器排序)进行排序,进行排序。

public class Student{
 private String name;
 private int age;
 public Student(String name, int age) {
  super();
  this.name = name;
  this.age = age;
 }
 public Student() {
  super();
  // TODO Auto-generated constructor stub
 }
 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 String toString() {
  return "Student [name=" + name + ", age=" + age + "]";
 }
}
import java.util.Comparator;
import java.util.TreeSet;

public class TreeSetDemo {
 public static void main(String[] args) {
  //创建集合,参数是一个接口,实际需要的是接口的实现类对象
  //TreeSet<Student> ts = new TreeSet<Student>(new ComparatorImpl());
  //使用匿名内部类来进行改进
  TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
   @Override
   public int compare(Student s1, Student s2) {
    int num = s1.getAge() - s2.getAge();
    int num2 = num==0?s1.getName().compareTo(s2.getName()):num;
    return num2;
   }
  });
  //创建对象存入集合
  Student s = new Student("guodegang", 50);
  Student s6 = new Student("liuyifei", 50);
  Student s2 = new Student("zhangxueyou", 55);
  Student s3 = new Student("amu", 45);
  Student s4 = new Student("tf-boys", 18);
  Student s5 = new Student("wangfeng", 49);
  ts.add(s);
  ts.add(s2);
  ts.add(s3);
  ts.add(s4);
  ts.add(s5);
  ts.add(s6);

  //遍历集合
  for (Student student : ts) {
   System.out.println(student);
  }
 }
}

Set的遍历
Iterator:创建迭代器进行遍历
foreach:利用增强for进行遍历

HashSet与TreeSet的相同点与不同点
相同点:
单列集合,元素不可重复
不同点:
1.底层存储的数据结构不同
HashSet底层用的是HashMap哈希表结构存储,而TreeSet底层用的是TreeMap树结构存储
2.存储时保证数据唯一性依据不同
HashSet是通过重写hashCode()方法和equals()方法来保证的,而TreeSet通过Compareable接口的compareTo()方法来保证的
3.有序性不一样
HashSet无序,TreeSet有序

这种情况的数据,属于一一对应的映射关系。这种关系的集合在java叫Map。
Map:将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。

Map接口中的方法概述(创建集合测试方法):

删除功能
void clear():移除集合中的所有键值对元素
V remove(Object key):根据键移除键值对元素,并返回值
判断功能
boolean containsKey(Object key):判断集合中是否包含指定的键
boolean containsValue(Object value):判断集合中是否包含指定的值
boolean isEmpty():判断集合是否为空
获取功能
Set

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class MapDemo {
 public static void main(String[] args) {
  //创建一个Map集合
  //键是学号--值是姓名
  Map<Integer,String> map = new HashMap<Integer,String>();
  //V put(K key,V value):集合添加键值对
  map.put(1, "周杰伦");
  map.put(2, "郭德纲");
  map.put(3, "刘德华");
  map.put(4, "张学友");
  //void clear():移除集合中的所有键值对元素
  //map.clear();
  //V remove(Object key):根据键移除键值对元素,并返回值
  //String remove = map.remove(1);
  //System.out.println(remove);
  //boolean containsKey(Object key):判断集合中是否包含指定的键
  //boolean containsKey = map.containsKey(2);
  //System.out.println(containsKey);
  //boolean containsValue(Object value):判断集合中是否包含指定的值
  //boolean containsValue = map.containsValue("周杰伦");
  //System.out.println(containsValue);
  //boolean isEmpty():判断集合是否为空
  //System.out.println(map.isEmpty());
  //int size():键值对对数。
  //System.out.println(map.size());
  //Collection<V> values():获取所有的值
  /*
  Collection<String> values = map.values();
  Iterator<String> it = values.iterator();
  while (it.hasNext()) {
   System.out.println(it.next());
  }*/
  //Set<Map.Entry<K,V>> entrySet():获取键值对对象的集合,遍历键值对对象,
     //利用getKey(),getValue()取出键和值(理解即可)
  Set<Entry<Integer,String>> entrySet = map.entrySet();
  for (Entry<Integer, String> entry : entrySet) {
   System.out.println(entry.getKey()+"  "+entry.getValue());
  }
  System.out.println("--------------------");
  //Set<K> keySet():获取所有的键
  Set<Integer> keys = map.keySet();
  for (Integer key : keys) {
   //V get(Object key):根据键获取值
   System.out.println(key+"  "+map.get(key));
  }
 }
}

HashMap

元素顺序:元素顺序不可预测
底层算法:哈希算法
对键没有要求(仅仅相对于TreeMap来说) 知识点demo演练:

练习1:存入(String,String)主要讲解遍历方式,键:丈夫 值:妻子

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
public class HashMapDemo {
 public static void main(String[] args) {
  // 1.存入(String,String)主要讲解遍历方式,键:丈夫  值:妻子
  HashMap<String, String> hm = new HashMap<String,String>();

  //给键值对集合存入元素
  hm.put("文章", "马伊琍");
  hm.put("黄晓明", "baby");
  hm.put("汪老师", "章子怡");
  hm.put("周杰伦", "昆凌");
  //hm.put("文章", "姚笛");,当后面存入的元素和前面的键相同的时候,前面的元素的值会被后面的元素的值代替

  //遍历,通过键找值
  Set<String> keys = hm.keySet();
  for (String key : keys) {
   System.out.println(key+"  "+hm.get(key));
  }
  System.out.println("-------------");
  //通过找到结婚证,来分别找到丈夫和妻子
  Set<Entry<String,String>> entrySet = hm.entrySet();
  for (Entry<String, String> entry : entrySet) {
   System.out.println(entry.getKey()+"  "+entry.getValue());
  }
 }
}

练习2: 存入(String,Student)键:String(国籍) 值:Student

public class Student{
 private String name;
 private int age;
 public Student(String name, int age) {
  super();
  this.name = name;
  this.age = age;
 }
 public Student() {
  super();
  // TODO Auto-generated constructor stub
 }
 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 String toString() {
  return "Student [name=" + name + ", age=" + age + "]";
 }
}
import java.util.HashMap;
import java.util.Set;
public class HashMapDemo {
 public static void main(String[] args) {
  // 2.存入(String,Student)键:String(国籍)  值:Student
  HashMap<String, Student> hm = new HashMap<String, Student>();

  //创建学生对象
  Student s1 = new Student("山姆大叔", 60);
  Student s2 = new Student("白起", 50);
  Student s3 = new Student("李白", 30);

  //将对象存入集合
  hm.put("美国", s1);
  hm.put("秦国", s2);
  hm.put("中国", s3);

  //同过键找值
  Set<String> keys = hm.keySet();
  for (String key : keys) {
   System.out.println(key+"  "+hm.get(key));
  }
 }
}

练习3:存入(Student,String),键是同一个对象的时候要把之前存入的元素挤出去,想要实现这样的效果的话,需要重写javabean里面的hashCode()和equals()方法。

 public class Student{
 private String name;
 private int age;
 public Student(String name, int age) {
  super();
  this.name = name;
  this.age = age;
 }
 public Student() {
  super();
  // TODO Auto-generated constructor stub
 }
 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 String toString() {
  return "Student [name=" + name + ", 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;
  Student other = (Student) 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;
 }
}
import java.util.HashMap;
import java.util.Set;
public class HashMapDemo {
 public static void main(String[] args) {
  // 3.存入(Student,String)
  //创建一个Map集合
  HashMap<Student, String> hm = new HashMap<Student, String>();
  //创建学生对象
  Student s1 = new Student("白起", 60);
  Student s2 = new Student("李白", 50);
  Student s3 = new Student("山姆大叔", 30);
  Student s4 = new Student("诸葛", 30);
  //将对象存入集合
  hm.put(s1, "秦国");
  hm.put(s2, "中国");
  hm.put(s3, "美国");
  hm.put(s4, "天府之国");
  //遍历集合
  Set<Student> keys = hm.keySet();
  for (Student s : keys) {
   System.out.println(s+"  "+hm.get(s));
  }
 }
}
发布了62 篇原创文章 · 获赞 55 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Veer_c/article/details/103827269
今日推荐