A cikin Java, yin aiki tare da rafuffuka da kirtani muhimmin bangare ne na aikin yau da kullun na mai haɓakawa. Ayyukan ajin StringJoiner a cikin wannan mahallin ba za a iya raina aikin ba. An gabatar da shi a cikin Java 8, StringJoiner aji ne mai amfani wanda ke gina jerin haruffan da aka raba ta hanyar mai iyaka kuma zaɓin rufe shi da prefix da kari. Wannan yana taimakawa wajen cimma ayyuka kamar haɗa rafi na kirtani ko alamu ta mai iyakancewa, musamman lokacin aiki tare da Streams API.
Wannan kayan aiki, wanda aka gina a ƙarƙashin kunshin java.util, yana nuna sauƙi, inganci, da sassauƙa, don haka ya mai da shi kayan aiki mai mahimmanci ga masu haɓakawa. Ajin StringJoiner yana kawar da ƙaƙƙarfan tsari na sarrafa iyakoki da hannu, yana rage yuwuwar kurakurai.
Maganar Matsalar
Sau da yawa yayin da ake mu'amala da rafi a cikin Java, kowane mai haɓaka yana fuskantar ƙalubalen haɗa igiyoyi ko wasu abubuwa, waɗanda su kansu sakamakon wasu ayyuka ne, zuwa cikin kirtani ɗaya mai ƙayyadaddun ƙayyadaddun ƙayyadaddun bayanai. Hanyoyin al'ada za su ƙunshi rubuta ƙarin madaukai da keɓance keɓancewa don cimma wannan, wanda ke sa lambar ta fi rikitarwa da ƙarancin karantawa.
Magani: Ajin StringJoiner
Ajin StringJoiner yana ba da mafita mai dacewa ga wannan matsalar. Ana iya amfani da shi don haɗa rafi na kirtani a cikin mafi inganci da fahimta. Ya ƙunshi ƙirƙirar misali na aji java.util.StringJoiner sannan kuma ƙara masa kirtani ta amfani da hanyar 'ƙara()'.
StringJoiner joiner = new StringJoiner(", "); joiner.add("one"); joiner.add("two"); String joined = joiner.toString();
Hanyoyin da ke da alaƙa da StringJoiner suna ba mu damar samar da prefix da kari, da amfani da sharuɗɗa kamar sarrafa lissafin fanko da saita tsoffin rubutu don lissafin fanko.
Bayanin mataki-mataki na Code
Amfani da ajin StringJoiner kai tsaye ne. Ga yadda za a iya amfani da shi:
1. Ƙirƙiri misali 'StringJoiner' ta hanyar ƙayyade maƙasudin cikin ginin. Wannan shine halin da ake amfani da shi tsakanin igiyoyin da za a haɗa su.
StringJoiner joiner = new StringJoiner(", ");
2. Kuna ƙara kirtani ko wasu abubuwa (waɗanda ke aiwatar da hanyar toString() zuwa misalin ''StringJoiner' ta amfani da hanyar ƙara(…):
joiner.add("one"); joiner.add("two");
3. A ƙarshe, don samun haɗin haɗin, kuna kiran hanyar toString() akan misalin StringJoiner.
String joined = joiner.toString();
Maɓallin da aka haɗa yanzu ya ƙunshi ƙimar "ɗaya, biyu".
Ƙarin Ayyuka da Laburaren karatu a cikin Java masu alaƙa da Haɗuwa da igiya
Java 8 kuma ya gabatar da wata hanya don haɗa kirtani: String.join(). Bugu da ƙari, hanyar Collectors.joining() daga ɗakin karatu na java.util.stream. Hakanan ya cancanci a ba da haske. Wannan hanyar tana ba mu damar haɗa rafi tare da iyakancewa, wanda ke nufin zaku iya haɗa igiyoyi da sauran abubuwa kai tsaye daga rafi.
Java ya ba mu ingantacciyar mafita da sauƙaƙe don haɗa igiyoyi ko abubuwa tare da masu iyakancewa a cikin nau'in StringJoiner, String.join(), da Collectors.joining(). Ji daɗin bincika waɗannan ayyuka a cikin ayyukan ci gaban ku na gaba!