Sharepoint - How to authenticate to SharePoint Online (Office 365) using REST API

I don't think it's possible to use digest or basic in Online.
From my perspective, you have different possible solution depending on your requirements.

From my POV there are three options available:

  1. Since you need user context, the easiest way for you is to create Azure AD application and use OAuth in order to obtain access token. You need to get access token for SharePoint resource specifically.
    There is a library for Azure AD and Java - ADAL for Java Sample using active-directory-java-webapp. Basically you need to create new application inside Azure AD, add SharePoint as "permission to other applications".
    Some useful links - Azure Active Directory developer's guide and Integrating applications with Azure Active Directory.
    For start, you can do a POC - create new application in azure ad, add permissions to manipulate sharepoint, use JAVA ADAL to get access token for sharepoint resource, and finally query list title with access token.

  2. Other way is to create SharePoint addin, but I'm not aware about JAVA component making you life simpler when programming SharePoint addins. Basically it also uses OAuth, but there is no authorization code flow, refresh token given you directly via POST request from SharePoint. To implement this approach you need to dig a bit into .NET implementation (SharePointContext.cs)

  3. The last option is to use direct username\password with federated authentication. I'm also not familiar if there is a corresponding library for JAVA, I can only give you an example in nodejs from here. In short, you need to POST preconfigured SAML XML to MS Online Security Token Service (STS), receive encoded token and exchange it to auth cookie with SharePoint Online. You can use a fiddler to see the actual requests.