<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sam Huang on Medium]]></title>
        <description><![CDATA[Stories by Sam Huang on Medium]]></description>
        <link>https://medium.com/@samtash1034?source=rss-3e9b84d6a726------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*SUsQhZN3p43I4FkdNIexUQ.png</url>
            <title>Stories by Sam Huang on Medium</title>
            <link>https://medium.com/@samtash1034?source=rss-3e9b84d6a726------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 18:01:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@samtash1034/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Java-你知道 equals 做的事情其實和 == 一樣嗎]]></title>
            <link>https://medium.com/@samtash1034/java-%E4%BD%A0%E7%9F%A5%E9%81%93-equals-%E5%81%9A%E7%9A%84%E4%BA%8B%E6%83%85%E5%85%B6%E5%AF%A6%E5%92%8C-%E4%B8%80%E6%A8%A3%E5%97%8E-46508fe7171f?source=rss-3e9b84d6a726------2</link>
            <guid isPermaLink="false">https://medium.com/p/46508fe7171f</guid>
            <category><![CDATA[string-pool]]></category>
            <category><![CDATA[equals]]></category>
            <category><![CDATA[int]]></category>
            <category><![CDATA[integer]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Sam Huang]]></dc:creator>
            <pubDate>Fri, 18 Apr 2025 15:52:56 GMT</pubDate>
            <atom:updated>2025-04-18T15:52:56.954Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*gLaLoqW0Wsd895kP.png" /></figure><h3>前言</h3><p>今天來介紹面試很常被問到的一題，equals 和 == 的差異</p><h3>==</h3><p>如果是<strong>基本類型</strong>，則比較 「內容」是否相同</p><blockquote><em>基本類型：byte、short、int、long、float…等</em></blockquote><pre><br>// 基本類型比較<br>int a = 10;<br>int b = 10;<br>System.out.println(a == b); // true</pre><p>如果是<strong>物件</strong>，則比較 「記憶體位置」 是否相同</p><blockquote><em>物件：String、Integer、Long、自定義的 Class…等</em></blockquote><pre>// 物件的比較<br>Student student1 = new Student(); <br>Student student2 = new Student();<br>System.out.println(student1 == student2); // false</pre><h3>equals</h3><p>比較「內容」是否相同</p><p>但是我們進到 Object.class 可以發現，equals 的底層實際上是通過 == 來比較的</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/331/0*rPL3l5kOe_9WTXOp.png" /></figure><blockquote><em>這也是為什麼標題會寫說 </em><em>equals 其實和 </em><em>== 做一樣的事情</em></blockquote><p>此時大家可能會有疑問，那為什麼使用 equals 比較 Integer的時候，會是比較內容，而不是記憶體位置</p><p>那是因為 Integer.class 有去重寫(Oeverride) equals 方法</p><p>首先我們進到 Integer.class 並找到 equals 方法</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/467/0*nCEyEoqJ-3BM4RIP.png" /></figure><p>可以發現 equals 方法會先把我們把 Integer 轉成 int，再用 == 去比較</p><blockquote><em>而我們剛剛有學到如果基本類型使用 == 的話則是去比較「內容」</em></blockquote><h3>== 的延伸</h3><p>對於 equals 方法相對來說比較不會出現問題，相比之下，使用 == 就可能會遇到一些問題，特別是涉及到記憶體位置的時候</p><h4>字串的比較</h4><p>因為 String 有 String Pool 的機制，因此是不是透過 new 的方式會導致比較的結果不同</p><pre><br>String s1 = &quot;hello world&quot;; // 先在 String Pool 中尋找有沒有存在「hello world」的字串，發現沒有則建立一個<br>String s2 = &quot;hello world&quot;; // 先在 String Pool 中尋找有沒有存在「hello world」的字串，發現已存在，直接引用<br>String s3 = new String(&quot;hello world&quot;); // 使用 new 創建的字串不會在 String Pool 裡面，而是在 heap 區裡面<br>System.out.println(s1 == s2); // true<br>System.out.println(s1 == s3); // false<br>System.out.println(s1 == s3); // false<br>System.out.println(s1.equals(s2)); // true<br>System.out.println(s1.equals(s3)); // true</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/694/0*_n2fg_DuL36mTdj0.png" /></figure><h4>不同型別的比較</h4><p>在 Java 中，如果我們比較不同型別的基本類型，會發生所謂的「自動型態提升(Automatic Type Promotion)」</p><h4>Integer 的比較</h4><p>Integer 的緩存機制：會自動緩存 -128 ~ 127 範圍內的整數，如果超出這個範圍的整數，則會創建一個新的 Integer 物件</p><h3>運行速度</h3><p>== 的速度會比 equals 快一些，因為是直接去比較記憶體位置</p><h3>結論</h3><p>在工作上自己還是使用 equals 比較多，哪怕是基本變數的比較，為了安全起見也還是一樣使用equals</p><p>自己完全沒遇過需要有比較兩個物件記憶體位置的時候，也可能是自己比較菜所以沒碰到這部分，如果有實際的案例歡迎分享！</p><p>完整的程式碼請參考 <a href="https://github.com/samtash1034/Sam-Blog-Example">GitHub</a></p><h3>參考資料</h3><p><a href="https://learnku.com/articles/49654">Java基本数据类型和Integer缓存机制</a> @LearnKu</p><p><a href="https://rickbsr.medium.com/%E6%B7%BA%E8%AB%87-string-pool-42c37db41322">淺談「String Pool」</a> @Medium</p><p><a href="https://worktile.com/kb/p/37775">java中的==和equals有什么区别</a> @worktile</p><p><a href="https://zhuanlan.zhihu.com/p/338350987">== 和 equals 的区别是什么？</a> @知乎</p><p><a href="https://www.linkedin.com/pulse/difference-between-equals-java-babar-shahzad/">Difference Between == and equals() in Java</a> @LinkedIn</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=46508fe7171f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[封包 Packet 是什麼？如何使用 Wireshark 查看封包內容]]></title>
            <link>https://medium.com/@samtash1034/%E5%B0%81%E5%8C%85-packet-%E6%98%AF%E4%BB%80%E9%BA%BC-%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8-wireshark-%E6%9F%A5%E7%9C%8B%E5%B0%81%E5%8C%85%E5%85%A7%E5%AE%B9-dae6390ebbce?source=rss-3e9b84d6a726------2</link>
            <guid isPermaLink="false">https://medium.com/p/dae6390ebbce</guid>
            <category><![CDATA[packet]]></category>
            <category><![CDATA[mac-address]]></category>
            <category><![CDATA[network]]></category>
            <category><![CDATA[osi-model]]></category>
            <category><![CDATA[ip]]></category>
            <dc:creator><![CDATA[Sam Huang]]></dc:creator>
            <pubDate>Thu, 17 Apr 2025 14:29:29 GMT</pubDate>
            <atom:updated>2025-04-17T14:29:29.391Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*HHrLuWXhzbiegSoM.png" /></figure><h3>前言</h3><p>在學習本章之前，有興趣的人可以先瞭解<a href="https://medium.com/@samtash1034/osi-moedel-7%E5%B1%A4%E6%9E%B6%E6%A7%8B%E4%BB%8B%E7%B4%B9-89b573525129">OSI 七層架構</a>、<a href="https://medium.com/@samtash1034/mac-address-%E6%98%AF%E4%BB%80%E9%BA%BC-%E5%AE%83%E5%92%8C-ip-address-%E6%9C%89%E4%BB%80%E9%BA%BC%E5%B7%AE%E7%95%B0-3b14674269b7">Mac Address</a>介紹</p><h3>什麼是封包?</h3><p><strong>封包 (Packet) 是網路傳輸的最小資訊單位</strong></p><p>今天電腦收到資料，會把資料劃分成好幾個封包，然後這些封包會由接收它們的電腦或裝置重新組合成資料</p><p>假設今天 Sam 想寫一封信寄給 Alice，可以看到在寄信的途中，信被切分成四小部分，也就是封包，當 Alice 收到信後，會把這四個封包組裝起來，再去讀這封信</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/446/1*YJCQoY6eN24vmjKrpnB2Ug.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/579/0*AgNGTeshmMZ1IZdT.png" /></figure><h3>封包標頭</h3><p>封包標頭會提供<strong>封包的內容、來源 IP 和目的 IP 等等</strong></p><p>網路上有一堆封包，要怎麼怎麼知道這個封包要傳給誰，因此就會找到標頭裡面的目的IP，並且將這封包傳到目的地(這邊用姓名來取代目的 IP)</p><p>當 Alice 收到 Sam 傳來的 4 個封包的時候，她還要組合起來，因此她會對照封包標頭最前面的數字，依照順序的去把這些封包組合起來，拼湊出完整的內容</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/357/0*TdEOey3TuR-DcCaD.png" /></figure><h3>封包標頭從哪裡來</h3><p>當數據在網路中傳輸時，會經過 OSI 定義的 7 層架構，封包會在經過的每一層都會添加自己的標頭，直到最下面一層，進而去生成完整的封包</p><h3>介紹每一層做的事</h3><h4>第５、６、７層-FTP/HTTP Header</h4><p>存放要傳遞的資訊</p><h4>第４層-TCP/UDP Header</h4><p>存放來源 Port 及目的 Port</p><p>補充 IP 和 Port 的關係：<br>假設我打開三台 Terminal，並且用第一台 Terminal 連線到 FTP Server，當 FTP Server 要回傳資料時，它可以透過 IP 找到我們這台電腦，找到電腦後還需要 Port 進而去判斷要回到哪一個 Terminal</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/452/0*29oShSrZYRhj9Ngf.png" /></figure><h4>第３層-IP Header</h4><p>存放來源 IP 及目的 IP</p><h4>第２層-Ethernet Header</h4><p>存放來源 MAC 及目的 MAC</p><h4>第１層</h4><p>實體層把 0101 二進制碼傳送出去</p><h3>如何使用 Wireshark 抓取封包</h3><p>Wireshark 是一個功能十分強大開源的網路封包剖析器，可即時從網路介面擷取封包中的資料</p><p>下載<a href="https://www.wireshark.org/download.html"> Wireshark</a></p><p>打開終端機，輸入 ftp 66.220.9.51，此時還不要按下 Enter</p><p>下載好 Wireshark 後，我們打開它，點擊上方的 Capture，選擇要抓封包的網卡(Traffic 可以看到網路的流量)，然後按下 Start 開始監控</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/717/0*PqBiZTujBqtakcUI.png" /></figure><p>此時到你的終端機上，把剛剛輸入的 ftp 66.220.9.51 按下 Enter，接著輸入帳號及密碼，帳號和密碼隨便輸入就好，因為我們只是要去看封包的資訊而已</p><pre>ftp 66.220.9.51<br>已連線到 66.220.9.51。<br>220 Welcome to the most popular FTP hosting service! Save on hardware, software, hosting and admin. Share files/folders with read-write permission. Visit http://www.drivehq.com/ftp/;<br>530 Please login with USER and PASS.<br>使用者 (66.220.9.51:(none)): sam<br>331 User name ok, need password.<br>密碼:<br>530 Not logged on, user (sam) or password incorrect!<br>登入失敗。</pre><h3>查看 Wireshark 封包層級資訊</h3><p>回到 Wireshark 上，點擊上方的 Stop，再來篩選我們要的資訊，在搜尋欄輸入 ftp</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*8zbiX1ZU5pVLVpoA.png" /></figure><h4>Ethernet II</h4><p>此處顯示來源 MAC 以及目的 MAC</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/523/0*0MI_nO-kCR3rXU6X.png" /></figure><h4>Internet Protocol Version 4</h4><p>此處顯示來源 IP 以及目的 IP</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/559/0*2RzsGJNU3FKaVmbl.png" /></figure><h4>Transmission Control Protocol</h4><p>此處顯示來源 Port 及目的 Port</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/485/0*7OnWVMiK2y8vArTv.png" /></figure><h4>File Transfer Protocol</h4><p>此處顯示我們傳送的資料</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/289/0*9Onm9zvoGWq0PtRW.png" /></figure><p>可以發現除了我用紅色框框標記起來的地方，每一層的 Header 還有其它更詳細的資訊，有興趣的人也可以深入研究</p><h3>參考資料</h3><p><a href="https://docs.tigera.io/calico/3.25/about/about-networking#anatomy-of-a-network-packet">About Networking</a> @TIGERA</p><p><a href="https://www.wireshark.org/">wireshark</a> @WIRESHARK</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dae6390ebbce" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[固定IP VS 浮動IP]]></title>
            <link>https://medium.com/@samtash1034/%E5%9B%BA%E5%AE%9Aip-vs-%E6%B5%AE%E5%8B%95ip-c116a9e0e3fe?source=rss-3e9b84d6a726------2</link>
            <guid isPermaLink="false">https://medium.com/p/c116a9e0e3fe</guid>
            <category><![CDATA[public-ip-address]]></category>
            <category><![CDATA[dynamic-ip]]></category>
            <category><![CDATA[static-ip]]></category>
            <category><![CDATA[ip]]></category>
            <category><![CDATA[network]]></category>
            <dc:creator><![CDATA[Sam Huang]]></dc:creator>
            <pubDate>Thu, 17 Apr 2025 14:21:23 GMT</pubDate>
            <atom:updated>2025-04-17T14:21:23.645Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ULxbPozgdXDPuA_G.png" /></figure><h3>前言</h3><p>在學習網路時很常聽到 Public IP、Private IP、IPv4、IPv6、Static IP、Dynamic IP…等，不太確定它們之間的差異在哪裡，今天要介紹的是根據分配方式來區分的 Static IP 和 Dynamic IP</p><h3>Static IP</h3><p>Static IP 翻成中文叫做「靜態 IP」</p><p>顧名思義就是不會動的 IP</p><p>通常在連線印表機或傳真機的時候會有固定 IP 的設定，不然每次要影印東西都要重新設定會麻煩許多</p><ul><li>架站的時候適合使用</li><li>因為 IP 是固定的，因此比較容易受到駭客攻擊</li><li>價格比較貴</li></ul><h3>Dynamic IP</h3><p>浮動 IP 翻成中文叫做「浮動 IP」</p><p>IP 是隨機，一般的用戶比較適合使用浮動 IP</p><p>IP 會隨著電腦開關機或是 ISP 業者重新發派 IP 而異動</p><p>因為浮動 IP 都會有租期所在，租期一到就會將 IP 收回，因此會斷線然後給予重新連線（舉例來說中華電的PPPOE浮動 IP 大約是 72 個小時換一次，時間通常在半夜）</p><ul><li>價格較便宜 設定較簡單，只需要將網卡下的 TCP/IP 設定自動取得 IP 即可</li><li>對於架站的人不那麼友善</li></ul><h3>參考資料</h3><p><a href="https://www.mobile01.com/topicdetail.php?f=110&amp;t=750030">Mobile01討論</a> @MOBILE01</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c116a9e0e3fe" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Springboot-Oauth2 Google 登入（不使用 Spring Security)]]></title>
            <link>https://medium.com/@samtash1034/springboot-oauth2-google-%E7%99%BB%E5%85%A5-%E4%B8%8D%E4%BD%BF%E7%94%A8-spring-security-b2ac1b2c584f?source=rss-3e9b84d6a726------2</link>
            <guid isPermaLink="false">https://medium.com/p/b2ac1b2c584f</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[spring-security]]></category>
            <category><![CDATA[oauth]]></category>
            <category><![CDATA[authorization]]></category>
            <category><![CDATA[spring-boot]]></category>
            <dc:creator><![CDATA[Sam Huang]]></dc:creator>
            <pubDate>Thu, 17 Apr 2025 14:16:43 GMT</pubDate>
            <atom:updated>2025-04-17T14:18:05.724Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*eOeG6waM6s5c65Uq.png" /></figure><h3>前言</h3><p>這裡將會介紹如何使用 Spring Boot 和 OAuth 2.0 實現 Google 登入功能。由於網路已經有太多相關的教學，但都是搭配 Spring Security，因此在這裡嘗試不使用 Spring Security 來達成第三方登入，也能讓自己對 Authorization Code Flow 更為熟悉</p><h3>建立 Goolge Oauth Credentials</h3><p>進到 Google Cloud 的 API 和服務</p><p>第一次進來要先創立專案，我這邊叫做 Oauth2 Login Demo</p><p>創立完成後要先去 Oauth 同意畫面設定，跟著指示輸入資料就好</p><p>上述步驟都完成後，點選建立憑證，這邊我們選擇 Oauth 用戶端 ID</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/0*PvMzDg6ec2R0uN9-.png" /></figure><p>應用程式類型選擇網頁應用程式</p><p>名稱隨意輸入</p><p>重新導向 URL 可以先跟我設定一樣，之後可以彈性修改</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/0*feOMjGorZPfr9YHM.png" /></figure><p>都輸入完成後，點選建立，接著把它提供的 JSON 下載下來</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/0*Reuc5S9hrorA_4t7.png" /></figure><p>JSON 打開後會長這樣，除了 project_id 和 auth_provider_x509_cert_url 以外，其它我們都用得到</p><pre>{<br>&quot;web&quot;:{<br>    &quot;client_id&quot;:&quot;800587325638-3dajgqr1644la8jh23utggkrevvmuhvm.apps.googleusercontent.com&quot;,<br>    &quot;project_id&quot;:&quot;oauth2-login-demo-395312&quot;,<br>    &quot;auth_uri&quot;:&quot;https://accounts.google.com/o/oauth2/auth&quot;,<br>    &quot;token_uri&quot;:&quot;https://oauth2.googleapis.com/token&quot;,<br>    &quot;auth_provider_x509_cert_url&quot;:&quot;https://www.googleapis.com/oauth2/v1/certs&quot;,<br>    &quot;client_secret&quot;:&quot;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&quot;,<br>    &quot;redirect_uris&quot;:[<br>        &quot;http://localhost:8080/callback/google&quot;<br>        ]<br>    }<br>}</pre><h3>Springboot Google 登入流程</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/547/0*i2_Ah3gmBNfsVvFc.png" /></figure><p>1️⃣</p><p>我們要拿 auth_uri 並帶上一些參數，向 google 請求同意授權的畫面</p><pre>https://accounts.google.com/o/oauth2/auth?<br>scope=profile%20email&amp;<br>response_type=code&amp;<br>redirect_uri=http://localhost:8080/callback/google&amp;<br>client_id=你的client_id</pre><p>scope：存取用戶資料範圍 ( profile 和 email 取得用戶的基本資料和信箱 )</p><p>response_type：告訴 Authorization Server 期望回應的類型</p><p>redirect_uri：用戶同意授權，會重定向到這個 URL</p><p>client_id：建立 Goolge Oauth Credentials 它幫我們產生的，這個 ID 讓Google知道是哪個應用程式正在發出授權請求</p><p>2️⃣</p><p>第一步的 URL 會引導使用者到 Google 的登入頁面，並選擇是否同意授權</p><p>這邊可以看到「Oauth2 Login Demo」，這就是我們當初建立的專案名稱</p><p>3️⃣</p><p>授權同意後，Authorization Server 會將用戶重定向到我們設定的 redirect_uri，同時帶一組 code 給我們，注意這個 code 具有唯一性和時效性</p><p>4️⃣</p><p>此時我們要用 tokne_uri 帶著 code 向 Authorization Server 換取 Access token</p><pre>https://oauth2.googleapis.com/token<br>Content-Type: application/x-www-form-urlencoded<br><br>code=第三步拿到的code&amp;<br>client_id=你的client_id&amp;<br>client_secret＝你的client_secret&amp;<br>redirect_uri=http://localhost:8080/callback/google&amp;<br>grant_type=authorization_code</pre><p>5️⃣</p><p>Authorization Server 驗證成功後，會回傳給我們 Access token</p><p>6️⃣</p><p>接著我們向 Resource Server 發起請求來獲取使用者資訊，請求中要帶著剛剛拿到的 Access Token．關於可以取得哪些資訊，我們在步驟一的 scop 參數就有設定了</p><pre>https://www.googleapis.com/oauth2/v3/userinfo<br>Authorization: Beare 第五步拿到的 Access Token</pre><p>7️⃣</p><p>Resource Server 回傳給我們使用者的資訊</p><h3>SpringBoot 設定</h3><h4>設置 application.yaml</h4><pre><br>oauth2:<br>registration:<br>google:<br>    client-id: 你的client-id<br>    client-secret: 你的client-secret<br>    redirect-uri: http://localhost:8080/callback/google<br>provider:<br>google:<br>    auth-uri: https://accounts.google.com/o/oauth2/auth<br>    token-uri: https://oauth2.googleapis.com/token<br>    user-info-uri: https://www.googleapis.com/oauth2/v3/userinfo</pre><p>這邊除了 user-info-uri，其他都是從剛剛下載的 JSON 檔可以得知的資訊</p><h4>設置 Controller</h4><pre>@RestController<br>public class GoogleLoginController {<br><br>    @Autowired<br>    private GoogleOauthService googleOauthService;<br><br>    // 步驟 1<br>    @GetMapping(&quot;/login/google&quot;)<br>    public String login() throws GeneralSecurityException, IOException {<br>        return googleOauthService.login();<br>    }<br><br>    // 步驟 3<br>    // 重新導向 URL<br>    @GetMapping(&quot;/callback/google&quot;)<br>    public String callback(@RequestParam String code) throws Exception {<br>        return googleOauthService.handleCallback(code);<br>    }<br><br>}</pre><h4>設置 Service</h4><pre>@Service<br>public class GoogleOauthServiceImpl implements GoogleOauthService {<br><br>    @Value(&quot;${oauth2.provider.google.auth-uri}&quot;)<br>    private String authUri;<br>    @Value(&quot;${oauth2.provider.google.token-uri}&quot;)<br>    private String tokenUri;<br>    @Value(&quot;${oauth2.provider.google.user-info-uri}&quot;)<br>    private String userInfoUri;<br>    @Value(&quot;${oauth2.registration.google.client-id}&quot;)<br>    private String clientId;<br>    @Value(&quot;${oauth2.registration.google.client-secret}&quot;)<br>    private String clientSecret;<br>    @Value(&quot;${oauth2.registration.google.redirect-uri}&quot;)<br>    private String redirectUri;<br><br>    @Autowired<br>    private RestTemplate restTemplate;<br><br>    @Override<br>    public String login() {<br>        return UriComponentsBuilder.fromHttpUrl(authUri)<br>                .queryParam(&quot;scope&quot;, &quot;profile email&quot;)<br>                .queryParam(&quot;response_type&quot;, &quot;code&quot;)<br>                .queryParam(&quot;redirect_uri&quot;, redirectUri)<br>                .queryParam(&quot;client_id&quot;, clientId)<br>                .build()<br>                .encode()<br>                .toUriString();<br>    }<br><br>    @Override<br>    public String handleCallback(String code) throws JsonProcessingException {<br>        // 步驟 4、5<br>        String accessToken = getAccessToken(code);<br><br>        // 步驟 6、7<br>        return getUserInfo(accessToken);<br>    }<br><br>    private String getAccessToken(String code) throws JsonProcessingException {<br>        HttpHeaders headers = new HttpHeaders();<br>        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);<br><br>        MultiValueMap&lt;String, String&gt; body = createTokenRequestBody(code);<br>        HttpEntity&lt;MultiValueMap&lt;String, String&gt;&gt; request = new HttpEntity&lt;&gt;(body, headers);<br><br>        ResponseEntity&lt;String&gt; response = restTemplate.postForEntity(tokenUri, request, String.class);<br>        String responseBody = response.getBody();<br><br>        ObjectMapper objectMapper = new ObjectMapper();<br>        JsonNode rootNode = objectMapper.readTree(responseBody);<br>        return rootNode.get(&quot;access_token&quot;).asText();<br>    }<br><br>    private MultiValueMap&lt;String, String&gt; createTokenRequestBody(String code) {<br>        MultiValueMap&lt;String, String&gt; body = new LinkedMultiValueMap&lt;&gt;();<br>        body.add(&quot;code&quot;, code);<br>        body.add(&quot;client_id&quot;, clientId);<br>        body.add(&quot;client_secret&quot;, clientSecret);<br>        body.add(&quot;redirect_uri&quot;, redirectUri);<br>        body.add(&quot;grant_type&quot;, &quot;authorization_code&quot;);<br><br>        return body;<br>    }<br><br>    private String getUserInfo(String accessToken) {<br>        HttpHeaders headers = new HttpHeaders();<br>        headers.set(&quot;Authorization&quot;, &quot;Bearer &quot; + accessToken);<br>        HttpEntity&lt;String&gt; entity = new HttpEntity&lt;&gt;(headers);<br><br>        ResponseEntity&lt;String&gt; response = restTemplate.exchange(userInfoUri, HttpMethod.GET, entity, String.class);<br><br>        return response.getBody();<br>    }<br><br>}</pre><h3>實際操作</h3><p>輸入 http://localhost:8080/login/google，拿到 Google 授權同意的 URL</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/0*EXMsLCux5xfJcqFT.png" /></figure><p>輸入 URL 後，會引導我們到登入頁面</p><p>這邊可以看到 「Oauth2 Login Demo」，也就是我們在建立 Goolge Oauth Credentials 的專案名稱</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/389/0*xgKWtd82-bKEcIgU.png" /></figure><p>登入成功和同意授權後，Authorization Server 會到重定向到 http://localhost:8080/callback/google，並帶上 code</p><p>成功拿到使用者的資訊了！！</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/0*FobFmyr3mnKVHyPi.png" /></figure><h3>參考資料</h3><p><a href="https://developers.google.com/identity/protocols/oauth2?hl=zh-tw">OAuth 2.0 存取 Google API</a> @Google Doc</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b2ac1b2c584f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[關於這個部落格的大小事]]></title>
            <link>https://medium.com/@samtash1034/%E9%97%9C%E6%96%BC%E9%80%99%E5%80%8B%E9%83%A8%E8%90%BD%E6%A0%BC%E7%9A%84%E5%A4%A7%E5%B0%8F%E4%BA%8B-3a8bd9309407?source=rss-3e9b84d6a726------2</link>
            <guid isPermaLink="false">https://medium.com/p/3a8bd9309407</guid>
            <category><![CDATA[website]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[ِarticle]]></category>
            <category><![CDATA[zeabur]]></category>
            <category><![CDATA[blog]]></category>
            <dc:creator><![CDATA[Sam Huang]]></dc:creator>
            <pubDate>Wed, 16 Apr 2025 14:30:37 GMT</pubDate>
            <atom:updated>2025-04-16T14:34:34.541Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*G7C6Ya5ZtHk9s6w3.png" /></figure><h3>動機</h3><p>其實一開始自己對於部落格沒有太大的想法，只是想要把工作的事情筆記下來，久而久之，筆記越來越多，想說有沒有一個可以統整的方式，後來就選擇寫部落格。</p><p>在寫部落格的時候，就希望自己呈現出來的東西是「高質量」、「易於理解」的，但最重要的還是內容的正確性</p><h3>部署的演進史</h3><p>分享一下在建立部落格時，我過往做的選擇，提供給有興趣的人參考</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/795/1*j7tJs0Qt1S8oqMKOH7XtTg.png" /></figure><h3>WordPress</h3><p>一開始架站是選擇 WordPress，只要把文章完成就好，其它的像是部署、備份、SEO，監控這些都在後台幫你做好</p><p>但後來因為自己想要高度客製化就棄用了</p><h3>AWS EC2</h3><p>因為自己有後端和資料庫，因此一定要一台 VM 才能滿足的我的需求，所以當初矇矇懂懂的我就去找了市占率最高的公有雲來租借主機</p><p>免費額度用完後收到帳單，發現一個月竟然要 2000 多，真的太貴了，因此選擇其他伺服器來架設</p><h3>DigitalOcean</h3><p>逃離 AWS 後，自己是選擇了 DigtialOcean 來架設我的部落格，不得不說這個網站的 UI 做得很好，使用者體驗也很不錯</p><p>一開始選擇約一個月 500 元的主機，但網速真的太慢，到後面才升級到 900 元</p><p>雖然也是不便宜都這樣也時刻提醒自己更不應該放棄寫 Blog</p><h3>Zeabur</h3><p>主打著「部署服務從未如此簡單」的名號，我就想說好來體驗看看</p><p>進去後才發現 Zeabur 是一個 Paas 平台，讓我可以專注於開發，而不用擔心伺服器的問題</p><p>進去設定好 GitHub 連結後然後直接部署，5分鐘內直接完成，它會直接判斷你使用的是什麼語言、框架以及對應的版本並直接做部署，我連 Dockerfile 都不用寫了</p><p>用下來體驗都不錯，而且它是根據流量來收費的，對我這種沒什麼人看的部落格來說是最好的了（？</p><h3>Medium</h3><p>最後使用的是Medium，最大的原因就是因為如果自己架部落格，我發現觸及率太低(有可能是我自己SEO太爛），另一方面也是希望文章能多被看見，這樣才不會違背自己想分享知識點給大家的初衷</p><h3>架構</h3><p>如果是自己架站的話，我的架構如下</p><p>前端：React + Ts + Scss</p><p>後端：SpringBoot</p><p>資料庫：MySQL</p><p>前端完成後我會 Build 成一包並放到後端的 static 資料夾中，這樣就只要部署 SpringBoot 就好了</p><h3>心得</h3><p>身為一個後端，平常接觸前端的機會少之又少，因此當初遇到最大的困難點就是畫面，好不容易補足一些前端的知識，想說可以不靠框架來架設網站，才發現很費力又很費時，因此又去學習 React，用了框架之後開發的效率大大的提升！</p><p>自己覺得寫部落格一方面是吸收新知，另一方面是把知識內化，並嘗試用淺顯易懂的方式讓別人完全理解，因此有些地方都會插入一些圖片來當作輔助，來更好的呈現我想表達的內容</p><p>目前給自己的目標是一個月產生一篇文章，期許自己 5 年後回過頭來看有達成目標～</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3a8bd9309407" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[《告別玻璃心的13件事》閱讀心得]]></title>
            <link>https://medium.com/@samtash1034/%E5%91%8A%E5%88%A5%E7%8E%BB%E7%92%83%E5%BF%83%E7%9A%8413%E4%BB%B6%E4%BA%8B-%E9%96%B1%E8%AE%80%E5%BF%83%E5%BE%97-e0bc70d2e50d?source=rss-3e9b84d6a726------2</link>
            <guid isPermaLink="false">https://medium.com/p/e0bc70d2e50d</guid>
            <category><![CDATA[read]]></category>
            <category><![CDATA[reading]]></category>
            <category><![CDATA[books]]></category>
            <dc:creator><![CDATA[Sam Huang]]></dc:creator>
            <pubDate>Tue, 15 Apr 2025 15:24:23 GMT</pubDate>
            <atom:updated>2025-04-15T15:25:54.237Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/900/0*bDlzOV9t5Mu_6yFC.png" /></figure><h3>前言</h3><p>這本書主要在告訴我們如何提升自己的<strong>心智強度</strong>，並且列出 13 項我們不應該做的事。這 13 件事主要是幫我們找出因應人生挑戰更好的方法，以避開那些陷阱</p><h3>ㄧ、不浪費時間自怨自艾</h3><blockquote><em>自憐是破壞力最強的非藥性毒品，容易上癮，給人一時的快感，進而脫離現實</em></blockquote><p>我們一定會經歷過人生的苦痛與哀傷，但有時候我們會沈浸在哀傷與不幸之中，想說自己遇到的問題比別人多很多，抱怨不公平，世界常常與我們作對等等</p><p>我們必須要停止自怨自艾，那只是在浪費時間，並且引發我們更多的負面情緒，還可能使我們忽略生命中的美好</p><p>自怨自艾讓人老是覺得<strong>我受到虧欠</strong>，感恩則讓人覺得<strong>我得到的比應得的還多</strong></p><p>我們可以時常保持感恩，並且感到自怨自艾時馬上轉念，有機會也可以多多參與活動</p><h3>二、不放棄主導權</h3><blockquote><em>我們憎恨敵人時，等於是讓他們主導了我們。我們的睡眠、食慾、血壓、健康、快樂都落入了他們的掌控</em></blockquote><p>讓別人控制我們的想法、感覺和行為時，我們就不可能心智堅強</p><p>如果喪失主導權，我們會對批評變得很敏感，讓別人左右我們的感受，長久下來會造成很大的內耗</p><h3>三、不怕改變</h3><blockquote><em>意志力不是有些人有，有些人沒有…而是有些人願意改變，有些人不願意</em></blockquote><p>我們很常騙自己那個壞習慣「沒那麼糟」，為自己找藉口</p><p>很多人不願意改變是為了迴避不安，但他們往往低估了自己承受不安的能力</p><p>專注在小改變，不要一次就想要達到特定的目標</p><h3>四、不在意無法掌控的事情</h3><blockquote><em>你也許無法控制發生在你身上的是，但你可以決定不被它們擊倒</em></blockquote><p>每件事都在掌握中，確實令人心安。<strong>但以為自己能掌控一切，卻可能是個問題</strong></p><p>想要掌控一切是浪費時間和精力，並且會對別人很嚴苛，覺得失敗都是自己的問題</p><p>專注在我們能掌控的事情上</p><p>練習學會「接受」</p><h3>五、不會想要處處迎合他人</h3><blockquote><em>在意他人想法，永為人囚</em></blockquote><p>有時候我們可能會覺得自己需要為別人的感覺負責</p><p>想辦法過符合個人信念的生活，而不是讓多數人開心的生活</p><h3>六、不怕審慎冒險</h3><blockquote><em>做事不要太膽怯拘謹，人生就是要實驗，實驗越多越好</em></blockquote><p>不審慎冒險，難以成大事</p><h3>七、不沉緬於過往</h3><blockquote><em>沈浸於往昔無法療癒傷痛，唯有活在當下才是良方</em></blockquote><p>有時候過去的遺憾令我們難以釋懷</p><p>花太多時間思考，要是當初做出稍微不同的選擇，人生會變成怎麼樣</p><p>沈湎過去就無法充分準備未來，阻礙我們前進</p><p>拒絕沈浸在過去，不是假裝往事從未發生過。我們要去接納過去，這樣才能活在當下。</p><p><strong>與過去和解使我們更堅強</strong></p><h3>八、不重蹈覆徹</h3><blockquote><em>只有我們無法從中學習的錯誤，才是真正的錯誤</em></blockquote><h3>九、不嫉妒他人成就</h3><blockquote><em>嫉妒就像自己喝毒藥，卻希望能毒死敵人</em></blockquote><p>避免和他人比較，就像比較頻果和橘子，我們都各有不同特才華、技巧和生活經驗，相互比較不是衡量自我價值的準確方式</p><p><strong>努力練習為他人喝采</strong></p><h3>十、不輕言放棄</h3><blockquote><em>失敗就是成功的過程，迴避失敗的人也遠離了成功</em></blockquote><p>有心練習比天賦更重要</p><h3>十一、不怕獨處</h3><blockquote><em>人類的悲苦，都是源自於無法獨自靜坐在房間裡</em></blockquote><p>每個月至少安排一天的「獨處日」</p><p>每天思考你的進度和目標</p><h3>十二、不怨天尤人</h3><blockquote><em>別到處怨天尤人，這個世界沒有欠你什麼，它比你先來</em></blockquote><p>以為自己是特例，所以不太需要努力，或不需要經歷別人遇到的過程，那樣的想法都不太健康</p><p>注意自己是否有「自命不凡」的心態</p><p>人生本來就很不公平</p><p>虛心接受批評，承認缺失和弱點</p><p>謙虛使我們更堅強</p><h3>十三、不求立竿見影</h3><blockquote><em>耐力、毅力、努力是所向無敵的成功組合</em></blockquote><p>看不到立即的成效，你就認為那樣做沒效</p><p>我們很容易缺乏耐心、高估自己的能力而且低估改變需要的時間</p><p>進度不見得都很明顯</p><p>過程中慶祝每個里程碑的達成</p><h3>結語</h3><p>我們要維持<strong>心智強度</strong></p><p>就跟體力一樣，心智強度也需要持續鍛鍊，永遠都有進步的空間</p><p>心智強度一旦疏於維持或強化，就會像肌肉一樣萎縮</p><h3>參考資料</h3><p><a href="https://www.books.com.tw/products/0010694737?gad_source=1&amp;gclid=CjwKCAiAg9urBhB_EiwAgw88meny8pMhJD_ElFpniyzc0SBTn_pBy0zZCB28pV2f85ixW1xBJko0phoCynkQAvD_BwE">告別玻璃心的十三件事：心智強者，不做這些事</a> @博客來</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e0bc70d2e50d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[處理 HashMap 衝突的背後英雄（下）]]></title>
            <link>https://medium.com/@samtash1034/%E8%99%95%E7%90%86-hashmap-%E8%A1%9D%E7%AA%81%E7%9A%84%E8%83%8C%E5%BE%8C%E8%8B%B1%E9%9B%84-%E4%B8%8B-0b8061499fc1?source=rss-3e9b84d6a726------2</link>
            <guid isPermaLink="false">https://medium.com/p/0b8061499fc1</guid>
            <category><![CDATA[mập]]></category>
            <category><![CDATA[collision]]></category>
            <category><![CDATA[hashmap]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Sam Huang]]></dc:creator>
            <pubDate>Tue, 15 Apr 2025 15:21:15 GMT</pubDate>
            <atom:updated>2025-04-15T15:21:15.053Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/928/0*rJwMv4d11r8sj9mO.png" /></figure><h3>前言</h3><p><a href="https://medium.com/@samtash1034/%E8%99%95%E7%90%86-hashmap-%E8%A1%9D%E7%AA%81%E7%9A%84%E8%83%8C%E5%BE%8C%E8%8B%B1%E9%9B%84-%E4%B8%8A-892e4036b7a9">上一篇</a> 主要介紹了 HashMap 存資料時會做什麼事，也介紹了雜湊碰撞的其中一個解決方法 — 「封閉尋址法」</p><p>今天會繼續介紹碰撞的另外三種解法，它們都被歸納在「開放尋址法」，分別是：</p><p>線性探測（Linear Probing）</p><p>二元探測（Quadratic Probing）</p><p>雙重雜湊（Double Hashing）</p><h3>前置作業</h3><p>這邊故意讓 hash(c) 的 index 和 hash(a) 一樣都為 0</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/829/0*KlE8W6LSkh4hyS72.png" /></figure><h3>線性探測（Linear Probing）</h3><p>今天我們想要將 &lt;c, item3&gt; 插入到 index 0，卻因為已經有值而發生碰撞</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/923/0*fA9FoGNEY7XhJJfX.png" /></figure><p>於是 item3 開始往下找，一格一格慢慢找，直到找到可用空間來插入</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/916/0*AiK6fXMg00pAchtK.png" /></figure><blockquote><em>會從當前碰撞的位置由左至右尋找可用空間，如果到底再從頭繼續尋找</em></blockquote><h3>二元探測（Quadratic Probing）</h3><p>二元探測和線性探測很像，只是他是以<strong>平方</strong>的方式往下探測，直到找到可用空間</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/921/0*6neJURWuLZmxNgvo.png" /></figure><h3>雙重雜湊（Double Hashing）</h3><p>如果遇到衝突，則換個方法來計算 index，以此來變更儲存的位置</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/927/0*ywvqgIDUG2T7aFX7.png" /></figure><h3>結論</h3><p>完整的程式碼請參考 <a href="https://github.com/samtash1034/Sam-Blog-Example">GitHub</a></p><h3>參考資料</h3><p><a href="https://sam-blog.com/article/java/8/Hash%20Table%20Data%20structure">Hash Table Data structure</a> @tutorialspoint</p><p><a href="https://www.youtube.com/watch?v=JDPv1UCXpCU&amp;ab_channel=1MINLearning">Quadratic probing explained in 1 minute</a> @Youtube</p><p><a href="https://www.youtube.com/watch?v=vPvxEDyxI2w&amp;list=PLhxdaTcUMi3nRM5mtOdQgO4VEtAEsTiYd&amp;index=21&amp;t=304s&amp;ab_channel=%E5%A5%AEgame%E7%8E%8B%E7%B4%AB%E6%A5%93">輕鬆搞懂資料結構: 雜湊(hash)</a> @Youtube</p><p><a href="https://www.baeldung.com/cs/hashing-linear-probing">Hashing — Linear Probing</a> @Baeldung</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0b8061499fc1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[處理 HashMap 衝突的背後英雄（上）]]></title>
            <link>https://medium.com/@samtash1034/%E8%99%95%E7%90%86-hashmap-%E8%A1%9D%E7%AA%81%E7%9A%84%E8%83%8C%E5%BE%8C%E8%8B%B1%E9%9B%84-%E4%B8%8A-892e4036b7a9?source=rss-3e9b84d6a726------2</link>
            <guid isPermaLink="false">https://medium.com/p/892e4036b7a9</guid>
            <category><![CDATA[mập]]></category>
            <category><![CDATA[collision]]></category>
            <category><![CDATA[hashmap]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Sam Huang]]></dc:creator>
            <pubDate>Tue, 15 Apr 2025 15:17:42 GMT</pubDate>
            <atom:updated>2025-04-15T15:17:42.617Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/928/0*zi2nH6PCxJLJBLhG.png" /></figure><h3>前言</h3><p>這次的主題比較龐大，因此拆分為上下兩部分，讓讀者可以分兩次來看，也不會一次閱讀太多吸收不了</p><h3>HashMap 在存資料時實際上做了什麼事情</h3><pre><br>Map&lt;String, String&gt; map = new HashMap&lt;&gt;();<br>map.put(&quot;a&quot;, &quot;item1&quot;);<br>map.put(&quot;b&quot;, &quot;item2&quot;);<br>map.put(&quot;c&quot;, &quot;item3&quot;);<br>map.put(&quot;d&quot;, &quot;item4&quot;);<br>map.put(&quot;e&quot;, &quot;item5&quot;);</pre><p>相信大家對上面這段程式碼並不陌生，只是簡單的將 key-value 塞入到 HashMap 中</p><p>這時 HashMap 主要會做三件事情</p><p>1.將我們的 key 透過 hash function 變成雜湊值</p><p>2.用這個雜湊值就算出一個索引值</p><blockquote><em>索引值的計算這邊就不深入探討，通常是根據雜湊表(Hash Table)的大小，並且取餘數</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/829/0*nXr3oZr5taUsxtOC.png" /></figure><p>3.根據 index 找到在 Hash Table 的相對應位置，並把資料塞進去</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/997/0*XgUUDzz2AZySb--G.png" /></figure><blockquote><em>眼尖的人可能已經發現，hash(a) 和 hash(c) 所產生的 index 都同樣為 0</em></blockquote><h3>Map Collision 是什麼</h3><p>Collison 翻成中文就是「碰撞」</p><p>因此 Map Collision 最常見的說法就是「雜湊衝突」、「雜湊碰撞」</p><p>如果兩個不同的 key 最終產生的 index 一模一樣，這種情況我們就稱為「雜湊碰撞」</p><p>當碰撞出現的時候，主要透過以下常見的方法去處理：</p><p>封閉尋址法（Closed Addressing）</p><p>開放尋址法（Open Addressing）</p><ul><li>線性探測（Linear Probing）</li><li>二元探測（Quadratic Probing）</li><li>雙重雜湊（Double Hashing）</li></ul><blockquote><em>這篇主要會介紹「封閉尋址法」，對「開放尋址法」有興趣的人可以參考</em><a href="https://sam-blog.com/article/java/8"><em>這篇</em></a></blockquote><h3>封閉尋址法（Closed Addressing）</h3><p>仔細思考這個方法的名稱，它希望「封閉」尋址，也就是不往外找其它的位址存放，而是延續現有的儲存空間做存放</p><p>我們已經知道 hash(a) 和 hash(c) 所產生的 index 相同，此時就會發生碰撞，因此我們就會用一條 Linked List ，來存放碰撞的值</p><blockquote><em>可以把 Linked List 想像成火車，一節車廂接著一節</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/682/0*dwlKSFEA2Ddg1A6V.png" /></figure><h3>Java 遇到 HashMap Collision 時會怎麼處理？</h3><p>其實 Java 對於衝突的解決機制，就是透過「封閉尋址法」來解決的</p><p>但是如果當我們的 Linked List 太長，則會自動將 Linked List 轉成紅黑樹(Red-Black-Tree)的方式來儲存，藉此來提升查詢的速度</p><blockquote><em>Linked List 轉換成 Red Black Tree 我們可以統稱為「treeify」</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/710/0*Y023yEBIgpcLnSB5.png" /></figure><p>Linkied List 太長的標準為何？</p><p>進到 HashMap 的源碼可以看到有一個參數叫做 TREEIFY_THRESHOLD ，並且預設為 8</p><p>也就是說當我們的 Linked List 的長度大於等於 8 時，就會自動轉換成 Red Black Tree</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*3t7PBBYHF9TMaan-.png" /></figure><h3>結論</h3><p>完整的程式碼請參考 <a href="https://github.com/samtash1034/Sam-Blog-Example">GitHub</a></p><h3>參考資料</h3><p><a href="https://stackoverflow.com/questions/47921663/when-and-how-does-hashmap-convert-the-bucket-from-linked-list-to-red-black-trees">HashMap convert the bucket from linked list to Red Black Trees</a> @Stack Overflow</p><p><a href="https://rickbsr.medium.com/%E6%B7%BA%E8%AB%87-hash-hashtable-%E8%88%87-hashmap-4e5f5e5d36da">淺談「Hash」、「Hashtable」與「HashMap」</a> @Medium</p><p><a href="https://learnloner.com/how-does-a-hash-map-handle-collisions-in-java/">How does a Hash map handle collisions in JAVA.</a> @learnloner</p><p><a href="https://medium.com/@ralph-tech/%E8%B3%87%E6%96%99%E7%B5%90%E6%A7%8B%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-%E9%9B%9C%E6%B9%8A%E8%A1%A8-hash-table-15f490f8ede6">資料結構學習筆記：雜湊表（Hash Table）</a> @Medium</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=892e4036b7a9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[OSI Moedel 7層架構介紹]]></title>
            <link>https://medium.com/@samtash1034/osi-moedel-7%E5%B1%A4%E6%9E%B6%E6%A7%8B%E4%BB%8B%E7%B4%B9-89b573525129?source=rss-3e9b84d6a726------2</link>
            <guid isPermaLink="false">https://medium.com/p/89b573525129</guid>
            <category><![CDATA[osi]]></category>
            <category><![CDATA[7-layers-of-osi-model]]></category>
            <category><![CDATA[network]]></category>
            <category><![CDATA[osi-model]]></category>
            <category><![CDATA[tcp-ip]]></category>
            <dc:creator><![CDATA[Sam Huang]]></dc:creator>
            <pubDate>Tue, 15 Apr 2025 15:11:09 GMT</pubDate>
            <atom:updated>2025-04-15T15:11:09.056Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*gJ7t94U2FaWKVrjV.png" /></figure><h3>前言</h3><p>平常在瀏覽網站、寄送 gmail 或是看 YT 的時候，我們很少會去注意到背後所發生的事，今天透過介紹 OSI 網路七層架構來自己更了解不同的網路裝置的軟硬體是如何順利地互相溝通，也能讓自己更了解網路背後的運作原理</p><h3>為何要瞭解 OSI 七層架構，OSI 是什麼？</h3><p>OSI（Open System Interconnection Model）是由 ISO — 國際標準化組織 於 1984 年開發。它是一個 7 層架構，每一層都具有要執行的特定功能。所有這 7 層協同工作，在全球範圍內將數據從一個人傳輸到另一個人</p><p>為什麼需要定義網路七層的標準？假設今天我 Windows 的電腦想要傳送資料給 Mac，但其中使用的網卡、網路線和作業系統都不同，而這些東西都不一樣怎麼能互相溝通，因此我們才需要定義網路七層的標準及規範</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/799/0*djuStFWvC5yDeSBs.png" /></figure><p>下面就來介紹每一層會做的事</p><h3>第 7 層 — 應用層 Application Layer</h3><p>應用層是最<strong>接近使用者</strong>的一層，它是用戶和網路服務互動的窗口</p><p>應用軟體本身並不駐留在應用層</p><p>應用層為用戶<strong>提供網路服務，設定與另一端應用軟體之間的通訊</strong>，像是 FTP (文件的傳輸)、HTTP/S(網頁的瀏覽)、SMTP(郵件)等等</p><h3>第 6 層 — 表達層 Presentation Layer</h3><p>表達層拿到應用層傳送過來的資料後，主要會對資料做三件事</p><p>傳送方：</p><p>１. 編碼：把資料編碼成 0 和 1，轉換成電腦看得懂的語言</p><p>２. 壓縮資料：減少檔案傳輸的大小 ，就像 5MB 的文件壓縮後可以變到 2MB</p><p>３. 加密</p><p>以次類推，接收方的表達層做的事就是把會議層的資料拿來做解密、解壓縮、解碼</p><h3>第 5 層 — 會議層 Session Layer</h3><p>當有兩台設備需要互相通訊的時候，會議層會創建 Session 讓這兩台設備可以溝通</p><p>會議層允許兩個系統以半雙工或全雙工的方式開始互相通訊</p><p>雙方通訊如果完成，會議層就會關掉 Session，終止連接</p><p>會議層還會在數據傳輸的時候設置檢查點，如果 Session 意外中斷，可以從上一個檢查點恢復數據傳輸</p><h3>第 4 層 — 傳輸層 Transport Layer</h3><p>傳送方的傳輸層會把資料切割成好幾段</p><p>接收方的傳輸層則會把切割的資料組起來</p><p>接收方還會執行錯誤的控制，從而確保接收的資料完整無缺，如果不完整，可要求重新傳輸</p><p>傳輸層還負責流量的控制，確保快速連線的傳送者不會淹沒具有低速連線的接受者</p><p>TCP 和 UDP 的端口號工作也在這一層(TCP 80 port 就是要連網頁，TCP 21 port 就是要連我們的 FTP)</p><h3>第 3 層 — 網路層 Network Layer</h3><p>傳送方會把我們的資料加上來源 IP 以及目的 IP，然後 Router 會根據目的IP 去查詢路由表，再丟給下一台 Router</p><blockquote><em>對於 Router 不熟悉的人可參考這篇：</em><a href="https://sam-blog.com/article/network/5"><em>Hub、Switch 和 Router 的差異及運作原理</em></a></blockquote><h3>第 2 層 — 資料連結層 Data Link Layer</h3><p>資料連結層提供節點到節點的數據傳輸</p><p>這一層主要由兩個部分組成：</p><ul><li>邏輯連結控制 Logical Link Control (LLC)：識別網路協議、執行錯誤檢查</li><li>媒體存取控制 Media Access Control (MAC)：把數據加上來源 MAC 以及目的 MAC，並使用 MAC address 連接設備並定義傳輸和接收數據的權限</li></ul><p>傳送方會把資料轉成訊號，然後丟給第一層（實體層）的網路線（如果使用光纖訊號會被轉成光波、使用無線網路則會轉成電磁波）</p><p>接收方則會將接收到訊號或光波號後轉換成資料</p><h3>第 1 層 — 實體層 Physical Layer</h3><p>實體層負責網路節點之間的物理電纜或無線連接</p><p>實體層定義了連接設備的連接器、電纜或無線技術，並負責傳輸原始數據，即一系列 0 和 1，同時負責位元速率的控制</p><p>實體層還定義了數據在兩個連接的設備之間流動的方式以及各種傳輸模式像是單工、半雙工和全雙工</p><h3>參考資料</h3><p><a href="https://youtu.be/gxoIrBFfpDU">網路七層與TCP/IP</a> @阿彬電腦</p><p><a href="https://www.networkworld.com/article/964816/the-osi-model-explained-and-how-to-easily-remember-its-7-layers.html">The OSI model explained</a> @NETWORKWORLD</p><p><a href="https://www.geeksforgeeks.org/open-systems-interconnection-model-osi/">What is OSI Model? — Layers of OSI Model</a> @geeksforgeeks</p><p><a href="https://www.youtube.com/watch?v=vv4y_uOneC0&amp;ab_channel=TechTerms">OSI Model Explained</a> @TechTerms</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=89b573525129" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Router 路由器延伸學習-網段]]></title>
            <link>https://medium.com/@samtash1034/router-%E8%B7%AF%E7%94%B1%E5%99%A8%E5%BB%B6%E4%BC%B8%E5%AD%B8%E7%BF%92-%E7%B6%B2%E6%AE%B5-a3a83c5b54b3?source=rss-3e9b84d6a726------2</link>
            <guid isPermaLink="false">https://medium.com/p/a3a83c5b54b3</guid>
            <category><![CDATA[router]]></category>
            <category><![CDATA[switch]]></category>
            <category><![CDATA[traceroute]]></category>
            <category><![CDATA[hub]]></category>
            <category><![CDATA[network]]></category>
            <dc:creator><![CDATA[Sam Huang]]></dc:creator>
            <pubDate>Tue, 15 Apr 2025 15:07:04 GMT</pubDate>
            <atom:updated>2025-04-15T15:07:04.536Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*DPpqxobwaEGNhiCN.png" /></figure><h3>前言</h3><p>想要先了解 Switch 和 Router 可以先參考 <a href="https://sam-blog.com/article/network/6">Hub、Switch 和 Router 的差異及運作原理</a></p><h3>Router 區分網段</h3><p>Router 有一個作用就是<strong>區分網段</strong></p><p>以下方的圖作為例子</p><p>Switch1 是 192.168.1.0 的網段，A、B 、C 電腦都在這一個網段底下</p><p>Switch2 是 192.168.2.0 的網段，D、E、F 電腦都在這一個網段底下</p><p>網段相同：</p><p>今天當 A 電腦想要傳封包給 B 電腦，在經過 Switch 的時候，發現都在 192.168.1.0 這個網段底下，因此不用經過 Router，就可以直接傳遞封包</p><p>網段不同：</p><p>但當今天 A 電腦想要傳封包給 D 電腦，在經過 Switch後，發現彼此的網段不同，因此就需要經過 Router 來將封包傳給 D 電腦</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zi--Tji3j_C5U1hJ.png" /></figure><h3>traceroute 指令</h3><p>trareroute：追蹤路由及查詢路線</p><p>今天我們在自己的電腦上想要去訪問 Google 首頁，那其中會經過非常多台的 Router，不會只經過一台</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*IA5Y-ndKkzgVm1IQ.png" /></figure><p>當我們想要追蹤路由，就可以在 Terminal 中輸入 traceroute www.google.com指令</p><blockquote><em>使用 Windows 的人則可以輸入 tracert </em><a href="http://www.google.com/"><em>www.google.com</em></a></blockquote><p>要連到 Google 首頁之前，可以發現中間經過了 11 台 Router</p><p>如果發現訪問網頁的速度變慢，那我們可以查看 log 後面的時間看到底是慢在哪裡（每台設備 traceroute 會幫我們測試 3次）</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*g0BLht_1wE0NRAeA.png" /></figure><h3>參考資料</h3><p><a href="https://www.hellotech.com/guide/for/how-to-run-a-traceroute-mac">How to Run a Traceroute on a Mac</a> @HelloTech</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a3a83c5b54b3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>