Java中set集合

来源:互联网 发布:防晒霜推荐知乎 编辑:程序博客网 时间:2024/05/16 10:07

Set集合

set集合简单使用

package com.cloud.vector;

import java.util.HashSet;

import java.util.Set;

publicclass Day2 {

/*

 --|Set 实现Set接口的集合类,无序,不可以重复

        元素添加输出的顺序不一样

*/

   publicstatic void main(String[] args) {

      Set set=new HashSet();

      set.add("Spring1");

      set.add("Spring2");

      set.add("Spring3");

      System.out.println("添加是否成功?"+set.add("Spring3"));

      System.out.println(set);

   }

}

Set集合添加元素

package com.cloud.vector;

import java.util.HashSet;

publicclass Day3 {

   publicstatic void main(String[] args) {

      HashSet<Person> set=new HashSet<Person>();

      /*添加简单元素,该元素不可重复

      set.add("Spring1");

      set.add("Spring2");

      set.add("Spring3");

      set.add("Spring3");

      */

      set.add(new Person(1,"Spring1"));

      set.add(new Person(2,"Spring2"));

      set.add(new Person(3,"Spring3"));

      System.out.println("是否添加成功?"+set.add(new Person(3,"Spring3")));

      System.out.println(set);

   }

}

class Person{

   intid;

   String name;

   public Person(int id,String name){

      this.id=id;

      this.name=name;

   }

   @Override

   public String toString() {

      return"{编号="+this.id+";名字="+this.name+"}";

   }

   @Override

   publicboolean equals(Object obj) {

      System.out.println("equals(Object obj)被调用");

      Person p=(Person) obj;

      returnthis.id==p.id;

   }

   @Override

   publicint hashCode() {

      System.out.println("===调用了hashCode()===");

      //这里修改返回id

      returnthis.id;

   }

}

String类型的比较

package com.cloud.vector;

publicclass Demo4 {

   publicstatic void main(String[] args) {

      String str1="hello";

      String str2=new String("hello");

      System.out.println("判断两个是否是相同的对象:"+(str1==str2));

      //hashCode默认情况下表示的是内存地址,但是String重写的了objecthashCode()的方法

      System.out.println("hashCode="+str1.hashCode());

      System.out.println("hashCode="+str2.hashCode());

   }

}

HashSet简单使用

package com.cloud.vector;

import java.util.HashSet;

import java.util.Scanner;

publicclass Demo5 {

   /**

    * 实现的需求:键盘录入用户名和密码,如果用户名和密码已经存在,则重复阻止添加

    */

   publicstatic void main(String[] args) {

      @SuppressWarnings("resource")

      Scanner scanner = new Scanner(System.in);

      HashSet<User> set=new HashSet<User>();

      while(true){

        System.out.println("请输入用户名:");

        String userName = scanner.next();

        System.out.println("请输入密码:");

        String password = scanner.next();

        //创建用户

        User user = new User(userName,password);

        if(set.add(user)){

           System.out.println("注册成功...");

           System.out.println("用户信息:"+set);

        }

        else{

           System.out.println("注册失败...");

        }

      }

   }

}

class User{

   String userName;

   String password;

   public User(String userName,String password){

      super();

      this.userName=userName;

      this.password=password;

   }

   @Override

   public String toString() {

      return"{用户名:"+this.userName+";密码:"+this.password;

   }

   @Override

   publicboolean equals(Object obj) {

      User user = (User)obj;

      returnthis.userName.equals(user.userName)&&this.password.equals(user.password);

   }

   @Override

   publicint hashCode() {

      returnuserName.hashCode()+password.hashCode();

   }

}

 

TreeSet简单用法

package com.cloud.vector;

import java.util.TreeSet;

publicclass Demo7 {

   publicstatic void main(String[] args) {

      TreeSet tree = new TreeSet();

      /*tree.add(4);

      tree.add(3);

      tree.add(6);

      tree.add(5);

      tree.add(2);

      //打印:[2, 3, 4, 5, 6]

      */

      tree.add('c');

      tree.add('e');

      tree.add('b');

      tree.add('a');

      //打印:[a, b, c, e]

      System.out.println(tree);

   }

}

 

TreeSet添加自定义元素

package com.cloud.vector;

import java.util.TreeSet;

