Java Basic _ Set List and Set interfaces, java Set listset

Source: Internet
Author: User

Java Basic _ Set List and Set interfaces, java Set listset

  • Several Methods for listing collections need to be mastered
  • AddFrist () adds an element to the first part of the set.
  • AddLast () adds an element at the end of the Set
  • RemoveFirst () removes the element of the first part of the set.
  • RemoveLast () removes the elements at the end of the set.
  • GetFrist () gets the element of the first part of the set.
  • GetLast () gets the element at the end of the set.
  • IsEmpty () determines whether the set is empty
  • LinkedList<String> linkedList = new LinkedList<>();linkedList.add("a");linkedList.add("a");linkedList.add("b");linkedList.add("a");linkedList.addFirst("0");linkedList.addLast("9");System.out.println(linkedList);linkedList.removeLast();System.out.println(linkedList);linkedList.removeFirst();System.out.println(linkedList);String first = linkedList.getFirst();System.out.println(first);String last = linkedList.getLast();System.out.println(last);boolean b = linkedList.isEmpty();System.out.println(b);
  •  

  • Set interface (unordered, non-repeated)
  • We can directly learn the HashSet interface.
  • HashSet: When you store the same element, if it is not added, it will determine whether the collection exists. Here we use the custom reference type description.
  • Package com. set; public class Person {private String name; private int 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;} public Person (String name, int age) {super (); this. name = name; this. age = age;} public Person () {super (); // TODO Auto-generated constructo R stub} @ 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; Person other = (Person) 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 ;}@ Override public String toString () {return "Person [name =" + name + ", age = "+ age +"] ";}}

    Note that we need to overwrite the hashcld method and the equlas () method. Of course, we need to overwrite the toString () method during printing.

  • Public static void main (String [] args) {HashSet <Person> hashSet = new HashSet <> (); hashSet. add (new Person ("Zhang San", 12); hashSet. add (new Person ("Li Si", 12); hashSet. add (new Person ("Zhang San", 12); hashSet. add (new Person ("Zhang San", 21); // System. out. println (hashSet); Iterator <Person> iterator = hashSet. iterator (); while (iterator. hasNext () {Person next = iterator. next (); System. out. println (next );}}}
    // Execution result: James 12
    Li Si 12
    Zhang San 21

     

  • This means that the de-duplicated elements of HashSet are unordered.
  • The Set interface also has a Set of sorted hashsets, which makes up for the unordered Set.
  • Public class Demo02_LinkedHashSet {public static void main (String [] args) {LinkedHashSet <String> hashSet = new LinkedHashSet <> (); hashSet. add ("a"); hashSet. add ("B"); hashSet. add ("c"); hashSet. add ("d"); // enhanced for traversal for (String string: hashSet) {System. out. print (string + "");} // Iterator traverses iterator <String> Iterator = hashSet. iterator (); while (iterator. hasNext () {String str = iterator. next (); System. out. println (str);} System. out. println (hashSet );}

     

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.