Bitmap thought and its related realization in Java __java

Source: Internet
Author: User

First look at a topic:

I'll give you a list of the phone numbers in Xian, probably in tens,

Requires that all duplicate phone numbers be found from, requiring as little time complexity as possible.

At present XI ' an city's telephone number probably starts with 8, is 8 digits, namely

Similar to 82678578 like this

Double violence Search time complexity is too high, we do not consider here.

An easy way to think is to create an array of flags,

int Boolean all rows, use the corresponding position value to replace whether this number appears,

The efficiency is improved according to the direct access characteristics of the array.

But have you ever thought or tested

Int[] A = new int[100000000];

Boolean[] A = new boolean[100000000];

Whether such a similar statement can be compiled and executed.

If you think about it, you'll find that the int field is too much space and we just need to know if the number exists.

So the simplest 0 and 1 are enough to represent the smallest storage unit for 0 and 1.

is one of the memory.

OK, this is the thought of bitmap.

By removing the phone number from the beginning of 8, you can map it to an array of 1 to 10000000.

8bit is 1byte,1024byte is 1kb,1024kb is 1MB

So the space occupied by the 10 million bit is

10000000/8/1024/1024MB is about 1MB more,

It's a piece of cake for the memory of a few g now.

At the same time, Java also has a corresponding implementation, Java.util.BitSet,

It's completely tailored to this Java class.

This class starts with jdk1.0, but some of these methods are jdk1.4.

Be careful when you use it.

In addition, Bitset is not thread-safe and requires external synchronization.

The following simple code gives an example of Bitset:

Creates a value of 10 million-bit bitset initial all bits is false java.util.BitSet Bitset = new Java.util.BitSet (10000000); Sets the value of the specified bit to True Bitset.set (9999, true); The output refers to the value of the position System.out.println (Bitset.get (9999)); SYSTEM.OUT.PRINTLN (Bitset.get (9998));

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.