The main record is the use of Redis's setbit.
- A string that we can save with Redis's setbit.
Import Org.junit.test;import Redis.clients.jedis.jedis;import Redis.clients.jedis.jedispool;import Redis.clients.jedis.jedispoolconfig;import Java.util.bitset;public class Bitmaptest {private static final String REDIS _path= "127.0.0.1"; IP address private static final int redis_port=6379; Port number private static final String redis_auth= "iostream"; private static final Jedispool Jedispool; static {//config Jedispool jedispoolconfig config=new jedispoolconfig (); Config.setmaxtotal (10); final int timeout=2000; Jedispool=new Jedispool (Config,redis_path,redis_port,timeout,redis_auth); } private static int pos=0; POS is the first bit that records the next character from which the first bit of the POS is 0, then the second character E's POS is 8, which accumulates up. private static void Solve (String Binstr,jedis Jedis) {//Because we use integer.tobinarystring to omit the high of 0//So we need to make 0 first. StringBuilder sb=new StringBuilder (); if (Binstr.length () <8) {for (int i=0;i<8-binstr.length (); ++i) {SB. append (0); } sb.append (BINSTR); } binstr=sb.tostring (); for (int i=0;i<8;++i) {if (Binstr.charat (i) = = ' 0 ') {jedis.setbit ("test", pos+i,false); }else {jedis.setbit ("test", pos+i,true); }} pos+=8; } public static void Main (string[] args) {Jedis jedis=jedispool.getresource (); String s= "Hello"; try {for (int i = 0; i < s.length (); i++) {String binstr=integer.tobinarystring (S.charat (i) ); will take the ASCII encoding value and convert it to binary one char is two bytes (byte) 1byte=4bit a char has 8bit so the upper limit is 127. The difference between byte and bit is to remember solve (Binstr,jedis); } System.out.println (Jedis.get ("Test")); }finally {jedis.close (); } }}
Redis Practice-bitmaps