In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "case Analysis of java set principle". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn the "java set principle case analysis" bar!
I. Overview
Collection is a kind of data container with variable length, various data structures and various storage objects. Collections in Java can be divided into List collections, Set collections, HashMap collections, and so on.
Java Collection Architecture:
II. Collection
Collection is the top-level interface for all value storage collections in Java, so all its direct or indirect implementation classes have their non-private methods, and we can start with its methods to understand the functional implementation of this system.
Boolean add (E e) ensures that this collection contains the specified element. Boolean addAll (Collection c) returns true if this collection contains all the elements in the specified collection. Boolean equals (Object o) compares whether this collection is equal to the specified object. Int hashCode () returns the hash code value of this collection. Boolean isEmpty () returns true if the collection contains no elements. Iterator iterator () returns an iterator that iterates over the elements of this collection. Boolean remove (Object o) removes a single instance of the specified element from the collection, if it exists. Boolean removeAll (Collection c) removes all elements in this collection that are also contained in the specified collection. Boolean retainAll (Collection c) retains only those elements in this collection that are also contained in the specified collection. Int size () returns the number of elements in this collection. Object [] toArray () returns an array containing all the elements in this collection. T [] toArray (T [] a) returns an array containing all the elements in this collection; the runtime type of the returned array is the same as the runtime type of the specified array. 1 、 List
List, a single-column collection, stores a set of data that is inserted in order, and the data can be repeated.
List collection
LinkedList
ArrayList
1) ArrayList
Example:
Public class CollectionTest {public static void main (String [] args) {List list = new ArrayList (); / / add an element, boolean add (E) ensures that this collection contains the specified element list.add ("Zhang San"); list.add (1); list.add ('A'); System.out.println (list) / / [Zhang San, 1, A] / / boolean addAll (Collection c): delete all elements in the specified collection from the collection set1.add ("aaa"); set1.add ("linux"); set.removeAll (set1); System.out.println (set); / / void clear (): clear all elements in the collection set.clear () System.out.println (set); / / int size (): get the number of elements in the collection int size = set.size (); System.out.println (size); / / boolean contains (Object o): determine whether the collection contains the specified element, including true, otherwise false; System.out.println (set.contains ("aaa")) / / boolean isEmpty (): determine whether the collection is empty System.out.println (set.isEmpty ());}
Note: when HashSet adds elements, it will first compare whether the hashCode values of the two elements are not equal, if the results are not equal, add them directly; if they are equal, then judge whether the equals values of the two elements are equal, if they are equal, do not add them, and add them if they are not equal.
2) TreeSet
TreeSet and TreeMap adopt the storage structure of red-black tree
Features: orderly, query speed faster than List
With TreeSet collections, objects must be comparable. There are two ways to make objects comparable:
The first: implement the Comparable interface and override the compareTo () method:
Second: write a comparator class that implements the Comparator interface and overrides the comare () method.
Example:
1. Entity class public class Student implements Comparable {private String name; private int age; private String sex; private int height; public Student () {} public Student (String name, int age, String sex, int height) {this.name = name; this.age = age; this.sex = sex; this.height = height;} 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;} public String getSex () {return sex;} public void setSex (String sex) {this.sex = sex;} public int getHeight () {return height } public void setHeight (int height) {this.height = height;} @ Override public boolean equals (Object o) {if (this = = o) return true; if (o = = null | | getClass ()! = o.getClass ()) return false; Student student = (Student) o Return age = = student.age & & height = = student.height & & Objects.equals (name, student.name) & & Objects.equals (sex, student.sex);} @ Override public int hashCode () {return Objects.hash (name, age, sex, height) } @ Override public String toString () {return "Student {" + "name='" + name +'\'+ ", age=" + age + ", sex='" + sex +'\'+ ", height=" + height +'}' @ Override public int compareTo (Student stu) {if (stu.getAge () > this.getAge ()) {return 1;} if (stu.getAge () < this.getAge ()) {return-1;} return stu.getName () .compareTo (this.getName ());}} 2. Test class: public class TreeSetTest {public static void main (String [] args) {TreeSet treeSet = new TreeSet (); Student student1 = new Student ("Zhang San", 20,165); Student student2 = new Student ("Li Si", 21, "male", 170); Student student3 = new Student ("Wang Wu", 19, "female", 160) Student student4 = new Student; Student student5 = new Student (Tian Qi, 20, male); treeSet.add (student1); treeSet.add (student2); treeSet.add (student3); treeSet.add (student4); treeSet.add (student5); System.out.println (treeSet);}} 3. Entity class public class Teacher {private String name; public Teacher () {} public Teacher (String name) {this.name = name;} public String getName () {return name;} public void setName (String name) {this.name = name @ Override public String toString () {return "Teacher {" + "name='" + name +'\'+'}';}} 4. Test class public class TreeSetTest2 {public static void main (String [] args) {Teacher teacher1 = new Teacher ("11"); Teacher teacher2 = new Teacher ("12"); Teacher teacher3 = new Teacher ("13"); Teacher teacher4 = new Teacher ("14"); Teacher teacher5 = new Teacher ("15") TreeSet treeSet1 = new TreeSet (new Comparator () {@ Override public int compare (Object o1, Object O2) {return o1.hashCode ()-o2.hashCode ();}}); treeSet1.add (teacher1); treeSet1.add (teacher2); treeSet1.add (teacher3); treeSet1.add (teacher4); treeSet1.add (teacher5) System.out.println (treeSet1);}}
Note: HashSet deweighting relies on hashCode and equals () methods, while TreeSet deweighting relies on comparators.
At this point, I believe that everyone on the "java set principle example analysis" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.