Java SE入门及基础(53)& Set 接口

目录

Set 接口

1. 特性描述

示例及源码解读

2. HashSet

示例及源码解读

3. TreeSet

示例及源码解读

4. LinkedHashSet

示例及源码解读


Set 接口

1. 特性描述

        A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited. Set also adds a stronger contract on the behavior of the equals and hashCode operations, allowing Set instances to be comparedmeaningfully even if their implementation types differ. Two Set instances are equal if they contain the same elements.
        集合是一个集合,不能包含重复的元素。 它为数学集合抽象建模。 Set 接口仅包含 Collection 继承的方法,并增加了禁止重复元素的限制。 Set 还为 equals hashCode 操作的行为增加了更紧密的约定,即使它们的实现类型不同,也可以有意义地比较Set 实例。 如果两个 Set 实例包含相同的元素,则它们相等。
        The Java platform contains three general-purpose Set implementations:
HashSet, TreeSet, and LinkedHashSet. HashSet, which stores its elements in a
hash table, is the best-performing implementation; however it makes no
guarantees concerning the order of iteration.
        Java平台包含三个通用的 Set 实现: HashSet TreeSet LinkedHashSet HashSet 将其元素存储在哈希表中,是性能最好的实现。 但是,它不能保证迭代的顺序。
示例及源码解读
package com . wq . set ;
import java . util . HashSet ;
import java . util . Iterator ;
public class HashSetTest {
        public static void main ( String [] args ) {
                HashSet < String > set = new HashSet <> ();
                set . add ( "a" );
                set . add ( "a" );
                set . add ( "b" );
                System . out . println ( set . size ());
                for ( String str : set ){
                        System . out . println ( str );
                }
                HashSet < String > hashSet = new HashSet <> ();
                hashSet . add ( "C" );
                hashSet . add ( "D" );
                hashSet . add ( "E" );
                set . addAll ( hashSet );
                System . out . println ( "===================" );
                for ( String str : set ){
                        System . out . println ( str );
                }
                System . out . println ( set . contains ( "C" ));
                System . out . println ( set . remove ( "E" ));
                Iterator < String > iterator = set . iterator ();
                while ( iterator . hasNext ()){
                        System . out . println ( iterator . next ());
                }
        }
}

2. HashSet

        This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.
        此类实现Set 接口,该接口由哈希表(实际上是 HashMap 实例)支持。 它不保证集合的迭代顺序。 特别是,它不能保证顺序会随着时间的推移保持恒定。 此类允许使用null 元素。
示例及源码解读
import java . util . HashSet ;
import java . util . Iterator ;
public class HashSetTest {
        public static void main ( String [] args ) {
                HashSet < String > set = new HashSet <> ();
                set . add ( "a" );
                set . add ( "a" );
                set . add ( "b" );
                System . out . println ( set . size ());
                for ( String str : set ){
                        System . out . println ( str );
                }
                HashSet < String > hashSet = new HashSet <> ();
                hashSet . add ( "C" );
                hashSet . add ( "D" );
                hashSet . add ( "E" );
                set . addAll ( hashSet );
                System . out . println ( "===================" );
                for ( String str : set ){
                        System . out . println ( str );
                }
                System . out . println ( set . contains ( "C" ));
                System . out . println ( set . remove ( "E" ));
                Iterator < String > iterator = set . iterator ();
                while ( iterator . hasNext ()){
                        System . out . println ( iterator . next ());
                }
        }
}

3. TreeSet

        A NavigableSet implementation based on a TreeMap. The elements are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used.
        基于TreeMap NavigableSet 实现。 元素使用其自然顺序或在集合创建时提供的 Comparator 进行排序,具体取决于所使用的构造方法。
示例及源码解读
package com .wq . set ;
public class Car /*implements Comparable<Car>*/ {
        private String brand ;
        private double price ;
        public Car ( String brand , double price ) {
                this . brand = brand ;
                this . price = price ;
        }
        public String getBrand () {
                return brand ;
        }
        public void setBrand ( String brand ) {
                this . brand = brand ;
        }
        public double getPrice () {
                return price ;
        }
        public void setPrice ( double price ) {
                this . price = price ;
        }
        @Override
        public String toString () {
                return "Car{" +
                        "brand='" + brand + '\'' +
                        ", price=" + price +
                        '}' ;
        }
        // @Override
        // public int compareTo(Car o) {
                // return Double.compare(price, o.price);
        // }
}
package com . wq . set ;
import java . util . Comparator ;
import java . util . TreeSet ;
public class TreeMapTest {
        public static void main ( String [] args ) {
                // TreeSet<Car> cars = new TreeSet<>();
                // cars.add(new Car("奥迪", 100000));
                // cars.add(new Car("保时捷", 150000));
                // cars.add(new Car("大众", 50000));
                // for(Car c: cars){
                        // System.out.println(c);
                // }
                Comparator < Car > c = ( c1 , c2 ) -> Double . compare ( c1 . getPrice (), c2 . getPrice ());
                TreeSet < Car > cars = new TreeSet <> ( c );
                cars . add ( new Car ( " 奥迪 " , 100000 ));
                cars . add ( new Car ( " 保时捷 " , 150000 ));
                cars . add ( new Car ( " 大众 " , 50000 ));
                for ( Car car : cars ){
                        System . out . println ( car );
                }
        }
}

4. LinkedHashSet

        Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order). Note that insertion order is not affected if an element is re-inserted into the set.
        Set接口的哈希表和链表实现,具有可预测的迭代顺序。 此实现与 HashSet 的不同之处在于,它维护在其所有条目中运行的双向链接列表。 此链表定义了迭代顺序,即将元素插入到集合中的顺序(插入顺序)。 请注意,如果将元素重新插入到集合中,则插入顺序不会受到影响。
示例及源码解读
import java . util . LinkedHashSet ;
public class LinkedHashSetTest {
        public static void main ( String [] args ) {
                LinkedHashSet < String > strings = new LinkedHashSet <> ();
                strings . add ( "D" );
                strings . add ( "C" );
                strings . add ( "B" );
                strings . add ( "A" );
                for ( String str : strings ){
                        System . out . println ( str );
                }
        }
}
  • 10
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值