In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Java to achieve byte [] to List code how to write, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.
Preface
In fact, this tool is written for yourself, because I always forget. So take notes.
Maven relies on com.google.guava guava 30.1.1-jre code package ai.guiji.csdn.tools;import com.google.common.primitives.Bytes;import java.util.Arrays;import java.util.List / * * @ Author swordsman A Liang _ ALiang @ Date 14:51 on 2022-1-26 @ Description: byte tool * / public class ByteUtils {/ * byte group to list * * @ param bytes byte group * @ return List * / public static List bytesToList (byte [] bytes) {return Bytes.asList (bytes) } / * list transferred byte group * * @ param list list * @ return byte [] * / public static byte [] listToBytes (List list) {return Bytes.toArray (list) } / * intercept bytes * * @ param bytes byte group * @ param from start bit * @ param to end bit * @ return bytes * / public static byte [] subBytes (byte [] bytes, int from, int to) {return Arrays.copyOfRange (bytes, from, to);}}
Code description
1, mainly through the guava toolkit to achieve, the code is simple.
Supplement
Java can realize not only byte [] to List, but also Map, List and byte [] to each other.
Map to byte [] can be converted from Map to json first, and then json to byte []. The idea of List and byte [] is the same.
/ / convert map to byte [] protected byte [] changeMapToByte (Map map) {byte [] bytes = null; try {bytes = JsonSerilizable.serilizableForMap (map). GetBytes ();} catch (Exception e) {baselog.error ("map to byte [] conversion exception", e);} return bytes } / / convert byte [] to map protected Map changeByteToMap (byte [] bytes) {Map retmap = null; try {if (bytes! = null) {retmap = JsonSerilizable.deserilizableForMapFromFile (new String (bytes), String.class);} else {baselog.error ("bytes is null in changeByteToMap") }} catch (Exception e) {baselog.error ("byte to map conversion exception", e);} return retmap;}
The JsonSerilizable class code is as follows:
Package com.jd.goldeneye.stat.common; import java.io.IOException;import java.util.HashMap;import java.util.List;import java.util.Map; import com.jd.fastjson.JSON;import com.jd.fastjson.TypeReference; public class JsonSerilizable {/ * serialize the linked list into a string in the json file * / public static String serilizableForList (Object objList) throws IOException {String listString = JSON.toJSONString (objList, true); / / (maps,CityEntity.class) Return listString;} / * read out the contents of the json file and deserialize it into a linked list * / public static List deserilizableForListFromFile (String listString2,Class clazz) throws IOException {List list2 = JSON.parseArray (listString2, clazz); return list2 } / * serialize the HashMap into a string and store it in the json file * / public static String serilizableForMap (Object objMap) throws IOException {String listString = JSON.toJSONString (objMap, true); / / (maps,CityEntity.class); return listString Read out the contents of the json file and deserialize them into HashMap * / public static HashMap deserilizableForMapFromFile (String listString2,Class clazz) throws IOException {Map map = JSON.parseObject (listString2, new TypeReference () {}); return (HashMap) map } / / usage Note: Entity is randomly defined. When using it, you can use / * String pathName = "src/test/java/com/./resources/file.json"; List entityList = new ArrayList (); JsonSerilizable.serilizableForList (entityList, pathName); List entityList2 = JsonSerilizable. PathName, Entity.class); HashMap Map = new HashMap () JsonSerilizable.serilizableForMap (Map, pathName); HashMap Map2 = JsonSerilizable. FormapfromFile (pathName, Entity.class); * /}
Test examples:
/ / convert map to byte [] protected byte [] changeMapToByte (Map map) {byte [] bytes = null; try {bytes = JsonSerilizable.serilizableForMap (map). GetBytes ();} catch (Exception e) {baselog.error ("map to byte [] conversion exception", e);} return bytes } / / convert byte [] to map protected Map changeByteToMap (byte [] bytes) {Map retmap = null; try {if (bytes! = null) {retmap = JsonSerilizable.deserilizableForMapFromFile (new String (bytes), String.class);} else {baselog.error ("bytes is null in changeByteToMap") }} catch (Exception e) {baselog.error ("byte to map conversion exception", e);} return retmap;} what is Java? Java is an object-oriented programming language that can write desktop applications, Web applications, distributed systems, and embedded system applications.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.