Sunday, March 26, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home Java

How to Use the Java TreeMap Data Structure

learningcode_x1mckf by learningcode_x1mckf
September 19, 2022
in Java
0
How to Use the Java TreeMap Data Structure
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


The Java TreeMap class shops knowledge in a tree construction utilizing a map interface. This class extends the AbstractMap class and, like its mother or father class, TreeMap has two kind parameters. One in all its kind parameters represents the keys within the TreeMap, whereas the opposite represents the values.

The TreeMap knowledge construction shops key-value pairs and permits you to carry out CRUD operations on this knowledge.


The way to Create a TreeMap in Java

The TreeMap class has 4 constructors that you should use to create a brand new TreeMap object. The default constructor is the preferred of the 4. This constructor takes no arguments and generates an empty tree map.


TreeMap<Integer,String> clients = new TreeMap<Integer,String>();

The code above generates an empty tree map known as clients.

Populating the TreeMap Knowledge Construction

The put() technique provides an merchandise to a TreeMap object. It takes two arguments—a key and its worth. You’ll be able to add gadgets to the tree map in any random order and the information construction will retailer them in ascending order, in line with their keys.


clients.put(105, "Jessica Jones");
clients.put(102, "Mark Williams");
clients.put(104, "Phil Blair");
clients.put(101, "Kim Brown");
clients.put(103, "Jim Riley");

The code above provides 5 clients, in random order, to the shoppers tree map.

Viewing Gadgets in a TreeMap

The TreeMap class shops its knowledge in an object. So, to see all of the gadgets in a tree map you possibly can merely print the tree map object to the console:


System.out.println(clients);

The code above prints the next output to the console:

101=Kim Brown, 102=Mark Williams, 103=Jim Riley, 104=Phil Blair, 105=Jessica Jones

Notice that the thing above shows the gadgets in ascending order. You too can view every merchandise and its corresponding key utilizing a Java for loop.


for (Entry<Integer, String> buyer : clients.entrySet())
System.out.println("Key: " + buyer.getKey() + " Worth: " + buyer.getValue());

The code above prints the next output to the console:

Key: 101 Worth: Kim Brown
Key: 102 Worth: Mark Williams
Key: 103 Worth: Jim Riley
Key: 104 Worth: Phil Blair
Key: 105 Worth: Jessica Jones

Updating Gadgets in a TreeMap

The TreeMap class permits you to replace an current merchandise utilizing the substitute() technique. There are two substitute strategies. The primary technique takes an current key and the brand new worth you wish to map the present key to.


clients.substitute(101,"Kim Smith");
System.out.println(clients);

The code above prints the next object within the console:

101=Kim Smith, 102=Mark Williams, 103=Jim Riley, 104=Phil Blair, 105=Jessica Jones

As you possibly can see Kim Brown is now Kim Smith. The second substitute() technique takes an current key, the important thing’s present worth, and the brand new worth you wish to map to the important thing.


clients.substitute(103,"Jim Riley", "Michelle Noah");
System.out.println(clients);

The code above prints the next object within the console:

101=Kim Brown, 102=Mark Williams, 103=Michelle Noah, 104=Phil Blair, 105=Jessica Jones

Within the object above Michelle Noah replaces Jim Riley.

Deleting Gadgets From the TreeMap

If you wish to take away a single merchandise from the tree map, the take away() technique is your solely choice. It takes the important thing related to the merchandise you wish to take away and returns the deleted worth.


clients.take away(104);
System.out.println(clients);

Operating the code above prints the next object to the console:

101=Kim Smith, 102=Mark Williams, 103=Michelle Noah, 105=Jessica Jones

This Java Class additionally has a clear() technique that permits you to delete all of the gadgets within the tree map.

The TreeMap vs. the HashMap Java Class

TreeMap and HashMap are two of the extra well-liked Java map lessons. They each lengthen the AbstractMap class. This relationship offers the TreeMap and HashMap lessons entry to lots of the identical capabilities.

Nonetheless, there are some noteworthy variations between these two map lessons. The TreeMap makes use of a Pink-Black tree implementation of the Map interface, whereas the HashMap makes use of a hash desk. HashMap permits you to retailer a single null key, whereas TreeMap doesn’t. Lastly, a HashMap is quicker than a TreeMap. The previous’s algorithmic velocity is O(1) whereas the latter’s is O(log(n)).



Source link

You might also like

2023 Java roadmap for developers – TheServerSide.com

YS Jagan launches Ragi Java in Jagananna Gorumudda, says focused on intellectual development of students – The Hans India

Disadvantages of Java – TheServerSide.com

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

2023 Java roadmap for developers – TheServerSide.com

by learningcode_x1mckf
March 26, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

2023 Java roadmap for developers  TheServerSide.com Source link

Read more

YS Jagan launches Ragi Java in Jagananna Gorumudda, says focused on intellectual development of students – The Hans India

by learningcode_x1mckf
March 26, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

YS Jagan launches Ragi Java in Jagananna Gorumudda, says focused on intellectual development of students  The Hans India Source link

Read more

Disadvantages of Java – TheServerSide.com

by learningcode_x1mckf
March 26, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Disadvantages of Java  TheServerSide.com Source link

Read more

Advantages of Java – TheServerSide.com

by learningcode_x1mckf
March 26, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Advantages of Java  TheServerSide.com Source link

Read more

Java Developer Survey Reveals Increased Need for Java … – Benzinga

by learningcode_x1mckf
March 25, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Java Developer Survey Reveals Increased Need for Java ...  Benzinga Source link

Read more
Next Post
Facebook Is JIT’ing C++ Code To Treat It Like A Crazy Fast Scripting Language

Facebook Is JIT'ing C++ Code To Treat It Like A Crazy Fast Scripting Language

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

SEGGER licenses C++ runtime library to SiFive for code size and performance efficiency – EEJournal

SEGGER licenses C++ runtime library to SiFive for code size and performance efficiency – EEJournal

October 11, 2022
Intro to Hilla: The full-stack Java framework

Intro to Hilla: The full-stack Java framework

October 16, 2022
Top 3 JavaScript Frameworks in 2022 | by Aniket | CodeX

Top 3 JavaScript Frameworks in 2022 | by Aniket | CodeX

September 25, 2022

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • 2023 Java roadmap for developers – TheServerSide.com
  • YS Jagan launches Ragi Java in Jagananna Gorumudda, says focused on intellectual development of students – The Hans India
  • Disadvantages of Java – TheServerSide.com

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?