当前位置:网站首页>Collection and method of traversing collection elements (1)
Collection and method of traversing collection elements (1)
2022-06-30 06:18:00 【Java shaping】
Catalog
One 、 An overview of the set framework
3、 ... and 、Collection Testing of methods declared in the interface
Four 、JDK 5.0 Added foreach loop , Used to traverse a collection , Array
5、 ... and 、 Using Iterators Interator Interface traverses collection elements
One 、 An overview of the set framework
1. aggregate 、 Arrays are structures that store multiple data , abbreviation java Containers explain : The storage at this time , Mainly refers to memory level storage , It doesn't involve persistent storage (.txt .jpg .avi In the database ) 2.1 The characteristics of arrays in storing multiple data : >— Once initialized , The length is determined . > Once the array is defined , The type of its elements is determined . We can only manipulate the data of the specified type . such as : String[] arr;int[] arr1; 2.2 The disadvantage of arrays in storing multiple data : >— Once initialized , Its length cannot be modified > The methods provided in the array are very limited , For adding 、 Delete 、 Inserting data is very inconvenient , The efficiency is not high > There is no ready-made attribute or method in the array to represent the number of stored elements > The characteristics of storing data in arrays : Orderly 、 repeatable . For unordered and unrepeatable requirements , Can't meet
Two 、 Collections framework
/----Collection Interface : Single column set , Objects used to store one by one /----List Interface : Stored in order , Repeatable data ( The dynamic array ) /----ArrayList、LinkedList、Vector /----Set Interface : Disordered storage , Non repeatable data ( Similar to the collection of high school ) /----HashSet、LinkedHashSet、TreeSet /----Map Interface : Double column set , Used to store one-to-one data .(key、value)( function ) /----HashMap、LinkedHashMap、TreeMap、Hashtable、Properties
3、 ... and 、Collection Testing of methods declared in the interface
requirement :Collection The implementation class of the interface needs to be rewritten equals(), Determine whether an element is included , Remove elements , lookup , All need to use contains(Object obj): Determine whether the set contains this obj containsAll(Collection coll1): Judging formal parameters coll1 Whether all elements in are in the current collection remove(Object obj): Delete... From the current collection obj Elements removeAll(Collection coll1): Difference set : Delete... From the current collection coll1 All elements in retainAll(Collection coll1): intersection : Keep the current set and coll1 Common elements equals(Collection coll1): Determine whether the elements of two sets are equal , We need to consider whether the set is ordered or unordered hashCode(): Returns the hash value of the current object toArray(): aggregate ---> Array Array ---> aggregate : call Arrays Class asList Static methods
public class CollectionTest1 {
@Test
public void test1() {
Collection c = new ArrayList();
c.add(123);
c.add(456);
c.add(new String("haiyu"));
c.add(new Person(12, "haiuyu"));
c.add(false);
boolean contains = c.contains(123);
System.out.println(contains);//true
System.out.println(c.contains(new String("haiyu")));//true
System.out.println(c.contains(new Person(12, "haiuyu")));//false
// summary :contains amount to equals,String Rewritten in equals, Compared to the content . The custom class has not been overridden .
// containsAll(Collection coll1): Judging formal parameters coll1 Whether all elements in are in the current collection
Collection coll1 = Arrays.asList(123, 456);
boolean b = c.containsAll(coll1);
System.out.println(b);//true
System.out.println("*************");
c.remove(123);
System.out.println(c);
c.removeAll(coll1);
System.out.println(c);
}
@Test
public void test2() {
Collection c = new ArrayList();
c.add(123);
c.add(456);
c.add(new String("haiyu"));
c.add(new Person(12, "haiuyu"));
c.add(false);
Collection coll1 = Arrays.asList(123, 456, 38383, 3);
c.retainAll(coll1);
System.out.println(c);
System.out.println(c.hashCode());//5230
Object[] a = c.toArray();
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
// Array ---> aggregate : call Arrays Class asList Static methods
List<String> list = Arrays.asList(new String[]{"aa", "bb", "cc"});//list Namely collection
System.out.println(list);
List<int[]> ints = Arrays.asList(new int[]{123, 456});
System.out.println(ints.size());//1
List<Integer> integers = Arrays.asList(new Integer[]{123, 456});
System.out.println(integers.size());//2
}
}
Four 、JDK 5.0 Added foreach loop , Used to traverse a collection , Array
public class ForTest {
@Test
public void test1() {
Collection c = new ArrayList();
c.add(123);
c.add(456);
c.add(new String("haiyu"));
c.add(new Person(12, "haiuyu"));
c.add(false);
//for( The type of collection element local variable : A collection of objects ) for( The type of array element local variable : The array itself )
// Iterators that are still called internally
for (Object obj:c){
System.out.println(obj);
}
}
5、 ... and 、 Using Iterators Interator Interface traverses collection elements
1、 There are two internal approaches :hasNext() next() 2、 There's another one inside remove() Method , It can be traversed , Delete elements from the collection , This method is different from a collection calling directly remove()
public class IteratorTest {
@Test
public void test1(){
Collection c = new ArrayList();
c.add(123);
c.add(456);
c.add(new String("haiyu"));
c.add(new Person(12,"haiuyu"));
c.add(false);
Iterator iterator = c.iterator();// It can be understood as having a pointer , Point to an empty place before the first element of the collection
// Mode one :
// System.out.println(iterator.next());
// System.out.println(iterator.next());
// System.out.println(iterator.next());
// System.out.println(iterator.next());
// System.out.println(iterator.next());
// System.out.println(iterator.next()); NoSuchElementException
// Mode two : Not recommended
// for (int i = 0; i < c.size(); i++) {
// System.out.println(iterator.next());
// }
// Mode three : recommend
while (iterator.hasNext()){// It can't be c.iterator.hasNext(), Because every time a new pointer structure appears
System.out.println(iterator.next());
}
}
边栏推荐
- Go pack and unpack
- 880. 索引处的解码字符串
- Golang之手写web框架
- Base64详解:玩转图片Base64编码
- DXP copper laying settings
- Jgaurora A8 configuration file
- Configure the user to log in to the device through telnet -- AAA local authentication
- [regular expression series] greedy and non greedy patterns
- 我做功能测试这么多年的心得
- Swoole process model diagram
猜你喜欢
Golang's handwritten Web Framework
PC viewing WiFi password
Strlen and sizeof, array length and string length, don't be silly
Traitement d'images 7 - amélioration d'images
Using C language pure for loop to implement ilovey
Gestion des utilisateurs de la base de données MySQL
Idea add database
Use and principle of completionservice (source code analysis)
Develop stylelint rules from zero (plug-ins)
Use of observer mode and status mode in actual work
随机推荐
Picture.....
MySQL advanced SQL statement
DXP copper laying settings
46. full arrangement -dfs double hundred code
Image processing 7- image enhancement
ES6 extended operator (...)
观察者模式、状态模式在实际工作中的使用
Idea run SQL file
880. 索引处的解码字符串
Usage of case, casez and casex statements in Verilog
旋转标注工具roLabelImg
File operation io-part1
谁不想要一个自己的博客网站呢 - 搭建博客网站wordpress
Cocos studio3.1 installation package win
Configure the user to log in to the device through telnet -- AAA local authentication
A complete performance test process
Installation and initialization of MariaDB database
Zibll sub theme v6.4.1wordpress open source download_ Crack the original / use it directly / no tutorial required
MySQL log management, data backup and recovery
MySQL日志管理、数据备份、恢复