其他分享
首页 > 其他分享> > 第七天集合

第七天集合

作者:互联网

今天主要学了Map集合直接实现或间接实现类有HashMap,LinkedMap,TreeMap,Hashtable,Properties其中最常用的是HashMap,里面存储的键值对是无序的,不可重复的。HashMap的存储结构为数组+链表+红黑树。LinkedMap是HashMap的子类,通过链表存储键值对,保证了数据的顺序。TreeMap也是有序的,可通过自然排序或自定义排序实现。Collections工具类是针对List,Map,Set集合的工具类,可以实现数据排序、翻转等操作。这些集合的底层实现还是等后期再来研究吧。

 

Map集合

创建Map集合的对象

HashMap

        Map<String,String> map = new HashMap<>();

       map.put("张无忌","赵敏");
       map.put("郭靖","黄蓉");
       map.put("杨过","小龙女");

       System.out.println(map.remove("郭靖"));
       System.out.println(map.containsValue("小龙女"));
       System.out.println(map);
       System.out.println(map.size());
==========
黄蓉
true
{杨过=小龙女, 张无忌=赵敏}
2
        Map<String,String> map = new HashMap<>();

       map.put("张无忌","赵敏");
       map.put("郭靖","黄蓉");
       map.put("杨过","小龙女");

       //获取键对应的值
       System.out.println(map.get("张无忌"));
       System.out.println("===========");
       //获取键的集合返回的是set集合
       Set<String> set = map.keySet();
       for (String s : set) {
           System.out.println(s);
      }
       System.out.println("===========");
       //获取值的集合返回的是Collection
       Collection<String> cle = map.values();
       for (String s : cle) {
           System.out.println(s);
      }

上面是遍历方法一

下面是遍历方法二通过entrySet方法

        Map map = new HashMap<>();
       map.put("张无忌","赵敏");
       map.put("郭靖","黄蓉");
       map.put("杨过","小龙女");
       //entrySet
       Set entry = map.entrySet();
       Iterator iterator = entry.iterator();
       while (iterator.hasNext()){
           Object obj = iterator.next();
           //Entry中的元素都是entry
           Map.Entry et = (Map.Entry) obj;
           System.out.println(et.getKey()+","+et.getValue());
      }

第三种遍历key取出对应的value

        Set set = map.keySet();
       for (Object o : set) {
           Object obj = map.get(o);
           System.out.println(o+","+obj);
      }

TreeMap

public static void main(String[] args) {
  TreeMap map = new TreeMap();
  Student s1 = new Student("Tom",23);
  Student s2 = new Student("Bob",20);
  Student s3 = new Student("Jack",18);
  Student s4 = new Student("Jerry",17);

  map.put(s1,95);
  map.put(s2,96);
  map.put(s3,94);
  map.put(s4,97);

  Set set = map.entrySet();
  Iterator iterator = set.iterator();
  while (iterator.hasNext()){
      Object obj = iterator.next();
      Map.Entry me = (Map.Entry) obj;
      System.out.println(me.getKey()+","+me.getValue());
  }
   
      private String name;
  private int age;
  public Student(){}

  public Student(String name, int age){
      this.name = name;
      this.age = age;
  }

  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 compareTo(Object o) {
      //按照年龄从小到大排序
      Student student = (Student) o;
      int num = this.age - student.age;
      //如果年龄相同那么判断名字是否相同
      int num2 = num == 0 ? this.name.compareTo(student.name) : num;
      return num2;
  }

  @Override
  public String toString() {
      return "Student{" +
              "name='" + name + '\'' +
              ", age=" + age +
              '}';
  }
=========
Student{name='Jerry', age=17},97
Student{name='Jack', age=18},94
Student{name='Bob', age=20},96
Student{name='Tom', age=23},95

Properties

/ /Properties:常用来处理配置文件。key和value都是string类型public static void main(String[] args) throws Exception {
Properties pros = new Properties();
FileInputStream fis = new FileInputstream( name:"jdbc.properties");pros. load(fis);//加载流对应的文件
string name = pros.getProperty( "name" ) ;
String password = pros.getProperty( "password" );
system.out.println("name = " + name + ", password = " + password);

Collections工具类

reverse(List):反转List中元素的顺序 shuffle(List):对List集合元素进行随机排序 sort(List):根据元素的自然顺序对指定List集合元素按升序排序 sort(List,Comparator):根据指定的Comparator一生的o对-S六施东L11/>swap(List,int,int):将指定list集合中的i处元素和j处元素进行交换

        //copy方法的正确用法        
List src = new ArrayList();
       List list = Arrays.asList(new Object[src.size()]);
       Collections.copy(list,src);

       //返回的list1就是线程安全的list
       List list1 = Collections.synchronizedList(list);

 

标签:map,name,Map,age,String,key,集合,第七天
来源: https://www.cnblogs.com/fengyvxxx/p/15805689.html