publicclass Demo8 {

   /**

    * TreeSet处理自定义成员变量

    * 注意事项:

    * 1.如果元素具备自然特性,则按照元素的自然特性进行排序

    * 2.如果元素不具备自然顺序的特性,那么元素所属的类必须实现Comparable接口

    * 3.如果compareTo方法返回的是0,那么该元素视为重复元素,不允许添加

    * 4.在添加元素中,如果该元素不具备自然特性,该元素所属的类也没有实现Comparable接口,就必须

    *   在创建TreeSet的时候传入一个比较器,把元素的比较放到compare方法中即可

    *   比较器使用方法:

    *    class类名 implements Comparator{

    *    }

    * 5.在传入元素的时候,如果元素本身不具备自然特性,但是元素所属的类实现了Comparable接口,同时

    *   TreeSet对象也传入了比较器,则比较器的规则优先使用

    * 6.TreeSet可以对字符串进行比较

    *   情况1:可以找到对应不同的字符,比较的就是对应的字符

    *

    *   情况2:找不到对应的不同的字符,比较的就是字符串长度

    */

   publicstatic void main(String[] args) {

      TreeSet<Employee> tree = new TreeSet<Employee>();

      tree.add(new Employee(1,"aaa", 500));

      tree.add(new Employee(2,"bbb", 400));

      tree.add(new Employee(3,"ccc", 300));

      tree.add(new Employee(4,"ddd", 200));

      //打印薪水由低到高排序

      System.out.println(tree);

   }

}

@SuppressWarnings("rawtypes")

class Employeeimplements Comparable{

   intid;

   String name;

   intsalary;

   public Employee(int id, String name,int salary) {

      super();

      this.id = id;

      this.name = name;

      this.salary = salary;

   }

   @Override

   public String toString() {

      return"{编号:"+this.id+";姓名:"+this.name+";薪水:"+this.salary+"}";

   }

   //该方法定义元素之间的比较规则

   //比较此对象(this)与制定对象的顺序

   @Override

   publicint compareTo(Object o) {

      Employee emp=(Employee) o;

      System.out.println(this.name+""+emp.name+"比较");

      returnthis.salary-emp.salary;

   }

}

 

TreeSet中比较器的使用

package com.cloud.vector;

import java.util.Comparator;

import java.util.TreeSet;

publicclass Demo9 {

   /**

    * 比较器使用的案例

    *   比较器使用方法:

    *    class类名 implements Comparator{

    *    }

    */

   publicstatic void main(String[] args) {

      //创建一个比较器

      MyComparator mycom = new MyComparator();

      //把比较器传入

      TreeSet<Employee2> tree = new TreeSet<Employee2>(mycom);

      tree.add(new Employee2(1,"aaa", 500));

      tree.add(new Employee2(2,"bbb", 400));

      tree.add(new Employee2(3,"ccc", 300));

      tree.add(new Employee2(4,"ddd", 200));

      System.out.println(tree);

   }

}

@SuppressWarnings("rawtypes")

class Employee2/*implements Comparable*/{

   intid;

   String name;

   intsalary;

   public Employee2(int id, String name,int salary) {

      super();

      this.id = id;

      this.name = name;

      this.salary = salary;

   }

   @Override

   public String toString() {

      return"{编号:"+this.id+";姓名:"+this.name+";薪水:"+this.salary+"}";

   }

   //该方法定义元素之间的比较规则

   //比较此对象(this)与制定对象的顺序

   /*

   @Override

   publicint compareTo(Object o) {

      Employee2emp=(Employee2) o;

      System.out.println(this.name+""+emp.name+"比较");

      return this.salary-emp.salary;

   }

   */

}

//自定义一个比较器

class MyComparatorimplements Comparator{

   @Override

   publicint compare(Object o1, Object o2) {

      Employee2 emp1=(Employee2) o1;

      Employee2 emp2=(Employee2) o2;

      return emp1.id-emp2.id;

   }

}

 

TreeSet比较字符串

package com.cloud.vector;

import java.util.TreeSet;

publicclass Demo10 {

   /**

    * TreeSet比较字符串案例

    */

   publicstatic void main(String[] args) {

      TreeSet tree = new TreeSet();

      //abcabw比较的是cwabcabcd比较的是d

      tree.add("abc");

      tree.add("abcd");

      tree.add("abw");

      System.out.println(tree);

      System.out.println("a".compareTo("c"));

   }

}

 

TreeSet案例

package com.cloud.vector;

import java.util.TreeSet;

publicclass Demo11 {

   /**

    * 对字符串进行排序

    * String ='2 15 17 4 8 6 12'

    */

   publicstatic void main(String[] args) {

      String str="2 15 17 4 8 6 12";

      String[] datas = str.split(" ");

      TreeSet tree = new TreeSet();

      for(int i=0;i<datas.length;i++){

        tree.add(Integer.parseInt(datas[i]));

      }

      System.out.println(tree);

   }

}


0 0
原创粉丝点击