What are the commonly used collection classes in java

王林
Release: 2019-12-02 17:04:31
Original
4171 people have browsed it

What are the commonly used collection classes in java

1. Set collection

The main implementation classes areHashSet,TreeSetto store references to objects, which are not allowed Duplicate objects.

Example code:

public class SetTest {     public static void main(String[] args) {           Set set=new HashSet();       //添加数据           set.add("abc");           set.add("cba");           set.add("abc");//故意重复           set.add(123);           set.add(true);            System.out.println("集合元素个数:"+set.size());       //遍历出集合中每一个元素            Iterator it=set.iterator();            while(it.hasNext()){               System.out.println(it.next());            }          }   }
Copy after login

Judged by the equals() method of java. If you have special needs, you must overload the equals() method.

1.HashSet(), call the hashCode() method of the object, obtain the hash code, and then calculate the location of the object in the set. Determine whether there are duplicates by comparing the hash code with the equals() method. Therefore, if you overload the equals() method, you must also overload the hashCode() method.

Recommended video tutorials:java online learning

2.TreeSet(), inherits theShortedSetinterface, and can Sorting of objects in a collection. The default sorting method is natural sorting, but this method can only sort objects that implement the Comparable interface. This interface is implemented in Java for numerical and character objects such as Integer, Byte, Double, Character, and String.

If there is special sorting, you must overload thecompareTo()method under this interface or construct a collection through the implementation class of theComparatorinterface.

2. List collection

The main implementation classes areLinkedListandArrayList. The former implements the linked list structure and the latter can represent variable sizes. array.

The characteristic of List is that it can store objects in a linear manner and allows the storage of duplicate objects. List can be sorted using the static method sort of the Collections class. sort(List list) natural sorting;sort(List listm, Comparator codddmparator)Customized sorting.

Example code:

List: linear collection interface, ordered;

ArrayList: dynamic array [variable length Dynamic array];

LinkedList: A collection of linked list structures.

  public class ListTest {     //ArrayList          static void testOne(){           List list=new ArrayList();       //添加数据            list.add("abc");            list.add("cba");           list.add(123);            list.add(0,"fist");       //查看集合长度            System.out.println("存放"+list.size()+"个元素");            list.remove(0);//删除第一个元素       //查看集合中是否包含cba            if(list.contains("cba")){             System.out.println("包含元素cba");         }       //取出集合中第二个元素            System.out.println("第二个元素是:"+list.get(1));       //取出集合中所有元素            for (int i = 0; i < list.size(); i++) {              System.out.println(list.get(i));           }          }       //LinkedList           static void testTwo(){           LinkedList list=new LinkedList();      //添加元素           list.add("aaaa");           list.add(123123);           list.addFirst("1111111");           list.addLast("2222222");           list.add("33333333");            System.out.println("元素个数:"+list.size());      //取出第三个元素           System.out.println("第三个元素是:"+list.get(2));       //第一个元素           System.out.println("第一个元素:"+list.getFirst());           System.out.println("最后一个元素:"+list.getLast());       //删除第一个元素           list.removeFirst();            for (Object object : list) {             System.out.println(object);           }          }          public static void main(String[] args) {     //testOne();          testTwo();          }       }
Copy after login

3. Map collection.

The main implementation classes areHashMapandTreeMap. Map does not require uniqueness for values, but requires uniqueness for keys. If an existing key is added, the original value object will be overwritten.

The HashMap class accesses key objects according to the hash algorithm. You can overload theequals()andhashCode()methods to compare keys, but they must be consistent. .TreeMap, can be sorted naturally, or a TreeMap can be constructed by passing the implementation class ofComparator.

Map: A collection of key-value pair storage structures, unordered.

Example code:

public class MapTest {     public static void main(String[] args) {       //实例化一个集合对象            Map map=new HashMap();       //添加数据            map.put("P01", "zhangSan");            map.put("P02", "Lucy");            map.put("PSex", "男");            map.put("PAge", "39");            map.put("PAge", "22");//key,重复会被后面的覆盖       //判断是否有一个key为PSex            if(map.containsKey("PSex")){              System.out.println("存在");            }           System.out.println("集合大小:"+map.size());           System.out.println("输出key为PAge的值:"+map.get("PAge"));       //遍历出Map集合中所有数据           Iterator it=map.keySet().iterator();           while(it.hasNext()){            String key=it.next().toString();            System.out.println("key="+key+",value="+map.get(key));         }          /*           Set set=map.keySet();//取出map中所有的key并封装到set集合中           Iterator it=set.iterator();           while(it.hasNext()){            String key=it.next().toString();            System.out.println("key="+key+",value="+map.get(key));         }           */         }       }
Copy after login

Recommended related articles and tutorials:java entry program

The above is the detailed content of What are the commonly used collection classes in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!