Home Database Mysql Tutorial 要点Java20java.util.Collections

要点Java20java.util.Collections

Jun 07, 2016 pm 03:56 PM
Main points

java.util.Collections 集合帮助类 示例程序(JUnit演示) 排序 @Test public void testSort() { ListInteger demoList = new ArrayListInteger(Arrays.asList(3, 2, 1)); assertEquals(3, demoList.get(0).intValue()); //public static T extends Comparable

java.util.Collections 集合帮助类

示例程序(JUnit演示)

排序

<code class="java">    @Test
    public void testSort() {
        List<Integer> demoList = new ArrayList<Integer>(Arrays.asList(3, 2, 1));
        assertEquals(3, demoList.get(0).intValue());

        //public static <T extends Comparable<? super T>> void sort(List<T> list)
        Collections.sort(demoList);

        assertEquals(1, demoList.get(0).intValue());

        //public static void shuffle(List<?> list) // 随机排序
        //public static void reverse(List<?> list) // 反序          
    }
Copy after login

查找

<code class="java">    @Test
    public void testBinarySearch() {
        List<Integer> demoList = new ArrayList<Integer>(Arrays.asList(3, 2, 1));
        //必须先排序
        Collections.sort(demoList);
        assertEquals(1, demoList.get(0).intValue());

        //二分查找位置
        //public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key)
        assertEquals(0, Collections.binarySearch(demoList, 1));

        //public static int indexOfSubList(List<?> source, List<?> target)
        // public static int lastIndexOfSubList(List<?> source, List<?> target)
    }
Copy after login

置换

<code class="java     ">    @Test
    public void testSwap() {
        List<Integer> demoList = new ArrayList<Integer>(Arrays.asList(3, 2, 1));

        //public static void swap(List<?> list, int i, int j)
        Collections.swap(demoList, 0, 2);
        assertEquals(1, demoList.get(0).intValue());
        //public static void rotate(List<?> list, int distance)  //指定距离轮换
        //public static <T> boolean replaceAll(List<T> list, T oldVal, T newVal)  //指定替换
    }
Copy after login

拷贝

<code class="java     ">    @Test
    public void testCopy() {
        List<Integer> demoList = new ArrayList<Integer>(Arrays.asList(3, 2, 1));

        //public static <T> void copy(List<? super T> dest, List<? extends T> src)
        //注意目的List的size最少要等于src的size
        List<Integer> copyList = new ArrayList<Integer>(Arrays.asList(1, 2, 3));
        Collections.copy(copyList, demoList);
        assertEquals(3, copyList.size());
        assertEquals(3, copyList.get(0).intValue());

        copyList.add(4);
        assertEquals(3, demoList.size());
    }
Copy after login

比较

<code class="java     ">    @Test
    public void testCompary() {
        List<Integer> demoList = new ArrayList<Integer>(Arrays.asList(3, 2, 1));
        assertEquals(1, Collections.min(demoList).intValue());
        assertEquals(3, Collections.max(demoList).intValue());

        List<Integer> compareList = new ArrayList<Integer>(Arrays.asList(5, 6, 7));
        //Returns true if the two specified collections have no elements in common
        assertTrue(Collections.disjoint(demoList, compareList));
    }
Copy after login

创造不同的集合

<code class="java     ">    @SuppressWarnings("unused")
    @Test
    public void testCreate() {
        List<Integer> demoList = new ArrayList<Integer>(Arrays.asList(3, 2, 1));

        //空对象 size=0  无添加方法
        List<Integer> emptyList = Collections.emptyList();

        //返回一个只包含指定对象的不可变列表。
        List<Integer> singletonList = Collections.singletonList(1);

        //返回指定列表的一个动态类型安全视图。
        List<Integer> checkedList = Collections.checkedList(demoList, Integer.class);

        //返回指定列表的不可修改视图。
        List<Integer> unmodifiableList = Collections.unmodifiableList(demoList);

        //返回指定列表支持的同步(线程安全的)列表。
        List<Integer> synchronizedList = Collections.synchronizedList(demoList);

        synchronized (synchronizedList) {
            Iterator<Integer> i = synchronizedList.iterator(); // Must be in synchronized block
            while (i.hasNext())
                i.next();
        }

        //map set sortedMap sortedSet
    }
Copy after login

环境 jdk1.6 window7 junit4

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1253
29
C# Tutorial
1228
24
Key points to learn Golang asynchronous programming Key points to learn Golang asynchronous programming Mar 18, 2024 am 08:36 AM

Key Points to Learn Golang Asynchronous Programming In modern software development, asynchronous programming has become more and more important because it can improve the performance and responsiveness of the program. As an efficient programming language, Golang also provides a wealth of tools and features to support asynchronous programming. This article will introduce the key points of learning Golang asynchronous programming and provide specific code examples. 1.Goroutine In Golang, Goroutine is the key element to achieve asynchronous programming. Gorouti

What are the key points in making ppt for Dragon Boat Festival? What are the key points in making ppt for Dragon Boat Festival? Mar 20, 2024 am 11:58 AM

No matter in life or work, we often celebrate various festivals together. At this time, our organizers usually prepare a holiday-themed ppt. So how should we complete the Dragon Boat Festival ppt if we want to make it? Let’s discuss the basic outline and key points of making a Dragon Boat Festival ppt! 1. For general festival ppts, we must first introduce the festival, including the source of allusions, related poems, customary activities, and the evolution of contemporary culture, etc. We usually divide it into several ppts to complete these general parts. After determining the general content structure, we can choose a suitable background, such as the theme of the Dragon Boat Festival, including elements such as Qu Yuan, dragon boats, and rice dumplings. 3. Then we can insert some melodious background music. The Dragon Boat Festival is a commemorative festival. The background

Master the key points and techniques of PHP development Master the key points and techniques of PHP development Sep 11, 2023 am 09:28 AM

Master the key points and technologies of PHP development. With the rapid development of the Internet, the field of Web development is becoming increasingly important. As one of the mainstream languages ​​for web development, PHP excels in its flexibility and powerful features. Whether you are a beginner or an experienced developer, you should master the key points and techniques in PHP development to improve development efficiency and quality. This article will introduce some key points and techniques to help you better master PHP development. 1. Understand the basic knowledge of PHP. Before using PHP for development, you must first

Master the key points and learning paths of Java software programming Master the key points and learning paths of Java software programming Feb 21, 2024 pm 04:39 PM

Master the key points and learning paths of Java software programming. Java is a high-level programming language widely used in software development. Its simple, stable and object-oriented features make it the first choice for developers. In order to become an excellent Java developer, in addition to mastering basic programming syntax knowledge, you also need to understand some key points and reasonable learning paths. 1. Master basic knowledge Before starting to learn Java programming, you first need to master some basic programming knowledge and concepts. This includes variables, data types, controls

How to quickly master the key points of Java function development How to quickly master the key points of Java function development Aug 05, 2023 pm 12:09 PM

How to quickly master the key points of Java function development. As a Java developer, it is very important to master the key points of Java function development. With the widespread application and continuous development of Java, mastering the core points of Java function development will help you complete project development quickly and efficiently. This article will introduce you to some key points and provide code examples to help you understand better. Learn the basics of Java Before starting to develop Java functions, you first need to master the basics of Java. This includes mastering Jav

Overview and key points of PHP live broadcast function development Overview and key points of PHP live broadcast function development May 22, 2023 pm 12:51 PM

With the popularity of network technology and mobile devices, live broadcast has become a popular Internet application. As a PHP developer, understanding and mastering the development of live broadcast functions is one of the indispensable skills. This article will introduce you to the overview and key points of PHP live broadcast function development. 1. Basic knowledge of live broadcast function development Before starting the development of live broadcast function, we need to understand some basic knowledge. First of all, live broadcast requires an audio and video streaming server to carry it. Commonly used ones include Wowza, Nginx-rtmp, SRS, etc. Secondly

What are the key elements that reveal sticky positioning? Revealing the key takeaways for achieving sticky positioning What are the key elements that reveal sticky positioning? Revealing the key takeaways for achieving sticky positioning Jan 28, 2024 am 10:17 AM

What are the key factors in studying sticky positioning? Key points for exploring sticky positioning Stickiness positioning refers to the way in marketing where a company creates and maintains a positive and lasting relationship with consumers, making it the brand of choice in the minds of consumers. In today's highly competitive market environment with numerous consumer choices, sticky positioning has become one of the key elements for companies to gain long-term competitive advantage. So, what are the key factors in studying sticky positioning? Now let’s explore sticky positioning

Analysis of the key points in Golang architecture, do you know what they are? Analysis of the key points in Golang architecture, do you know what they are? Mar 02, 2024 pm 06:09 PM

Analysis of the key points in Golang architecture, do you know what they are? In today's era of rapid development of the Internet, various programming languages ​​are emerging, and one of the languages ​​that has attracted much attention is the Go language (Golang). It is favored by more and more developers because of its simplicity, efficiency, powerful concurrency performance, and excellent tool chain. In the process of developing projects using Go language, reasonable architectural design is a crucial part. In this article, several key points of Golang architecture will be analyzed,

See all articles